Computer/Filename manipulations on Windows using command prompt or a batch file

From Mamin LAB
Jump to navigation Jump to search

Filename manipulations with a batch file on Windows using command prompt[edit | edit source]


Main_Page > Computer > Filename manipulations on Windows using command prompt or a batch file



Swapping a part of the filename(s) between a specific character(s)[edit | edit source]

The files that are named with <first>_<second>.jpg, then these will be changed to <second>_<first>.jpg
(%A will be <first>. %B will be <second>.)

for /f "eol=: delims=" %F in ('dir /b /a-d *_*.jpg') do for /f "tokens=1* eol=_ delims=_" %A in ("%~nF") do ren "%F" "%~nB_%A%~xF"


Adding a string on the filename(s)[edit | edit source]

The files that are named with <first>_<second>.jpg, then these will be changed to <first>_Mamin_<secod>.jpg
(%A will be <first>. %B will be <second>.)

for /f "eol=: delims=" %F in ('dir /b /a-d *_*.jpg') do for /f "tokens=1* eol=_ delims=_" %A in ("%~nF") do ren "%F" "%A_Mamin_%~nB%~xF"


Adding a string on the filename(s) with a batch file[edit | edit source]

The files that are named with <first>_<second>.jpg on the directory "dir01', then these will be changed to <first>_Mamin_<secod>.jpg
(%%A will be <first>. %%B will be <second>.)


Filename: rename.bat

@echo off
pushd "dir01" || exit /b
for /f "eol=: delims=" %%F in ('dir /b /a-d *_*.jpg') do (
  for /f "tokens=1* eol=_ delims=_" %%A in ("%%~nF") do ren "%%F" "%%A_Mamin_%%~nB%%~xF"
)
popd


Deleting a string on the filename(s)[edit | edit source]

The files that are named with <first>_<second>_<third>.jpg, then these filenames will be changed to <first>_<secod>.jpg _<third> will be deleted on each filenames.
(%A will be <first>. %B will be <second>. %C will be <third>. But in this case, %C will be ignored.)

for /f "eol=: delims=" %F in ('dir /b /a-d *_*.jpg') do for /f "tokens=1,2,3 eol=_ delims=_" %A in ("%~nF") do ren "%F" "%A_%B%~xF"


About for on the commpand prompt[edit | edit source]

(You can see the manual using for /? on command prompt. Just type "for /?".)

eol=c          - specifies an end of line comment character
                 (just one)

skip=n         - specifies the number of lines to skip at the
                 beginning of the file.

delims=xxx     - specifies a delimiter set.  This replaces the
                 default delimiter set of space and tab.

tokens=x,y,m-n - specifies which tokens from each line are to
                 be passed to the for body for each iteration.
                 This will cause additional variable names to
                 be allocated.  The m-n form is a range,
                 specifying the mth through the nth tokens.  If
                 the last character in the tokens= string is an
                 asterisk, then an additional variable is
                 allocated and receives the remaining text on
                 the line after the last token parsed.

usebackq       - specifies that the new semantics are in force,
                 where a back quoted string is executed as a
                 command and a single quoted string is a
                 literal string command and allows the use of
                 double quotes to quote file names in
                 file-set.


Table 1
Variable with modifier Description
%~I Expands %I which removes any surrounding
quotation marks ("").
%~fI Expands %I to a fully qualified path name.
%~dI Expands %I to a drive letter only.
%~pI Expands %I to a path only.
%~nI Expands %I to a file name only.
%~xI Expands %I to a file extension only.
%~sI Expands path to contain short names only.
%~aI Expands %I to the file attributes of file.
%~tI Expands %I to the date and time of file.
%~zI Expands %I to the size of file.
%~$PATH:I Searches the directories listed in the PATH environment variable and expands %I to the fully qualified name of the first one found.
If the environment variable name is not defined or the file is not found by the search, this modifier expands to the empty string.