cannot use variable as path for for loop

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
rusty_uk
Posts: 5
Joined: 18 Jan 2017 04:00

cannot use variable as path for for loop

#1 Post by rusty_uk » 18 Jan 2017 04:08

Hi

Apologies for the simplicity of this, but Batch is not my native tongue :(

Im trying to clean up a script to use variables as paths rather than defining them inline. But everytime i put a variable name into the for loop it fails.

So the following snip works

Code: Select all

setlocal EnableDelayedExpansion
set tempfile="c:\windows\temp\diskpart_create_tmp.txt"
set listfile="C:\windows\temp\diskpark_listdisks_tmp.txt"
set otherfile="C:\windows\temp\diskpart_listdisks_output.txt"
echo off
ECHO rescan>!listfile!
ECHO list disk >>!listfile!
diskpart /s !listfile! > !otherfile!
    for /F "skip=12 tokens=2-6" %%a in (C:\windows\temp\diskpart_listdisks_output.txt) do (
        echo     %%a %%b %%c %%d %%e


but this does not

Code: Select all

setlocal EnableDelayedExpansion
set tempfile="c:\windows\temp\diskpart_create_tmp.txt"
set listfile="C:\windows\temp\diskpark_listdisks_tmp.txt"
set otherfile="C:\windows\temp\diskpart_listdisks_output.txt"
echo off
ECHO rescan>!listfile!
ECHO list disk >>!listfile!
diskpart /s !listfile! > !otherfile!
    for /F "skip=12 tokens=2-6" %%a in (!otherfile!) do (
        echo     %%a %%b %%c %%d %%e


Anyone have any idea what im doing wrong?

Thanks in Advance
R

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

Re: cannot use variable as path for for loop

#2 Post by dbenham » 18 Jan 2017 05:20

Sure - your definition of otherfile includes enclosing quotes. FOR /F sees the quotes and treats the value as a string instead of a file path.

The solution is to add the "usebackq" option so that a quoted string is still treated as a file path.

Code: Select all

    for /F "usebackq skip=12 tokens=2-6" %%a in (!otherfile!) do (


Note - I generally don't like to include enclosing quotes in my variable values. They tend to cause problems. I find it easier to define the variable without quotes, and then add quotes when I expand, as needed. I enclose the entire SET assignment expression within quotes, and then the quotes are not included in the value.

For example:

Code: Select all

setlocal EnableDelayedExpansion
set "tempfile=c:\windows\temp\diskpart_create_tmp.txt"
set "listfile=C:\windows\temp\diskpark_listdisks_tmp.txt"
set "otherfile=C:\windows\temp\diskpart_listdisks_output.txt"
echo off
ECHO rescan>"!listfile!"
ECHO list disk >>"!listfile!"
diskpart /s !listfile! > "!otherfile!"
    for /F "usebackq skip=12 tokens=2-6" %%a in ("!otherfile!") do (
        echo     %%a %%b %%c %%d %%e


If the sole purpose of creating otherfile is to process the content with FOR /F, then there is a simpler way that avoids creation of the file. Simply process the output of the command directly using FOR /F by enclosing the command within single quotes:

Code: Select all

setlocal EnableDelayedExpansion
set tempfile="c:\windows\temp\diskpart_create_tmp.txt"
set listfile="C:\windows\temp\diskpark_listdisks_tmp.txt"
echo off
ECHO rescan>!listfile!
ECHO list disk >>!listfile!
diskpart /s !listfile! > !otherfile!
    for /F "skip=12 tokens=2-6" %%a in ('diskpart /s !listfile!') do (
        echo     %%a %%b %%c %%d %%e


Dave Benham

rusty_uk
Posts: 5
Joined: 18 Jan 2017 04:00

Re: cannot use variable as path for for loop

#3 Post by rusty_uk » 18 Jan 2017 05:26

Dave

Thanks for the prompt reply. Works a charm now.

Cheers

R

Post Reply