Computing.Net > Forums > Programming > For loop in DOS batch program

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

For loop in DOS batch program

Reply to Message Icon

Name: Vijay1984
Date: March 15, 2009 at 22:38:02 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

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




Response Number 1
Name: Holla
Date: March 16, 2009 at 01:10:31 Pacific
+1
Reply:

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.



Response Number 2
Name: Vijay1984
Date: March 16, 2009 at 02:11:29 Pacific
+1
Reply:

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



Response Number 3
Name: Vijay1984
Date: March 16, 2009 at 03:06:36 Pacific
+1
Reply:

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



Response Number 4
Name: Vijay1984
Date: March 16, 2009 at 05:17:10 Pacific
+1
Reply:

Dear friends,

Kindly help me out on the above issue.

Thanks,
Vijay



Response Number 5
Name: Holla
Date: March 17, 2009 at 11:08:52 Pacific
+1
Reply:

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.



Related Posts

See More



Reply to Message Icon

Working Remote Printer In... Combining multiple newlin...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Google Ads



Results for: For loop in DOS batch program

XP Pro Batch Script -FOR loop issue www.computing.net/answers/programming/xp-pro-batch-script-for-loop-issue/16073.html

count in for loop in dos www.computing.net/answers/programming/count-in-for-loop-in-dos/16609.html

FOR loop www.computing.net/answers/programming/for-loop/18900.html