Dos SET /P not working as expected

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

thesavo

Thread Starter
Joined
Dec 14, 2008
Messages
58
I am trying to use a work around in a CMD batch file in Windows XP. Since Dos does not support 'back ticks' to inject a command into another one.

Setup. i have a directory of 90 folders that are named using random characters.
Each folder contains a folder called "UPDATE"
Update contains a file named KB#######.CAT

I have a list of all directories in a filenamed 'dirlist.txt'. My FOR /f loop zips through each directory, searches for "KB####.cat "
the goal is to rename each folder to its corresponding KB#####.cat file.

Since I can't use back-quotes like I can in linux to
[pseudo]
rename RandomCharacterFoldername 'Get KB file name.cat'
[/pseudo]

I have to get the name output it to a temp.text file called "var.txt"
then read that into a variable. Then use that variable as the 2nd parameter in the REN command.

Set /p doesn't seem to work.

Code:
for /f  %%G in (dirlist.txt) do (
dir /b %%G\update\*.cat>var.txt
set /p myvar =<var.txt 
ren %%G   %myvar%
set "myvar="
)
 

Attachments

Joined
Aug 7, 2007
Messages
9,028
So you have this folder:
Code:
C:\Documents and Settings\Owner\Desktop\updates\updates\SoftwareDistribution\Download\0034610052cb298a7b8a4f6282e6
and want to rename it to:
Code:
C:\Documents and Settings\Owner\Desktop\updates\updates\SoftwareDistribution\Download\KB012345
Where the KB012345 part matches the .cat file in the update subfolder.
Do you want the folder name to have the .cat extension, or just the KB###### part of the name?

The reason yours isn't working is you need to be using Delayed Expansion, as your file is actually only one line, so %myvar% is replaced with its value when the line is read, so will always be null. Also, the set /P statement is using "myvar " with a trailing space, and the rename command is using a different variable, "myvar" without a trailing space, they are not the same variable.
The value read from the file with the set /P statement won't be seen without using Delayed Expansion.
This should work, though I haven't tested it:
Code:
SetLocal EnableDelayedExpansion
for /f  %%G in (dirlist.txt) do (
dir /b %%G\update\*.cat>var.txt
set /p myvar=<var.txt 
ren %%G !myvar!
)
This might be an easier way, this will rename the folder without the .cat extension, and no need for delayed expansion or a temp file:
Code:
Set _Root=%Userprofile%\Desktop\updates\updates\SoftwareDistribution\Download
PushD %_Root%
For /F "Tokens=* Delims=" %%I In ('Dir /AD /B') Do (
  For /F "Tokens=* Delims=" %%J In ('Dir /A-D /B "%%I\update\*.cat"') Do Ren %%I %%~nJ
)
PopD
If you want the .cat to be part of the folder name, just remove the ~n part and use just %%J
 

thesavo

Thread Starter
Joined
Dec 14, 2008
Messages
58
Thank you for your help. Using "SetLocal enabledelayed expansion" and encapsulating the variable in '!' worked splendidly. .
Also thankyou for reminding meto use the dirlist command in the loop instead of relying on a previous created list file.
The script is more dynamic now. I found a way to drop the .cat extension.

Code:
@Echo off
SetLocal EnableDelayedExpansion
for /f %%G in ('dir /ad /b') do (
dir /b %%G\update\*.cat>var.txt
set /p myvar=<var.txt
set myvar=!myvar:~0,-4!
ren %%G !myvar!)
del var.txt
pause
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Members online

Top