Rename Folders YEAR-MON to YEAR-NN Via Single Command

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Samir
Posts: 345
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Rename Folders YEAR-MON to YEAR-NN Via Single Command

#1 Post by Samir » 02 Feb 2015 10:27

I know there's a way to do this, but my batch-fu has gotten rusty. :(

I have folders named like 2014-JAN for a YEAR-MON combination. Because these don't sort right, I want to mass rename them to 2014-01 or YEAR-NN.

I know there has to be a way to do this in a single command on the command line using FOR, but can't think of the right logic. Any help?

So far I've thought about iterating through each year using for, then each month using for, but I'm getting stuck on how I would get the right digit for the month associated with the original month name. I'm sure there may be a way to iterate through the months first, and then repeating that for each year, but I run into the same problem in logic of how to associate month names and days.

Any assistance appreciated.

dbenham
Expert
Posts: 2273
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Rename Folders YEAR-MON to YEAR-NN Via Single Command

#2 Post by dbenham » 02 Feb 2015 11:32

Both of these methods are untested, but unless I have made a silly error, they should work :wink:

Pure batch: Doh :roll: Does not work because wildcard not allowed in REN targe name when renaming folders

Code: Select all

@echo off
setlocal enableDelayedExpansion
set m=0
for %%A in (
  JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC
) do for /d %%F in (????-%%A) do (
  set /a m+=1
  set "mm=0!m!"
  ren "%%F" "*-!mm:-2!"
)


Using my JREN.BAT JScript/batch hybrid utility:
But this works :D

Code: Select all

JREN "^(\d{4}-)(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)$" "$1+ts({dt:$2+' 1 2000',fmt:'{mm}'})" /i /j /d


Dave Benham
Last edited by dbenham on 02 Feb 2015 14:26, edited 1 time in total.

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

Re: Rename Folders YEAR-MON to YEAR-NN Via Single Command

#3 Post by Aacini » 02 Feb 2015 11:50

Code: Select all

cmd /V:ON /C "for /F "tokens=1,2 delims=-" %a in ('dir /B /AD') do @(if not defined JAN (set i=100&for %m in (JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC) do @(set /A i+=1&set %m=!i:~1!)))&ECHO ren %a-%b %a-!%b!"


:mrgreen:
Antonio

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

Re: Rename Folders YEAR-MON to YEAR-NN Via Single Command

#4 Post by Samir » 02 Feb 2015 12:00

Aacini wrote:

Code: Select all

cmd /V:ON /C "for /F "tokens=1,2 delims=-" %a in ('dir /B /AD') do @(if not defined JAN (set i=100&for %m in (JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC) do @(set /A i+=1&set %m=!i:~1!)))&ECHO ren %a-%b %a-!%b!"


:mrgreen:
Antonio
Wow, more complicated than I thought it would be. Would you mind stepping me through the logic?

dbenham
Expert
Posts: 2273
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Rename Folders YEAR-MON to YEAR-NN Via Single Command

#5 Post by dbenham » 02 Feb 2015 12:21

Oh - I missed the requirement that this be done as a single command on the command line. :oops:

Very similar to Aacini's solution, except I use the REN wild card rules:

Code: Select all

cmd /v:on /c "set mm=100&for %m in (jan feb mar apr may jun jul aug sep oct nov dec) do @>nul set /a mm+=1&for /d %f in (????-%m) do @echo ren "%f" "*-!mm:~1!""


And here is another option that avoids delayed expansion:

Code: Select all

for %A in (jan:01 feb:02 mar:03 apr:04 may:05 jun:06 jul:07 aug:08 sep:09 oct:10 nov:11 dec:12) do @for /f "delims=: tokens=1,2" %M in ("%A") do @for /d %F in (????-%M) do @echo ren "%F" "*-%N"


Remove the ECHO near the end of either solution to make it functional
FAIL on both counts because rename of folder does not allow wildcards of any kind

Dave Benham
Last edited by dbenham on 02 Feb 2015 14:56, edited 1 time in total.

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

Re: Rename Folders YEAR-MON to YEAR-NN Via Single Command

#6 Post by Samir » 02 Feb 2015 13:59

dbenham wrote:Oh - I missed the requirement that this be done as a single command on the command line. :oops:

Very similar to Aacini's solution, except I use the REN wild card rules:

Code: Select all

cmd /v:on /c "set mm=100&for %m in (jan feb mar apr may jun jul aug sep oct nov dec) do @>nul set /a mm+=1&for /d %f in (????-%m) do @echo ren "%f" "*-!mm:~1!""


And here is another option that avoids delayed expansion:

Code: Select all

for %A in (jan:01 feb:02 mar:03 apr:04 may:05 jun:06 jul:07 aug:08 sep:09 oct:10 nov:11 dec:12) do @for /f "delims=: tokens=1,2" %M in ("%A") do @for /d %F in (????-%M) do @echo ren "%F" "*-%N"


Remove the ECHO near the end of either solution to make it functional


Dave Benham
All good! I love that second one as it seems like I know what's going on. Pretty ingenious to hardcode the month relations and then just use a delimiter to break them apart for the REN. I'm going to try it right now. 8)

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

Re: Rename Folders YEAR-MON to YEAR-NN Via Single Command

#7 Post by Samir » 02 Feb 2015 14:06

dbenham wrote:And here is another option that avoids delayed expansion:

Code: Select all

for %A in (jan:01 feb:02 mar:03 apr:04 may:05 jun:06 jul:07 aug:08 sep:09 oct:10 nov:11 dec:12) do @for /f "delims=: tokens=1,2" %M in ("%A") do @for /d %F in (????-%M) do @echo ren "%F" "*-%N"
I just tried this one and the resulting

Code: Select all

ren "2014-JAN" "*-01"
didn't look right to me. I ran the REN code just by itself and it gave me an error.

Code: Select all

S:\>ren "2014-JAN" "*-01"
The filename, directory name, or volume label syntax is incorrect.
I thought the * in the ren was a problem so I tried this with the same error:

Code: Select all

S:\>ren "2014-JAN" "????-01"
The filename, directory name, or volume label syntax is incorrect.
Any ideas?

dbenham
Expert
Posts: 2273
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Rename Folders YEAR-MON to YEAR-NN Via Single Command

#8 Post by dbenham » 02 Feb 2015 14:53

Ugh :( :oops:
I mis-remembered my own post documenting REN wildcard rules - Neither the source mask nor the target mask can contain wildcards when renaming folders.

So now my solution is nearly identical to Aacini's. I just restructured it a bit and put in some safe-guards to avoid renaming folders that should not be renamed.

Code: Select all

cmd /v:on /c "set "mm=100"&(for %M in (JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC) do @>nul set /a mm+=1&set "_%M=!mm:~1!")&for /f "tokens=1,2 delims=-" %A in ('dir /b /ad ????-???') do @if defined _%B echo ren "%A-%B" "%A-!_%B!""

And here is a solution that hard codes the abbreviation to number translation:

Code: Select all

cmd /v:on /c "(for %V in ("JAN=01" "FEB=02" "MAR=03" "APR=04" "MAY=05" "JUN=06" "JUL=07" "AUG=08" "SEP=09" "OCT=10" "NOV=11" "DEC=12") do @set "_%~V")&set _&for /f "tokens=1,2 delims=-" %A in ('dir /b /ad ????-???') do @if defined _%B echo ren "%A-%B" "%A-!_%B!""


Dave Benham

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

Re: Rename Folders YEAR-MON to YEAR-NN Via Single Command

#9 Post by Samir » 02 Feb 2015 15:39

dbenham wrote:Ugh :( :oops:
I mis-remembered my own post documenting REN wildcard rules - Neither the source mask nor the target mask can contain wildcards when renaming folders.

So now my solution is nearly identical to Aacini's. I just restructured it a bit and put in some safe-guards to avoid renaming folders that should not be renamed.

Code: Select all

cmd /v:on /c "set "mm=100"&(for %M in (JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC) do @>nul set /a mm+=1&set "_%M=!mm:~1!")&for /f "tokens=1,2 delims=-" %A in ('dir /b /ad ????-???') do @if defined _%B echo ren "%A-%B" "%A-!_%B!""

And here is a solution that hard codes the abbreviation to number translation:

Code: Select all

cmd /v:on /c "(for %V in ("JAN=01" "FEB=02" "MAR=03" "APR=04" "MAY=05" "JUN=06" "JUL=07" "AUG=08" "SEP=09" "OCT=10" "NOV=11" "DEC=12") do @set "_%~V")&set _&for /f "tokens=1,2 delims=-" %A in ('dir /b /ad ????-???') do @if defined _%B echo ren "%A-%B" "%A-!_%B!""


Dave Benham
Thank you! You know what's strange? I remember wildcards on REN from previous DOS versions, even on directories. I don't recall which versions, but I remember win95 had it working.

Since yours is basically the same algorithm, mind walking me through it? I'd like to understand all the details as there's a lot of stuff in there I don't ordinarily use. Thank you in advance!

dbenham
Expert
Posts: 2273
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Rename Folders YEAR-MON to YEAR-NN Via Single Command

#10 Post by dbenham » 02 Feb 2015 15:59

It is pretty straight forward.

The CMD /V:ON /C "......" starts a new cmd.exe session with delayed expansion enabled. The enclosing quotes make it so I don't have to worry about escaping the & and > symbols. This only works because none of the quoted values within the command contain poison characters or spaces.

The first FOR loop defines a variable for each month in the form "_MON=nn". The mm variable is initialized to 100 so it is easy to get 0 padded numbers. SET /A increments the value for each month, and I simply take a substring starting at the 2nd character (position 1). The 2nd FOR loop processes the result of the DIR /B command that lists folders that match the template. Each name is parsed into the year and month parts. I verify that the _MON variable is defined, skipping the folder if it is not. The delayed !_%B! expansion translates the MON into the correct 0 padded numeric value as part of the REN.

The second solution hard codes the translations. For example, in the first iteration where %V="JAN=01", the set statement of set "_%~V"
becomes set "_JAN=01". The reset is the same.

Dave Benham

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

Re: Rename Folders YEAR-MON to YEAR-NN Via Single Command

#11 Post by Aacini » 02 Feb 2015 20:56

Another one! :P

Code: Select all

cmd /Q /V:ON /C "set i=1&(for %a in (4 4 -10 -1 6 -1 -3 2 6 -1 -1) do set /A _!i!=%a,i+=1)&set /A i=1,j=104&for /F "tokens=1,2 delims=-" %a in ('dir /B /AD 2014-???') do ECHO ren %a-%b %a-!j:~1!&set /A j+=_!i!,i+=1"

This method requires that all 12 folders exists, from JAN to DEC.

Antonio

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

Re: Rename Folders YEAR-MON to YEAR-NN Via Single Command

#12 Post by Samir » 03 Feb 2015 06:07

dbenham wrote:It is pretty straight forward.

The CMD /V:ON /C "......" starts a new cmd.exe session with delayed expansion enabled. The enclosing quotes make it so I don't have to worry about escaping the & and > symbols. This only works because none of the quoted values within the command contain poison characters or spaces.

The first FOR loop defines a variable for each month in the form "_MON=nn". The mm variable is initialized to 100 so it is easy to get 0 padded numbers. SET /A increments the value for each month, and I simply take a substring starting at the 2nd character (position 1). The 2nd FOR loop processes the result of the DIR /B command that lists folders that match the template. Each name is parsed into the year and month parts. I verify that the _MON variable is defined, skipping the folder if it is not. The delayed !_%B! expansion translates the MON into the correct 0 padded numeric value as part of the REN.

The second solution hard codes the translations. For example, in the first iteration where %V="JAN=01", the set statement of set "_%~V"
becomes set "_JAN=01". The reset is the same.

Dave Benham
Wow, those a much, much more sophisticated methods than I would have thought of. I've learned a few things. Thank you!
Aacini wrote:Another one! :P

Code: Select all

cmd /Q /V:ON /C "set i=1&(for %a in (4 4 -10 -1 6 -1 -3 2 6 -1 -1) do set /A _!i!=%a,i+=1)&set /A i=1,j=104&for /F "tokens=1,2 delims=-" %a in ('dir /B /AD 2014-???') do ECHO ren %a-%b %a-!j:~1!&set /A j+=_!i!,i+=1"

This method requires that all 12 folders exists, from JAN to DEC.

Antonio
Thank you! Would you mind stepping through it like Dave did? (if the algorithm is different, which it does look quite different :shock:)

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

Re: Rename Folders YEAR-MON to YEAR-NN Via Single Command

#13 Post by foxidrive » 03 Feb 2015 06:23

Samir wrote:I know there has to be a way to do this in a single command on the command line using FOR, but can't think of the right logic.

Was there an over-riding reason to do this in one line? Just curious.

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

Re: Rename Folders YEAR-MON to YEAR-NN Via Single Command

#14 Post by Samir » 03 Feb 2015 07:43

foxidrive wrote:
Samir wrote:I know there has to be a way to do this in a single command on the command line using FOR, but can't think of the right logic.

Was there an over-riding reason to do this in one line? Just curious.
To be honest, I didn't think it would be this complex and be more on the order of the rename xx to xx-1 solution I asked for a while back.

I generally like a one line command that does the job so I don't have to create a batch file and manage it. Especially when it is going to be a one-time use like this.

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

Re: Rename Folders YEAR-MON to YEAR-NN Via Single Command

#15 Post by Squashman » 03 Feb 2015 07:48

Samir wrote:I generally like a one line command that does the job so I don't have to create a batch file and manage it. Especially when it is going to be a one-time use like this.

Pretty much overkill for a one time use. I would have just manually renamed 12 folders. If you tell me that you are just always going to copy and paste that from some help manual you have created then you might as well save it as a batch file and run it.

If you save the batch file and always run it as a batch file you will always have the code to look back on for future use and it may help you write other similar batch files with the concepts that are within that batch file.

Post Reply