Computing.Net > Forums > Programming > batch confusion

batch confusion

Reply to Message Icon

Original Message
Name: momchil
Date: August 31, 2007 at 01:59:56 Pacific
Subject: batch confusion
OS: NT or later
CPU/Ram: 2 Ghz
Model/Manufacturer: HP
Comment:

Hi,

i use a batch to get a value from another file

for /F "usebackq delims==" %%i IN ( activename.txt) DO Set line = %%i
echo %line%

here is what I get. any idea why ?

C:\batch\try>Set line = @Fw_Profile_Domain

C:\batch\try>Set line =

C:\batch\try>Set line =

C:\batch\try>echo
ECHO is on.

thank you



Report Offensive Message For Removal

Response Number 1
Name: Shr0Om
Date: August 31, 2007 at 03:11:32 Pacific
Subject: batch confusion
Reply: (edit)

You cannot echo %line% within the FOR loop becuase the variable isn't set until it reaches the end ")".

There are two solutions:
1. Use echo %%i instead of echo %line% in the FOR loop.
2. Use call :label method (which is posted hereunder)


@echo off
for /F "usebackq delims==" %%i IN (activename.txt) do (
set line=%%i
call :show
)
:show
echo %line%


Report Offensive Follow Up For Removal

Response Number 2
Name: Razor2.3
Date: August 31, 2007 at 03:19:36 Pacific
Subject: batch confusion
Reply: (edit)

Well, you took the first step in debugging it yourself. Namely, left ECHO turned ON, so it's not too hard to see what's happening. You have two problems.

1) SET line=
will clear a variable. Since your input has two blank lines, line gets set once, and cleared twice.

2) Spaces are needlessly important in command scripting. As such, you're not using %line%, you're using %line %. (Notice the space?)

A better command would be:
FOR /F "usebackq delims==" %%i IN (activename.txt) DO IF NOT (%%i)==() SET line=%%i


Report Offensive Follow Up For Removal

Response Number 3
Name: Shr0Om
Date: August 31, 2007 at 05:23:18 Pacific
Subject: batch confusion
Reply: (edit)

"becuase the variable isn't set until it reaches the end "
I correct myself here. Still, the solutions posted works.


Report Offensive Follow Up For Removal

Response Number 4
Name: Shr0Om
Date: August 31, 2007 at 06:21:10 Pacific
Subject: batch confusion
Reply: (edit)

I forgot to mention that you also can use delayed expansion:

@echo off
SetLocal EnableDelayedExpansion
for /F "usebackq delims==" %%i IN (activename.txt) do (
set line=%%i
echo !line!
)


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: batch confusion

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software