FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%*" '
) DO (
ECHO REN "%sourcedir%\%name%" "%name:~13,10%%name%"
The required REN commands are merely
ECHOed for testing
purposes. After you've verified that the commands are correct, change
ECHO REN to
REN to actually rename the files.
set to point to the
directory of interest. By setting a variable, the directoryname can be
changed in one place rather than editing it in multiple places should it be
used in multiple places. The
set "var=value" syntax ensures
any trailing spaces on the line are not included in the value assigned.
dir command produces a list of filenames in the target
/b means "filenames only - no header/trailer lines,
no date, time size - just the filename"
/a-d means "and no
This list is built in memory, and then read line-by-line by the
"delims=" ensure that the entire line
is applied to the metavariable
%%a so that filenames that
include default separators are not truncated. Each filename is then
assigned to the variable
name because substringing can't be
applied directly to a metavariable. The subroutine
is then executed.
The subroutine is quite simple - all it does is
rename command, using the full filename built from the variables
name. The syntax of the
ren command requires that the new name is name-only, the
subdirectory is implicit. The new name is built from the substring within
name starting at character 13 (the first character is
0) for a length of 10 characters + the original
dir list is built in memory before the
for starts executing, the names delivered to the
for are the original names. If a file has become renamed, it
will not be re-processed as its new name is not in that original list.
The idea of
ren line is so that
the user can see whether the
ren commands generated are
correct. All that happens is that a report is sent to the screen. If it is
then determined that the result appears correct, then changing
ren will actually execute the rename rather than
reporting it. That's a decision to be made by the user. If the resultant
ren is wrong (wrong count of position or length, or wrong
directory or whatever) then no harm is done as the product is simply a
report until the job is run with
echo ren changed to