将前导零添加到文件名

时间:2012-01-18 19:10:41

标签: batch-file dos batch-processing

我正在尝试修改由其他人创建的批处理文件,根据文件第4行上找到的数字添加前导零。实际文件名是第3行找到的名称和第4行的数字的串联。所以如果前几行如下:

3.1.19
-1
TEST
560

文件名为v_TEST00560.TXT。如您所见,文件名中的总位数应为5.如果第4行显示的数字为8(见下文),则:

3.1.19
-1
TEST
8

文件名为v_TEST00008.txt。 我的文件如下:

@Echo Off 

Setlocal EnableDelayedExpansion

REM File: rename5.bat
REM The script will look for and parse one (or more) input files 
REM Input files can containrecords for one or more vessels.
REM This script assumes that each record starts with the "3.1.19" string.

REM %%%%%%%%%%%%%%%%%%%%%%% Configuration Section %%%%%%%%%%%%%%%%%%%%%%%

SET INPUT_DIR=C:\Files\RenameFileName\Input
SET OUTPUT_DIR=C:\Files\RenameFileName\Output
SET ARCHIVE_DIR=C:\Files\RenameFileName\Archive
SET TEMP_DIR=C:\Files\RenameFileName\tmp
SET INPUT_FILENAME=INTERFACE.TXT
SET REC=3.1.19

REM %%%%%%%%%%%%%%%%%%%%%%%%%%%% Checking Section %%%%%%%%%%%%%%%%%%%%%%%%%%%%

FOR /F "usebackq tokens=* eol= delims= " %%d IN (`date /t`) do SET RUNDATE=%%d
echo [%RUNDATE% %TIME%] Script starting...

IF NOT EXIST %INPUT_DIR% (
    SET MESSAGE=Input directory not found.
    goto END
)

IF NOT EXIST %OUTPUT_DIR% (
    SET MESSAGE=Output directory not found.
    goto END
)

IF NOT EXIST %ARCHIVE_DIR% (
    SET MESSAGE=Archive directory not found.
    goto END
)

IF NOT EXIST %TEMP_DIR% (
    echo Temporary directory does not exit. 
    echo Creating %TEMP_DIR%
    mkdir %TEMP_DIR%
)


REM %%%%%%%%%%%%%%%%%%%%%%%%% Main Processing %%%%%%%%%%%%%%%%%%%%%%%%% 

dir %INPUT_DIR%\%INPUT_FILENAME% 1>NUL 2>NUL
IF %ERRORLEVEL% EQU 1 (
    SET MESSAGE=Input files not present.
    goto END
) 

FOR /F "usebackq tokens=* eol= delims= " %%d IN (`date /t`) do SET RUNDATE=%%d
echo [%RUNDATE% %TIME%] Input files found. Start Processing...

FOR /F "usebackq" %%I IN (`dir /b %INPUT_DIR%\%INPUT_FILENAME%`) DO (
    SET INPUT_FILE=!INPUT_DIR!\%%I
    echo READING Input file: !INPUT_FILE!

    SET N=
    FOR /F "tokens=* eol= delims= "  %%A IN (!INPUT_FILE!) Do (
        set LINE=%%A
        set LINE2=!LINE:~0,6!
        if !LINE2! EQU !REC! (
            SET /A N+=1
            echo Creating temp file !TEMP_DIR!\!N!.tmp
        )
        echo !LINE! >> !TEMP_DIR!\!N!.tmp
    )

    FOR /F "usebackq" %%Y in (`dir /b !TEMP_DIR!\*.tmp`) DO (
        SET TEMPFILE=!TEMP_DIR!\%%Y
        SET N=
        FOR /F %%A IN (!TEMPFILE!) DO (
            SET /A N+=1
            IF !N! EQU 3 SET S=%%A
            IF !N! EQU 4 SET T=%%A
        )
        SET S=!S:~0,10!
        SET T=!T:~0,10!

        echo CREATING Output File: %OUTPUT_DIR%\V_!S!00!T!.TXT
        MOVE !TEMPFILE!  %OUTPUT_DIR%\V_!S!00!T!.TXT
    )
)

REM %%%%%%%%%%%%%%%%%%%%%%%%% Archiving Section %%%%%%%%%%%%%%%%%%%%%%%%% 

FOR /F "usebackq" %%t IN (`cscript "%~dp0timestamp.vbs" //Nologo`) do SET TIMESTAMP=%%t

FOR /F "usebackq" %%I IN (`dir /b %INPUT_DIR%\%INPUT_FILENAME%`) DO (
    echo ARCHIVING Input file %%I to %ARCHIVE_DIR%
    rem COPY !INPUT_DIR!\%%I !ARCHIVE_DIR!\%%I.!TIMESTAMP!
    MOVE !INPUT_DIR!\%%I !ARCHIVE_DIR!\%%I.!TIMESTAMP!
)

REM %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

FOR /F "usebackq tokens=* eol= delims= " %%d IN (`date /t`) do SET RUNDATE=%%d
SET MESSAGE=[%RUNDATE% %TIME%] Processing Done.

:END
echo %MESSAGE%
FOR /F "usebackq tokens=* eol= delims= " %%d IN (`date /t`) do SET RUNDATE=%%d
echo [%RUNDATE% %TIME%] Script finished. 

正如你所看到的,它非常复杂,我不知道如何自己做这些改变。 BAT运行完美,但是如果固定则为零,而不是根据已存在的位数生成。任何帮助欣赏

1 个答案:

答案 0 :(得分:8)

我不打算阅读你的所有代码,但我用它来填充零。

第一行是您从文件中读取的数字。
第二行在变量的开头填充了足够多的零 第三行除了变量中的最后五个字符之外的所有字符。

Set Number=123

Set Number=00000%Number%
Set Number=%Number:~-5%