向文件名添加前导零

发布于 2024-12-28 05:27:51 字数 3537 浏览 2 评论 0原文

我正在尝试修改其他人创建的批处理文件,根据文件第 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 运行完美,但零的数量是固定的,并且不会根据已存在的位数生成。任何帮助表示赞赏

I am trying to modify a batch file created by somebody else, to add leading zeros depending on the number found on line 4 of the file. The actual filename is a concatenation of the name found on line 3, and the numbers on line 4. So if the first few lines are as follows:

3.1.19
-1
TEST
560

The file name would be v_TEST00560.TXT. As you can see, the total number of digits in the file name should be 5. If the number which appears on line 4 is 8 (see below), then:

3.1.19
-1
TEST
8

The file name will be v_TEST00008.txt.
The file I have is as follows:

@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. 

As you can see, its quite sophisticated, and I have no idea how to make these changes myself. The BAT runs perfectly, but the number of zeroes if fixed, and not generated depending on the number of digits already present. Any help appreciate

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

软甜啾 2025-01-04 05:27:51

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

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

Set Number=123

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

I'm not about to read all of your code, but I use this for padding zeros.

The first line is whetever number you read from your file.
The second line pads more than enough zeros to the start of the variable.
The third line cuts off all but the last five characters from the variable.

Set Number=123

Set Number=00000%Number%
Set Number=%Number:~-5%
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文