Page 1 of 1

check file not string

Posted: 27 Jul 2011 07:18
by batch_it
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

Re: check file not string

Posted: 27 Jul 2011 08:31
by Daeshawna
Hey! Are there any updates on this?

free tv

Re: check file not string

Posted: 27 Jul 2011 08:41
by batch_it
It still don't work. I have no idea how can I declare the file behind the string(path)

Re: check file not string

Posted: 27 Jul 2011 09:28
by dbenham
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

Re: check file not string

Posted: 28 Jul 2011 00:37
by batch_it
Hi Dave,

Thanks for your reply. Works well.