1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Dos SET /P not working as expected

Discussion in 'Software Development' started by thesavo, Jan 9, 2011.

Thread Status:
Not open for further replies.
  1. thesavo

    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="
    )
    
     

    Attached Files:

  2. TheOutcaste

    TheOutcaste

    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
     
  3. thesavo

    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
    
    
     
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 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/973711

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice