I have a bunch of directories (folders, if you like) that follow this pattern
\20121022 Description of the directory's contents goes here\
(some don't have it, just the date)
and I need to rename them to follow the following pattern
\2012-10-22 Description of the directory's contents ...\
Is there a way to do it using Windows cmd and the tools that come with it (namely, ren)? If not, what would be the most portable way to do it? I have a very restricted set of privileges on the machine I'm doing this on.
Answer
If you start with the directory name in a variable, like fdir:
set "fdir=20121022 Description of this directory"
Then your batch file can split up the directory name:
set "fdiryear=%fdir:~0,4%"
set "fdirmonth=%fdir:~4,2%"
set "fdirday=%fdir:~6,2%"
set "fdirdesc=%fdir:~8%"
at this point, the variables look like:
fdir=20121022 Description of this directory
fdiryear=2012
fdirmonth=10
fdirday=22
fdirdesc= Description of this directory
Then define the new directory name, and rename the directory:
set "fnew=%fdiryear%-%fdirmonth%-%fdirday%%fdirdesc%"
ren "%fdir%" "%fnew%"
the new directory name will be:
fnew=2012-10-22 Description of this directory
If you can give me some more information about how you want to collect the list of directory names, I can provide a more complete script.
For example, if all the directories are in the current directory, then the batch file would look like this:
@echo off
echo.
for /D %%f in ("2010*", "2011*", "2012*") do call :work "%%~f"
echo.
set "fdir="
set "fdiryear="
set "fdirmonth="
set "fdirday="
set "fdirdesc="
goto :EOF
:work
set fdir=%~1
set "fdiryear=%fdir:~0,4%"
set "fdirmonth=%fdir:~4,2%"
set "fdirday=%fdir:~6,2%"
set "fdirdesc=%fdir:~8%"
set "fnew=%fdiryear%-%fdirmonth%-%fdirday%%fdirdesc%"
echo Renaming folder "%fdir%" to "%fnew%"
rem ren "%fdir%" "%fnew%"
goto :EOF
Explanations:
This will turn off the echoing of the commands as they are executed to avoid a lot of clutter on the screen:
@echo off
This will display a blank line:
echo.
The for command:
for /D %%f in ("2010*", "2011*", "2012*") do call :work "%%~f"
/D means to search for matches of Directory names only.
If you omit the /D, it will search for matches of Filenames only.
%%f (%%~f) is a variable name that gets set to the names of the matched directories.
[in ("2010*", "2011*", "2012*")] is a list of patterns to search for.
In this case, it will only process directories that begin with a year:
2010, or 2011, or 2012. You can edit this list for your needs. If you
know that all directories in the current folder are in the same format
and are candidates to be renamed, the list can be simply: ("*") like:
for /D %%f in ("*") do call :work "%%~f"
the word "do" simply precedes the command that you want to "do" for each
match found.
call :work says to execute the commands at the label :work for each match.
"%%~f" says to pass the matched directory name as the first argument to the
commands at the label you specified (:work)
The "~" in "%%~f" says to remove "quote" marks from %%f to avoid passing the
directory name inside of 2 sets of "quote" marks. For example, if %%f contained
"20121022 Description of this directory" then using "%%f" would pass
""20121022 Description of this directory"", which would fail. "%%~f" will pass
exactly 1 set of "quotes".
After all the matching directory names are processed, execution returns to the "echo." command that follows the "for ..." command.
Next is just some "cleanup" to clear the variables used in the script so they don't accumulate and clutter up the variable space (environment).
set "fdir="
set "fdiryear="
set "fdirmonth="
set "fdirday="
set "fdirdesc="
The next statement exits the batch script and ends execution.
goto :EOF
Next is the label
:work
Labels begin with a colon at the first column followed by a
label name made up of letters and numbers (no spaces). Labels are
the targets of call and goto commands.
Then the command:
set fdir=%~1
Sets the variable named "fdir" to be the directory name that was
passed from the "for" command.
The "~" in %~1 means to use the name only without any surrounding "quote" marks.
The next commands:
set "fdiryear=%fdir:~0,4%"
set "fdirmonth=%fdir:~4,2%"
set "fdirday=%fdir:~6,2%"
set "fdirdesc=%fdir:~8%"
set "fnew=%fdiryear%-%fdirmonth%-%fdirday%%fdirdesc%"
Split up the directory name into the desired pieces, and
define the "new" directory name.
The next command:
echo Renaming folder "%fdir%" to "%fnew%"
Just displays the old and new directory names so you can see the
progress on the screen.
And the last commands:
rem ren "%fdir%" "%fnew%"
goto :EOF
Renames the directory and then "exits" to return from the call.
Note: I made the actual command to rename the directory a comment so you can run the batch file and see what will happen without actually renaming anything. Once you have run it and are confident the correct directories will be properly renamed, you can edit the line to rename the directory and run the batch script again.
To do that, remove the "rem" from the line so:
rem ren "%fdir%" "%fnew%"
Becomes:
ren "%fdir%" "%fnew%"
No comments:
Post a Comment