Articles

For loop in DOS batch program

March 15, 2009 at 22:38:02
Specs: Windows XP

Dear friends,

This is my first post in this forum. Kindly correct me if i'm not adhering to any forum rules.

I am a newbie to DOS batch file programming. Kindly help me solving the problem i am encountering for the past 4 days!

I wrote the following code:

@echo on
:: HANDLE IMPROPER COMMAND LINE ARGUMENTS
If %1=="" (
echo Provide input file name and the output directory
exit /b
)
If %2 == "" (
echo Provide input file name and the output directory
exit /b
)

set input=%1
:: HANDLE FILE NOT FOUND CONDITION -- START
if not exist "%input%" (
echo File Not Found or Does not exist.
exit /b
)
:: HANDLE FILE NOT FOUND CONDITION -- END

:: PROCESS FILE -- START
if exist "%input%" (
for /f "tokens=1,2* delims=|" %%i in (%input%) do (
echo %%j > %2\Output.txt
set executableFile="C:\Program Files\ScanSoft\RealSpeak 4.0\standard.exe"
set language="Indian English"
set speechDirectory="C:\Program Files\ScanSoft\RealSpeak 4.0\speech"
set output=%2\Output.txt
call %executableFile% %language% Sangeeta %speechDirectory% "%output%"
set pcmFile=%%i.pcm
rename "standard.pcm" %pcmFile%
)
echo Completed generating '.pcm' files in this directory: %2
exit /b
)
:: PROCESS FILE -- END

I will be giving two input parameters to the batch file at the comand line.
(1) Input File Name
(2) Output directory
First parameter is a text file containing the following data:
1|Mr. NameOne
2|Mr. NameTwo
3|Mr. NameThree
As seen in the code, i am calling an executable, which runs perfectly. That executable will always generate a file called "standard.pcm". I have to rename it with the first token of the input file. Example:
1.pcm, 2.pcm, 3.pcm etc., Its parsing the first line properly, and renaming happens but from the second iteration, the variable doesn't get updated and stays on 1.pcm itself, displaying "duplicate file error".

Kindly help in solving this issue.

Thanks,
Vijay


See More: For loop in DOS batch program

Report •


#1
March 16, 2009 at 01:10:31

Vijay,

At the beginning of the batch file, you need to have a line
called-
setlocal EnableDelayedExpansion
and use
rename "standard.pcm" !pcmFile!
instead of
rename "standard.pcm" %pcmFile%

That would fix the problem you mentioned.
Also, I see other minor issues like

If %1=="" (
-will always fail the condition. Change it to
If "%1"=="" (

later,

--
Holla.


Report •

#2
March 16, 2009 at 02:11:29

Holla,

Thank you very much for your solution.
Just changed this
SETLOCAL ENABLEDELAYEDEXPANSION in my batch file and incorporated all suggestions given by you. It is now working fine!

Thanks,
Vijay


Report •

#3
March 16, 2009 at 03:06:36

Dear friends,

There is one more problem!
After making the above mentioned changes the batch file is working correctly when the path doesn't conatin any spaces. But if i give "C:\Program Files\My Folder\Input Name.txt" including the double quotes its neither throwing error nor generating the output.

Kindly provide your valuable help.

Thanks,
Vijay


Report •

Related Solutions

#4
March 16, 2009 at 05:17:10

Dear friends,

Kindly help me out on the above issue.

Thanks,
Vijay


Report •

#5
March 17, 2009 at 11:08:52

Vijay,

it gets confused when there are two times double quotes.
the lines -
set input=%1
if not exist "%input%" (

ends up generating two double-quotes when %1 has already
double quoted.

to rectify that,
use "%~1" - wherever you are referring to %1
%~1 removes any quotes supplied by the user, if there are
any.

That will result in a consistant behaviour no matter wheter
quotes are supplied or not.

--
Holla.


Report •


Ask Question