Rename xx to xx-1 Via Single Command Line

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Rename xx to xx-1 Via Single Command Line

#1 Post by Samir » 16 Mar 2014 09:29

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.

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Rename xx to xx-1 Via Single Command Line

#2 Post by Samir » 16 Mar 2014 09:42

This produces a lot of errors, but does the job:

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.

aGerman
Expert
Posts: 4745
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Rename xx to xx-1 Via Single Command Line

#3 Post by aGerman » 16 Mar 2014 09:44

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

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Rename xx to xx-1 Via Single Command Line

#4 Post by Samir » 27 Mar 2014 08:13

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
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?

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Rename xx to xx-1 Via Single Command Line

#5 Post by Squashman » 27 Mar 2014 08:42

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

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Rename xx to xx-1 Via Single Command Line

#6 Post by Aacini » 27 Mar 2014 10:24

This command-line rename directories 10 to 29 into 9 to 28:

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? :mrgreen:

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Rename xx to xx-1 Via Single Command Line

#7 Post by Squashman » 27 Mar 2014 11:16

Aacini wrote:[off-topic]: Why "Squashman" is green? :mrgreen:

Moderators have that option in their user control panel.

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Rename xx to xx-1 Via Single Command Line

#8 Post by Samir » 19 Jun 2014 17:37

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 I don't have to have a batch file just for the rename. :mrgreen:

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Rename xx to xx-1 Via Single Command Line

#9 Post by Squashman » 19 Jun 2014 18:34

Samir wrote:
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 I don't have to have a batch file just for the rename. :mrgreen:

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Rename xx to xx-1 Via Single Command Line

#10 Post by foxidrive » 19 Jun 2014 20:58

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.

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Rename xx to xx-1 Via Single Command Line

#11 Post by Samir » 08 Jul 2014 11:47

Squashman wrote:
Samir wrote:
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 I don't have to have a batch file just for the rename. :mrgreen:

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.

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.
I don't use it that often (maybe once every few years).

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. 8)

Post Reply