script was working, now it will not

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
df9870
Posts: 1
Joined: 08 Jun 2010 16:13

script was working, now it will not

#1 Post by df9870 » 08 Jun 2010 16:20

I put together a script to take ouy out from a dsquery and create a list of FQDNs.
The script was working, I closed the command window, opened a new one and now the script does not work.
Please help
The script is below

@echo off
for /f %%x in (computers.txt) do (
echo %%x
set mystr=%%x
for /f "useback tokens=*" %%a in ('%mystr%') do set mystr=%%~a
set mystr=%mystr:CN =%
set mystr=%mystr:DC =.%
set mystr=%mystr:Computers=%
set mystr=%mystr: =%
echo %mystr%
)

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: script was working, now it will not

#2 Post by jeb » 09 Jun 2010 08:48

Hi df9870,

The reason is the expansion phase of the variable.
The %var% is only expanded once, at the beginning of the block ().
So you only see the value before you entered the block.

If you want to use the current value use the delayed expansion syntax.

Code: Select all

@echo off
setlocal EnableDelayedExpansion
for /f %%x in (computers.txt) do (
echo %%x
set mystr=%%x
for /f "useback tokens=*" %%a in ('!mystr!') do set mystr=%%~a
set mystr=!mystr:CN =!
set mystr=!mystr:DC =.!
set mystr=!mystr:Computers=!
set mystr=!mystr: =!
echo !mystr!
)


jeb

Post Reply