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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

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

#16 Post by Samir » 03 Feb 2015 08:17

Squashman wrote:
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.
Like I said, I didn't think it was going to be this sophisticated. Not just 12 folders either, there's a few years worth. Not the biggest amount of time, but I thought it to be a good exercise in batch.

The manual is this forum. I search here for a solution to a problem first. 90% of the time there's a batch that someone else has already written that I can modify for what I need. This was one of the rare occasions when I didn't, and I learned quite a bit about approaching a problem by looking at the solutions.

Never underestimate the power of the collective medium. It outweighs any 'library' of solutions one would have locally.

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

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

#17 Post by Squashman » 03 Feb 2015 08:26

Samir wrote:The manual is this forum. I search here for a solution to a problem first.
Never underestimate the power of the collective medium. It outweighs any 'library' of solutions one would have locally.

What happens when the site owner decides to shut down the site for good without any warning. You just can't rely on the Internet always being in there. I am sure most kids these days feel that way and don't really care about backup. I personally save a copy of every script I find useful on this site. It is categorized into folders and backed up every night.

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

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

#18 Post by Samir » 03 Feb 2015 10:00

Squashman wrote:
Samir wrote:The manual is this forum. I search here for a solution to a problem first.
Never underestimate the power of the collective medium. It outweighs any 'library' of solutions one would have locally.

What happens when the site owner decides to shut down the site for good without any warning. You just can't rely on the Internet always being in there. I am sure most kids these days feel that way and don't really care about backup. I personally save a copy of every script I find useful on this site. It is categorized into folders and backed up every night.
And you bring up a very good point. I've actually had this happen on a car forum where I posted tons of cross-reference research and links--all gone with one database crash. :(

I guess this all comes down to the owner of the site and my faith in them. DosTips isn't a site for the masses or about a subject that you only have a passing interest in. DOS (in its pure form) has been dead now for almost 10 years, and yet this site still garners interest as long as batch file programming is around (which it seems like it still shall be from the Win10 thread).

The day there's no more traffic on this site, I'm going to leech the whole thing and save it locally because I will be worried that it will be gone one day.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

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

#19 Post by Ed Dyreen » 03 Feb 2015 13:57

Samir wrote:DOS (in its pure form) has been dead now for almost 10 years, and yet this site still garners interest as long as batch file programming is around (which it seems like it still shall be from the Win10 thread).
The command line interpreter is an important part of any operating system. An OS without one is just silly.
Samir wrote:The day there's no more traffic on this site, I'm going to leech the whole thing and save it locally because I will be worried that it will be gone one day.
I wouldn't worry about that, when this site is shutdown, alternatives arise. It has always been like that.
If information is lost because one didn't had at least 3 copies, it probably wasn't that important.

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

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

#20 Post by Samir » 03 Feb 2015 14:32

Ed Dyreen wrote:The command line interpreter is an important part of any operating system. An OS without one is just silly.
Here Here! I know the mouse and touchscreen lovers out there may think otherwise, haha.
Samir wrote:The day there's no more traffic on this site, I'm going to leech the whole thing and save it locally because I will be worried that it will be gone one day.
If information is lost because one didn't had at least 3 copies, it probably wasn't that important.[/quote]Or it just wasn't accessed recently. ;)

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

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

#21 Post by foxidrive » 03 Feb 2015 20:04

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.


Your computing tasks will benefit from writing scripts to do tasks rather than using the command line.

The major benefit is that you can use an echo statements and pause to test if what you are doing is correct or not,
because a typo or long untested command line can create so much damage that it would take you weeks to repair it - or make you need to restore from your backup.

You could use echo statements on the command line too - but then the amount of command line editing far exceeds what you would need to put in a batch script - most of the time.

Then there is the added danger in a command line of using the doskey history and arrowing up to the wrong command - after you had fixed it on a different line.

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

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

#22 Post by foxidrive » 03 Feb 2015 20:52

Samir wrote: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.


Here's a way that will give you another script to examine, to ensure that it does what you need - it works over an entire folder tree.

Your question did prompt some inventive solutions and which entertained the guys, from all accounts. :)

I didn't test their solutions - but this particular task is complicated if there are trees of folders because if the top level folder is renamed first then
the rest of the renames in that tree fail, unless certain methods are used to keep rechecking for the changed foldernames.

This method should get around that by using the sort /r

The wildcard used might catch some unintended folders so it's best to review the renfolders.bat.txt file first.

EDIT: I improved the code to eliminate harmless but unneeded renames and made the targeting of the required folders more accurate.
It now uses find.exe so it will be slower....

Code: Select all

@echo off
setlocal enabledelayedexpansion
(
for /d /r %%a in (2???-???) do (
for %%z 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 "foldername=%%~nxa"
for /f "delims==" %%b in ("%%~z") do echo "%%~nxa"|find /i "%%b" >nul && echo @ren "%%a" "!foldername:%%~z!"
)
)
)>~.tmp
sort /r <~.tmp >renfolders.bat.txt
del ~.tmp

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

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

#23 Post by Samir » 04 Feb 2015 06:22

foxidrive wrote:Your computing tasks will benefit from writing scripts to do tasks rather than using the command line.

The major benefit is that you can use an echo statements and pause to test if what you are doing is correct or not,
because a typo or long untested command line can create so much damage that it would take you weeks to repair it - or make you need to restore from your backup.

You could use echo statements on the command line too - but then the amount of command line editing far exceeds what you would need to put in a batch script - most of the time.

Then there is the added danger in a command line of using the doskey history and arrowing up to the wrong command - after you had fixed it on a different line.
For small one-time tasks, I disagree. And using echo statements while developing a one-time command line is usually what I do. There is that danger of doskey (and I know what you mean because it must have happened to both of us at some point), but I'm typically not typing very quickly when thinking about a one-liner, so I've become good at not hitting the up-arrow key.

For this particular task, I didn't realize it required such sophistication, so if I would have made this myself, it probably would have just been a batch file of REN or MOVE commands. I just remembered that MOVE can be used to rename directories. I'll look at Dave's code again using REN and replace it with MOVE to see what happens.

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

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

#24 Post by Samir » 04 Feb 2015 06:26

foxidrive wrote:
Samir wrote: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.


Here's a way that will give you another script to examine, to ensure that it does what you need - it works over an entire folder tree.

Your question did prompt some inventive solutions and which entertained the guys, from all accounts. :)

I didn't test their solutions - but this particular task is complicated if there are trees of folders because if the top level folder is renamed first then
the rest of the renames in that tree fail, unless certain methods are used to keep rechecking for the changed foldernames.

This method should get around that by using the sort /r

The wildcard used might catch some unintended folders so it's best to review the renfolders.bat.txt file first.

EDIT: I improved the code to eliminate harmless but unneeded renames and made the targeting of the required folders more accurate.
It now uses find.exe so it will be slower....

Code: Select all

@echo off
setlocal enabledelayedexpansion
(
for /d /r %%a in (2???-???) do (
for %%z 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 "foldername=%%~nxa"
for /f "delims==" %%b in ("%%~z") do echo "%%~nxa"|find /i "%%b" >nul && echo @ren "%%a" "!foldername:%%~z!"
)
)
)>~.tmp
sort /r <~.tmp >renfolders.bat.txt
del ~.tmp
I only needed it for a single directory, but I'm sure your code will help someone that sees this thread for a similar need. 8)

The only thing that I see that may cause a problem is that the REN command doesn't work on dirs. :( However, MOVE does.

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

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

#25 Post by Samir » 04 Feb 2015 06:34

Strange, even when the REN is changed to MOVE, testing the result of

Code: Select all

move "2014-JAN" "*-01"
didn't work. :(

I also tried:

Code: Select all

move "2014-JAN" "????-01"
move "drive letter:2014-JAN" "????-01"
move "drive letter:\path\2014-JAN" "????-01"
and they all result in the error

Code: Select all

The filename, directory name, or volume label syntax is incorrect.
I checked MOVE /? twice to make sure the syntax was correct. The operation is being done on a locally mapped network drive (which I think may be the issue). Any ideas?

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

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

#26 Post by foxidrive » 04 Feb 2015 10:01

Samir wrote:The only thing that I see that may cause a problem is that the REN command doesn't work on dirs. :( However, MOVE does.


Try it again. Ren works fine.

I read this quote and assumed you had a folder tree :D

Not just 12 folders either, there's a few years worth.

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

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

#27 Post by dbenham » 04 Feb 2015 11:36

REN does not support wildcards in either the source or the target when renaming folders.

MOVE does not support wildcards in the target ever - not for files, not for folders.

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

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

#28 Post by Samir » 04 Feb 2015 12:00

foxidrive wrote:
Samir wrote:The only thing that I see that may cause a problem is that the REN command doesn't work on dirs. :( However, MOVE does.


Try it again. Ren works fine.

I read this quote and assumed you had a folder tree :D

Not just 12 folders either, there's a few years worth.
I didn't initially follow the logic in your solution because I didn't see that it was using a full name (no wildcards) until now. Sorry about that.

All the directories are just in 1 level deep, so there's no tree traversing needed per se.

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

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

#29 Post by Samir » 04 Feb 2015 12:01

dbenham wrote:REN does not support wildcards in either the source or the target when renaming folders.

MOVE does not support wildcards in the target ever - not for files, not for folders.
Gotcha. It sure would be nice if the /? for each command specifically says when wildcards are not allowed.

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

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

#30 Post by foxidrive » 04 Feb 2015 12:13

Samir wrote:All the directories are just in 1 level deep, so there's no tree traversing needed per se.


Yep, ta. Saw that earlier.

My code still works for a bunch of folders that are within a single folder.

Post Reply