2012-01-18 125 views
2

我正在嘗試修改其他人創建的批文件,以便根據文件的第4行中找到的編號添加前導零。實際文件名是在第3行中找到的名稱的串聯,並在第4行的數字因此,如果前幾行如下:將前導零添加到文件名

3.1.19 
-1 
TEST 
560 

的文件名是v_TEST00560.TXT。正如可以看到,在文件名的總位數應5.如果8(見下文),其出現在導線4的數目,則:

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運行良好,但是固定的數量爲零,而不是根據已存在的數字數量生成。任何幫助讚賞

回答

8

我不是要讀取所有的代碼,但我使用此填充零。

第一行是您從文件中讀取的數字。
第二行爲變量的起始處填充足夠多的零。
第三行刪除變量中除最後五個字符外的所有字符。

Set Number=123 

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

很好的答案......我不知道我是如何忘記這一點的。 – aphoria 2012-01-19 12:09:14

+2

+1「我不會讀全部......」 – 2012-01-19 12:17:44

+0

嗨,謝謝你的回覆。當談到腳本時,我很無能,所以這個後續問題不應該讓你大吃一驚。如何讓腳本從第4行中選擇數字? – Delphi 2012-01-19 14:27:46