Please help with looping in a dos batch file
Posted: 01 Dec 2008 13:33
First, thank you all for this excellent forum. I've gone from zero knowledge about batch (other than knowing that I can get things done faster at the command line than in windows) to writing a passable dos batch file to process some text files.
I receive 68 text files each month. Each file begins with either R or H, and has the same name length - 15 characters. The 15th character can be either 1, 2, 3, or 4.
I'm interested in doing further processing on those with names names ending in 3.
On the files beginning with 'R' I want to rename them by choosing the 5th through the 8th characters of the old name. For the 'H' files, I want the 4th through the 8th characters.
Until now, copied the '3' files to another directory, and cut and pasted each of the files into a spreadsheet, added that part of the name I needed, then added additional information and dumped them into an access database.
(Yeah, I hear you groaning!)
I managed to write a DOS batch file that concatenates the file into one and adds the additional information. I then import it to the database.
Problem: I want to add code that separates the '3' files from the other without doing it manually.
Studying this forum and grabbing bits and pieces from all over the web, I've come up with this:
@ECHO on
setlocal enableextensions
setlocal enabledelayedexpansion
set "_offset=0" & REM starting with first character
set "_#chars=1" & REM number of characters to match
for %%a in (*3.txt) do call :process1 %%a
goto next
:process1
if "%_temp%"=="" goto :end
set "_temp=%%~na"
set "ID=!_temp:~%_offset%,%_#chars%!"
goto :end
:next
for %%a in (*3.txt) do (
IF !ID!==H call :Heiman
IF !ID!==R call :Rapiscan
)
:Heiman
set oldName=!_temp!
set newName=!oldName:~3,5!
echo ren !oldName! !newName!
:Rapiscan
set oldName=!_temp!
set newName=!oldName:~4,4!
echo ren !oldName! !newName!
:END
Everything I've tried to make it loop and do either one or the other CALL has resulted in continuous loops, or, just stopping.
Can somebody help, please?
(I can't use any of the Unix tools that have been ported to dos/windows because our computers are so PARANOID about security they're virtually useless. If Uncle Sam didn't put it there, I can't use it.)
Thanks for any light you guys can shed.
marye
P.S. Echo is on so I can see what the thing does, using pauses. I also find a program that steps me through the code 1 line at a time, and gives me an opportunity to see the (cryptic) error message.
One more thing: I'm still trying to figure out errorlevel, so if your solution uses that, please, please, please explain what it's doing.
edit: This code is only the part for separating and renaming the files. I can incorporate the code into the one that does the further processing.
I receive 68 text files each month. Each file begins with either R or H, and has the same name length - 15 characters. The 15th character can be either 1, 2, 3, or 4.
I'm interested in doing further processing on those with names names ending in 3.
On the files beginning with 'R' I want to rename them by choosing the 5th through the 8th characters of the old name. For the 'H' files, I want the 4th through the 8th characters.
Until now, copied the '3' files to another directory, and cut and pasted each of the files into a spreadsheet, added that part of the name I needed, then added additional information and dumped them into an access database.
(Yeah, I hear you groaning!)
I managed to write a DOS batch file that concatenates the file into one and adds the additional information. I then import it to the database.
Problem: I want to add code that separates the '3' files from the other without doing it manually.
Studying this forum and grabbing bits and pieces from all over the web, I've come up with this:
@ECHO on
setlocal enableextensions
setlocal enabledelayedexpansion
set "_offset=0" & REM starting with first character
set "_#chars=1" & REM number of characters to match
for %%a in (*3.txt) do call :process1 %%a
goto next
:process1
if "%_temp%"=="" goto :end
set "_temp=%%~na"
set "ID=!_temp:~%_offset%,%_#chars%!"
goto :end
:next
for %%a in (*3.txt) do (
IF !ID!==H call :Heiman
IF !ID!==R call :Rapiscan
)
:Heiman
set oldName=!_temp!
set newName=!oldName:~3,5!
echo ren !oldName! !newName!
:Rapiscan
set oldName=!_temp!
set newName=!oldName:~4,4!
echo ren !oldName! !newName!
:END
Everything I've tried to make it loop and do either one or the other CALL has resulted in continuous loops, or, just stopping.
Can somebody help, please?
(I can't use any of the Unix tools that have been ported to dos/windows because our computers are so PARANOID about security they're virtually useless. If Uncle Sam didn't put it there, I can't use it.)
Thanks for any light you guys can shed.
marye
P.S. Echo is on so I can see what the thing does, using pauses. I also find a program that steps me through the code 1 line at a time, and gives me an opportunity to see the (cryptic) error message.
One more thing: I'm still trying to figure out errorlevel, so if your solution uses that, please, please, please explain what it's doing.
edit: This code is only the part for separating and renaming the files. I can incorporate the code into the one that does the further processing.