Reputation: 703
Have 2,000 files, each with 51-62 character-long filenames. For each filename, I would like to add the character-stretch in position 14-23 to the very beginning of the filename. (Filename character types and patterns are variable, so the rename must be based on position counting from the filename's beginning.) Windows 7, command prompt FCIV solution preferred.
For example:
original:
abcdefghijklmNo0123456p8901234567890.ext
new:
No0123456pabcdefghijklmNo0123456p8901234567890.ext
Upvotes: 1
Views: 2284
Reputation: 79982
@ECHO OFF
SETLOCAL
SET "sourcedir=c:\folder"
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\*" '
) DO (
SET "name=%%a"
CALL :transform
)
GOTO :EOF
:transform
ECHO REN "%sourcedir%\%name%" "%name:~13,10%%name%"
GOTO :eof
The required REN commands are merely ECHO
ed for testing purposes. After you've verified that the commands are correct, change ECHO REN
to REN
to actually rename the files.
The variable sourcedir
is 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.
The dir
command produces a list of filenames in the target directory /b
means "filenames only - no header/trailer lines, no date, time size - just the filename" /a-d
means "and no directory names"
This list is built in memory, and then read line-by-line by the for/f
. 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 :transform
is then executed.
The subroutine is quite simple - all it does is echo
a rename command, using the full filename built from the variables sourcedir
and 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 character 0
) for a length of 10 characters + the original name.
Since the 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 ECHO
ing the 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 echo ren
to 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 ren
.
Upvotes: 1