|
Answer» 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:
Code: [Select]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 appreciated.
Code: [Select]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 )
This is the section you are going to need to modify under the main processing section. It's just a matter of adding an extra variable and doing a little modification. See below:
Code: [Select]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!
SET OUTNUM=0000!T! SET OUTNUM=!OUTNUM:~-5!
echo CREATING Output File: %OUTPUT_DIR%\V_!S!!OUTNUM!.TXT MOVE !TEMPFILE! %OUTPUT_DIR%\V_!S!!OUTNUM!.TXT )
Let us know if you need this explained or would like to know more in depth what is happening.Thanks for replying so quickly and indicating the changes needed so clearly. One last question. Any goo sites where I can BRUSH up on DOS scripting?http://www.robvanderwoude.com/ http://www.dostips.com/
|