| 1. |
Solve : RENAME FILES IN ONE DIRECTORY USING A BATCH FILE? |
|
Answer» A bit of a differnet twist to my filename rename issue in a batch file. Not sure why the variable was not being adjusted.. Because %filenum% should have been !filenum! I suspect we're getting close..however the result is not what I had targeted... The updated file names all have the same variable value.. e.g. filex-1, file-y-1, filez-1 I'm trying to get ---- filex-1, filey-2, filez-3. My fault in not being able to articulate clearly what I wanted... The for loop now is coded as: for %%A in (*.txt) do ( @echo on set /A filenum=%filenum%+1 set oldname=%%A set newname=%%~nA-!filenum!.txt rem echo "oldname" rem echo "newname" ren "!oldname!" "!newname!" rem pause )You only changed the second %filenum% when you should have changed both. Terrific !!!! I have exactly what I want... thanks fo much !!! One of the issues I have with some of the code is that I don't fully understand how some of the sublties really work... e.g. the significance of the !variable! VERSUS %variable% within the SCOPE of the FOR statement.. I assume !variable! indicates that the new value of the variable is to be used each time through the loop as opposed to the %variable% which appears to use only the initial value of the variable set during the first loop ... again, just guessing... I've attempted to find some information on this and haven't had much luck...the HELP of the FOR statement does not explain this well....The topic you need to learn about is "delayed expansion". There is a ton of stuff you can find out by Googling, explained much better than I can, However here is a brief run through: When a batch file containing only % sign variables is run, the first thing that HAPPENS is that ALL of the variables are "expanded" into their known values. Then the code is executed. Any variables which are SET before a parenthetical expression such as a FOR loop or an extended IF statement, keep the value they had before, and those which are SET inside the expression are blank, as you have noticed. However, with Windows 2000 came delayed expansion. You enable it with setlocal enabledelayedexpansion and you use variables with ! instead of %. Thanks for the tip... I'll follow up....and do some reading... |
|