Computing.Net > Forums > Programming > Can't get file to work in for loop

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Can't get file to work in for loop

Reply to Message Icon

Name: pball
Date: October 9, 2008 at 18:52:28 Pacific
OS: XP Pro
CPU/Ram: 2.8 ghz / 2 gig
Product: home made
Comment:

I have this little script which has a settings file so the batch doesn't have to be edited.

echo Drag and drop settings file onto this window then hit enter
set /p settings=

for /f "tokens=*" %%a in (%settings%) do (
set %%a
)

This simple lets you drag and drop the settings file in the cmd window and sets the variable to the file path. The problem is I get this error when the for loop tries to run.

Drag and drop settings file onto this window then hit enter
"F:\Encoding\Scripts\Auto Encoder\settings_pball.txt"
Environment variable F:\Encoding\Scripts\Auto not defined
settings file not found

The setting file has var=setting for different things one per line. If works fine when I use a for loop to get the setting file that is in the same folder.

for /f "tokens=*" %%x in ('dir /b settings*.txt') do (
set settings=%%x
)

That is currently used to get the settings file from the same folder as the script.

I just want to know why I get this error when I try to use the drag and drop method. I use that input method with many other scripts I've made.



Sponsored Link
Ads by Google

Response Number 1
Name: Judago
Date: October 10, 2008 at 00:28:36 Pacific
Reply:

Try:


for /f "usebackq tokens=*" %%a in ("%settings%") do (
set %%a
)

If your file name contains a space, which is a delimiter for the "in" part of the for loop, it expects the part before the space to be one file and the next part to be another and so on.... If you simply surround it in double quotes then it is parsed as a string, but if you use the usebackq option the "meaning" of the quotes change, double quotes can then used for file names. It will still work fine for files without spaces, so I think it should solve your problem completely.


0

Response Number 2
Name: pball
Date: October 10, 2008 at 03:37:17 Pacific
Reply:

Thanks, I got it working by removing the quotes from the %settings% in the for loop. Since if you drag a file with a space in it's file path it adds quotes.

So it all works now, whether the settings file has spaces or no spaces in it's path.

Thanks, this one really bugged me since it didn't seem different from any other time I've tried doing this.

EDIT
is it just me or is thread above other threads that have newer posts?


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







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


Sponsored links

Ads by Google


Results for: Can't get file to work in for loop

Can't get Flash Movie to Play in IE www.computing.net/answers/programming/cant-get-flash-movie-to-play-in-ie/17210.html

Adding characters to variable in FOR loop www.computing.net/answers/programming/adding-characters-to-variable-in-for-loop/19704.html

i can't get clrscr() to work www.computing.net/answers/programming/i-cant-get-clrscr-to-work/3510.html