使用文件

时间:2016-03-17 12:38:10

标签: batch-file cmd batch-rename

我需要在批处理文件中使用的代码,该代码重命名从SAP创建的银行文件(我是SAP人),存储在服务器上的某个位置。

问题:所有银行文件都从SAP中的序列表中获取名称(日期+数字)。在我将它们发送到银行之前,它们必须具有一定的名称结构。

我有一个代码,到现在为止一直很好。现在的问题是我从SAP发送“批量”(几个)文件,它们随机命名 在每个文件的第一行中有一个唯一的批次ID,即银行序列号,并且必须按此顺序命名文件。 我已经做了很多VBA编程,但我不是很强调这个主题。

需要解决方案:我需要的是,对于每个文件,它应该读取文件的第一行并获取位置71和4位置向前,将其保存在变量中并将其添加到结尾的文件名。

示例:如果SAP的原始文件名是20160301-001.txt我想将其重命名为“P.00934681758.005.TBII.00000xxxx.txt”(其中“xxxx”是位置文件第一行中的71到74.

今天查看了很多银行目录,但是下面你可以找到今天的代码(这个工作,除了在这段代码中它重新编号为“a”,从没有1开始 - 从不超过9个文件)并且我想要将其替换为文件中的这4位数字:

今天的名字: P.00934681758.005.TBII.00000!a!.dat(“一个变量”)

新名称: P.00934681758.005.TBII.00xxxx.dat(“xxxx”文件中的数字)

今日代码(部分代码 - 显示2个“扫描”目录)

    @echo off & setlocal EnableDelayedExpansion 

REM Start by renaming all files in each folder to the namerule of the bank

cd PL270\UT01

set a=1

for /f "delims=" %%i in ('dir /b *') do (
    if not "%%~nxi"=="%~nx0" (
        ren "%%i" "P.00934681758.002.TBII.00000!a!".dat 
        set /a a+=1
 )
) 

cd..
cd..\PL570\UT01

set a=1

for /f "delims=" %%i in ('dir /b *') do (
    if not "%%~nxi"=="%~nx0" (
        ren "%%i" "P.00934681758.005.TBII.00000!a!".dat 
        set /a a+=1
 )
) 

代码今天在* .bat文件中运行,每隔5分钟在服务器上安排一次。

非常感谢所有帮助: - )

B.r。解决

2 个答案:

答案 0 :(得分:0)

据我所知,您需要将硬编码的a替换为您正在重命名的文件中的代码:

for /f "delims=" %%i in ('dir /b *') do (
    set /p fline=<"%%i"
    set code=!fline:~70,4!
    echo !code!
    if not "%%~nxi"=="%~nx0" (
        ren "%%i" "P.00934681758.002.TBII.00000!code!".dat 
        set /a a+=1
 )
)

(一旦我为SAP工作 - 我曾经有过无聊......)

答案 1 :(得分:0)

以下脚本遍历给定的目录树并搜索匹配的文件。如果文件的名称以今天的格式YYYYMMDD开头,后跟-,后跟一个数字,后跟扩展名.txt,则会认为该文件是匹配的。对于找到的每个文件,从第一行提取所需字符,然后将它们附加到前缀P.00934681758.005.TBII.00,并附加扩展名.dat

批处理文件的行为可以通过开头定义的常量来控制。要了解它的工作原理和发生的情况,请参阅整个文件中的rem条评论。

以下是代码:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem Definition of constants:
set "LOCATION=%~dp0" & rem Working directory; `%~dp0` is container, `.` is current;
set "RECURSIVE=#" & rem Flag to walk through working directory recursively, if defined;
set "SEPARATOR=-" & rem Separator character of original file name (one character!);
set "PATTERN=????????%SEPARATOR%*.txt" & rem Search pattern for `dir`;
rem Filter for `findstr /R` to match only correct file names (`.*` deactivates it):
set "REGEX=[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]%SEPARATOR%[0-9][0-9]*\.txt";
rem Filter for date prefix in file names (fixed date like `YYYYMMDD`, all files if empty):
set "TODAY=%DATE:~,4%%DATE:~5,2%%DATE:~8,2%" & rem Expected `%DATE%` format: `YYYY/MM/DD`;
set "NEWNAME=P.00934681758.005.TBII.00" & rem Beginning of new file name;
set "NEWEXT=.dat" & rem New file extension;
set "NUMLINE=0" & rem Zero-based line number where to extract characters from;
set "POSITION=70,4" & rem Zero-based start position, number of characters (length);
set "FORCE=#" & rem Flag to force extracted characters not to be empty, if defined;

rem Change to working directory:
pushd "%LOCATION%" || (
    >&2 echo Cannot find "%LOCATION%". & exit /B 1
)
rem Initialise some variables:
set /A NUMLINE+=0
if %NUMLINE% LEQ 0 (set "NUMSKIP=") else (set "NUMSKIP=skip^=%NUMLINE%^ ")
if defined RECURSIVE (set "RECURSIVE=/S ")
rem Walk through directory (tree) and search for matching file names (sorted by name):
for /F "eol=| delims=" %%F in ('
    dir /B %RECURSIVE%/A:-D /O:-N-D "%PATTERN%" ^| ^
        findstr /R /C:"^%REGEX%$" /C:"\\%REGEX%$"
') do (
    rem Split file name by given separator:
    for /F "eol=| tokens=1,* delims=%SEPARATOR%" %%I in ("%%~nxF") do (
        set "PREFIX=%%I"
        set "SUFFIX=%%J"
        setlocal EnableDelayedExpansion
        rem Check whether first part of file name matches today's date:
        if "%TODAY%"=="" (set "TODAY=!PREFIX!")
        if "!PREFIX!"=="!TODAY!" (
            set "OLDNAME=!PREFIX!%SEPARATOR%!SUFFIX!"
            rem Extract characters from predefined position from file (sub-routine):
            call :EXTRACT PORTION "!OLDNAME!"
            if defined FORCE (
                rem Check extracted character string if empty optionally:
                if not defined PORTION (
                    >&2 echo Nothing at position ^(%POSITION%^). & popd & exit /B 1
                )
            )
            rem Build new file name using extracted character string:
            set "BUILTNAME=%NEWNAME%!PORTION!%NEWEXT%"
            rem Check whether a file with the new name already exists:
            if not exist "!BUILTNAME!" (
                rem Actually rename file here (as soon as `ECHO` is removed!):
                ECHO ren "!OLDNAME!" "!BUILTNAME!"
            ) else (
                >&2 echo "!BUILTNAME!" already exists. & popd & exit /B 1
            )
        ) else (
            endlocal
            rem First part of file name does not match today's date, so leave loop:
            goto :CONTINUE
        )
        endlocal
    )
)
:CONTINUE
popd

endlocal
exit /B

:EXTRACT PORTION "FileSpec"
rem Sub-routine to extract characters from a file at a given position;
set "PART="
setlocal DisableDelayedExpansion
rem Read specified line from given file:
for /F usebackq^ %NUMSKIP%delims^=^ eol^= %%L in ("%~2") do (
    set "LINE=%%L"
    if defined LINE (
        setlocal EnableDelayedExpansion
        rem Extract specified sub-string of read line by position and length:
        set "PART=!LINE:~%POSITION%!"
        if  defined PART (
            for /F delims^=^ eol^= %%S in ("!PART!") do (
                endlocal
                set "PART=%%S"
            )
        ) else (
            endlocal
        )
    )
    rem Do not read any more lines from the file, leave loop:
    goto :QUIT
)
:QUIT
rem Return extracted character string:
endlocal & set "%~1=%PART%"
exit /B

只要您没有删除ECHO命令前的大写ren ,脚本就不会重命名。