Rename Folders YEAR-MON to YEAR-NN Via Single Command
Moderator: DosItHelp
Rename Folders YEAR-MON to YEAR-NN Via Single Command
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.
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.
Re: Rename Folders YEAR-MON to YEAR-NN Via Single Command
Both of these methods are untested, but unless I have made a silly error, they should work
Pure batch: Doh Does not work because wildcard not allowed in REN targe name when renaming folders
Using my JREN.BAT JScript/batch hybrid utility:
But this works
Dave Benham
Pure batch: Doh 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
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.
Re: Rename Folders YEAR-MON to YEAR-NN Via Single Command
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!"
Antonio
Re: Rename Folders YEAR-MON to YEAR-NN Via Single Command
Wow, more complicated than I thought it would be. Would you mind stepping me through the logic?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!"
Antonio
Re: Rename Folders YEAR-MON to YEAR-NN Via Single Command
Oh - I missed the requirement that this be done as a single command on the command line.
Very similar to Aacini's solution, except I use the REN wild card rules:
And here is another option that avoids delayed expansion:
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
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.
Re: Rename Folders YEAR-MON to YEAR-NN Via Single Command
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.dbenham wrote:Oh - I missed the requirement that this be done as a single command on the command line.
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
Re: Rename Folders YEAR-MON to YEAR-NN Via Single Command
I just tried this one and the resultingdbenham 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"
Code: Select all
ren "2014-JAN" "*-01"
Code: Select all
S:\>ren "2014-JAN" "*-01"
The filename, directory name, or volume label syntax is incorrect.
Code: Select all
S:\>ren "2014-JAN" "????-01"
The filename, directory name, or volume label syntax is incorrect.
Re: Rename Folders YEAR-MON to YEAR-NN Via Single Command
Ugh
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.
And here is a solution that hard codes the abbreviation to number translation:
Dave Benham
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
Re: Rename Folders YEAR-MON to YEAR-NN Via Single Command
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.dbenham wrote:Ugh
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
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!
Re: Rename Folders YEAR-MON to YEAR-NN Via Single Command
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
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
Re: Rename Folders YEAR-MON to YEAR-NN Via Single Command
Another one!
This method requires that all 12 folders exists, from JAN to DEC.
Antonio
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
Re: Rename Folders YEAR-MON to YEAR-NN Via Single Command
Wow, those a much, much more sophisticated methods than I would have thought of. I've learned a few things. Thank you!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
Thank you! Would you mind stepping through it like Dave did? (if the algorithm is different, which it does look quite different )Aacini wrote:Another one!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
Re: Rename Folders YEAR-MON to YEAR-NN Via Single Command
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.
Re: Rename Folders YEAR-MON to YEAR-NN Via Single Command
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.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.
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.
Re: Rename Folders YEAR-MON to YEAR-NN Via Single Command
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.