# Dos SET /P not working as expected



## thesavo (Dec 14, 2008)

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.


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


----------



## TheOutcaste (Aug 8, 2007)

So you have this folder:

```
C:\Documents and Settings\Owner\Desktop\updates\updates\SoftwareDistribution\Download\0034610052cb298a7b8a4f6282e6
```
and want to rename it to:

```
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:

```
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:

```
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 (Dec 14, 2008)

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.


```
@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
```


----------

