Rename xx to xx-1 Via Single Command Line
Moderator: DosItHelp
Rename xx to xx-1 Via Single Command Line
I know I've done this a hundred times using for, but I can't remember exactly how I did it. I can make the variable series of 01 to 10, but then decrementing that so the resulting command is 'ren 10 09' is what I don't recall.
Any help appreciated.
Any help appreciated.
Re: Rename xx to xx-1 Via Single Command Line
This produces a lot of errors, but does the job:
This is able to rename directories 10 to 29 to 9 to 28.
Code: Select all
FOR /L %F IN (10, 1, 29) DO FOR /L %G IN (9,1,28) DO REN %F %G
This is able to rename directories 10 to 29 to 9 to 28.
Re: Rename xx to xx-1 Via Single Command Line
Try something like that:
Adapt it and remove ECHO and PAUSE.
Regards
aGerman
Code: Select all
@echo off &setlocal EnableDelayedExpansion
for /l %%i in (101 1 110) do (set /a "n1=%%i, n2=%%i-1" & ECHO ren !n1:~-2! !n2:~-2!)
PAUSE
Adapt it and remove ECHO and PAUSE.
Regards
aGerman
Re: Rename xx to xx-1 Via Single Command Line
Thank you! So without the echo and pause, would this execute directly from the command line? Or would something need to be set in the environment table for the delayed expansion?aGerman wrote:Try something like that:Code: Select all
@echo off &setlocal EnableDelayedExpansion
for /l %%i in (101 1 110) do (set /a "n1=%%i, n2=%%i-1" & ECHO ren !n1:~-2! !n2:~-2!)
PAUSE
Adapt it and remove ECHO and PAUSE.
Regards
aGerman
Re: Rename xx to xx-1 Via Single Command Line
Not sure why you would want to type this at the cmd line but go nuts.
Code: Select all
cmd /v:on /C "for /l %i in (101 1 110) do (set /a "n1=%i, n2=%i-1" & ECHO ren !n1:~-2! !n2:~-2!)"
Re: Rename xx to xx-1 Via Single Command Line
This command-line rename directories 10 to 29 into 9 to 28:
Just be sure to enter this command before:
Antonio
[off-topic]: Why "Squashman" is green?
Code: Select all
for /L %i in (9,1,29) do @(if defined j call ECHO ren %i %j%) ^& set j=%i
Just be sure to enter this command before:
Code: Select all
set "j="
Antonio
[off-topic]: Why "Squashman" is green?

Re: Rename xx to xx-1 Via Single Command Line
Aacini wrote:[off-topic]: Why "Squashman" is green?
Moderators have that option in their user control panel.
Re: Rename xx to xx-1 Via Single Command Line
So I don't have to have a batch file just for the rename.Squashman wrote:Not sure why you would want to type this at the cmd line but go nuts.Code: Select all
cmd /v:on /C "for /l %i in (101 1 110) do (set /a "n1=%i, n2=%i-1" & ECHO ren !n1:~-2! !n2:~-2!)"

Re: Rename xx to xx-1 Via Single Command Line
Samir wrote:So I don't have to have a batch file just for the rename.Squashman wrote:Not sure why you would want to type this at the cmd line but go nuts.Code: Select all
cmd /v:on /C "for /l %i in (101 1 110) do (set /a "n1=%i, n2=%i-1" & ECHO ren !n1:~-2! !n2:~-2!)"
So you are literally going to type this whole line in every time?
Because if you are just going to copy and paste it from a file then it might as well be a .BAT file.
This would be a total waste of time and resources in my world. If something can be automated or done quicker we do it no matter how trivial it is. We work with a limited staff and build full end to end automation for client data processing.
Re: Rename xx to xx-1 Via Single Command Line
Squashman wrote:So you are literally going to type this whole line in every time?
Because if you are just going to copy and paste it from a file then it might as well be a .BAT file.
+1 That's exactly what batch files are good for - repeating tasks exactly the same way every time.
Samir, if you typo the command when typing it in then you have to undo the mess it creates. That's time wasted.
Re: Rename xx to xx-1 Via Single Command Line
Squashman wrote:Samir wrote:So I don't have to have a batch file just for the rename.Squashman wrote:Not sure why you would want to type this at the cmd line but go nuts.Code: Select all
cmd /v:on /C "for /l %i in (101 1 110) do (set /a "n1=%i, n2=%i-1" & ECHO ren !n1:~-2! !n2:~-2!)"
So you are literally going to type this whole line in every time?
Because if you are just going to copy and paste it from a file then it might as well be a .BAT file.
This would be a total waste of time and resources in my world. If something can be automated or done quicker we do it no matter how trivial it is. We work with a limited staff and build full end to end automation for client data processing.
I don't use it that often (maybe once every few years).foxidrive wrote:+1 That's exactly what batch files are good for - repeating tasks exactly the same way every time.
Samir, if you typo the command when typing it in then you have to undo the mess it creates. That's time wasted.
But when I do, I'd rather just use the code directly than to try to figure out what the parameters are to a batch file I created long ago. I always like to look at my commands and understand them unless I specifically made a batch file for a specific task that I don't have to worry about.
