check file not string

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
batch_it
Posts: 3
Joined: 27 Jul 2011 06:59

check file not string

#1 Post by batch_it » 27 Jul 2011 07:18

Hi guys,

I need your help in the following script.

If I put the batch file in the same dir as the file I want to check it work in that way:

Code: Select all

for /F "tokens=1,2 delims==" %%i in (Settings.ini) do (
echo %%i
)



But if I want to use a variable the script try to split the string of the variable and not the content of the file.

Code: Select all

set oldini="%APPDATA%\Settings.ini"
for /F "tokens=1,2 delims==" %%i in ("%oldini%") do (
echo %%i
)


thanks in advance

Daeshawna
Posts: 1
Joined: 27 Jul 2011 07:55

Re: check file not string

#2 Post by Daeshawna » 27 Jul 2011 08:31

Hey! Are there any updates on this?

free tv

batch_it
Posts: 3
Joined: 27 Jul 2011 06:59

Re: check file not string

#3 Post by batch_it » 27 Jul 2011 08:41

It still don't work. I have no idea how can I declare the file behind the string(path)

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: check file not string

#4 Post by dbenham » 27 Jul 2011 09:28

batch_it wrote:But if I want to use a variable the script try to split the string of the variable and not the content of the file.

Code: Select all

set oldini="%APPDATA%\Settings.ini"
for /F "tokens=1,2 delims==" %%i in ("%oldini%") do (
echo %%i
)
The batch is doing exactly what you told it to do:

FOR /F %%i IN (file1 file2) - operates on a set of files
FOR /F %%i IN ("string") - operates on a string
FOR /F %%i IN ('command') - operates on the output of a command

Your code has the double quotes, so it is treated as a string.

But %APPDATA% may contain spaces, so you need to use double quotes to keep the filename together...
...that is what the "usebackq" option is for

FOR /F "usebackq" %%i IN ("filename1 with spaces" file2) - operates on a set of files, perhaps with spaces
FOR /F "usebackq" %%i IN ('string') - operates on a string
FOR /F "usebackq" %%i IN (`command`) - operates on a command.

Also, your oldini is already defined with double quotes, so "%oldini%" expands to ""somePath"" - not what you want.

Try:

Code: Select all

set oldini="%APPDATA%\Settings.ini"
for /F "usebackq tokens=1,2 delims==" %%i in (%oldini%) do (
echo %%i
)


Dave Benham

batch_it
Posts: 3
Joined: 27 Jul 2011 06:59

Re: check file not string

#5 Post by batch_it » 28 Jul 2011 00:37

Hi Dave,

Thanks for your reply. Works well.

Post Reply