Rename a Windows batch file

I have a file, for example AAA_a001.jpg , BBB_a002.jpg , CCC_a003.jpg on Windows 7 and I'm trying to use the package to rename these files to a001_AAA.jpg , a002_BBB.jpg , a003_CCC.jpg .

Just to swap the contents between _ .

I searched for a while, but still don't know how to do it. Can anyone help? Thanks.

+6
source share
5 answers
 @echo off pushd "pathToYourFolder" || exit /b for /f "eol=: delims=" %%F in ('dir /b /ad *_*.jpg') do ( for /f "tokens=1* eol=_ delims=_" %%A in ("%%~nF") do ren "%%F" "%%~nB_%%A%%~xF" ) popd 

Note. The name is split into the first occurrence of _ . If the file is called "part1_part2_part3.jpg", it will be renamed to "part2_part3_part1.jpg"

+6
source

Use the REN command

REN for rename

 ren ( where the file is located ) ( the new name ) 

Example

 ren C:\Users\&username%\Desktop\aaa.txt bbb.txt 

he will change aaa.txt to bbb.txt

Your code will be:

 ren (file located)AAA_a001.jpg a001.AAA.jpg ren (file located)BBB_a002.jpg a002.BBB.jpg ren (file located)CCC_a003.jpg a003.CCC.jpg 

etc.

 IT WILL NOT WORK IF THERE IS SPACES! 

Hope this helps: D

+24
source

as Itsproinc said, the REN team works!

but if your path / file name has spaces, use quotation marks ""

Example:

 ren C:\Users\&username%\Desktop\my file.txt not my file.txt 

add ""

 ren "C:\Users\&username%\Desktop\my file.txt" "not my file.txt" 

hope this helps

+5
source

I assume that you know the length of the part before _ and after the underline, as well as the extension. If you do not, it can be more difficult than a simple substring.

 cd C:\path\to\the\files for /f %%a IN ('dir /b *.jpg') do ( set p=%a:~0,3% set q=%a:~4,4% set b=%p_%q.jpg ren %a %b ) 

I just came up with this script and I have not tested it. See this and for more details.

IF you want to assume that you do not know the position of _ , as well as the length and extension, I think you could do something for loops to check the index _ , and then the last index . wrap it in goto and make it work. If you want to get through this problem, I would suggest you use WindowsPowerShell (or Cygwin) at least (for you) or install a more advanced scripting language (think Python / Perl), you will get more support either way.

+1
source

I will rename the code

 echo off setlocal EnableDelayedExpansion for %%a in (*.txt) do ( REM echo %%a set x=%%a set mes=!x:~17,3! if !mes!==JAN ( set mes=01 ) if !mes!==ENE ( set mes=01 ) if !mes!==FEB ( set mes=02 ) if !mes!==MAR ( set mes=03 ) if !mes!==APR ( set mes=04 ) if !mes!==MAY ( set mes=05 ) if !mes!==JUN ( set mes=06 ) if !mes!==JUL ( set mes=07 ) if !mes!==AUG ( set mes=08 ) if !mes!==SEP ( set mes=09 ) if !mes!==OCT ( set mes=10 ) if !mes!==NOV ( set mes=11 ) if !mes!==DEC ( set mes=12 ) ren %%a !x:~20,4!!mes!!x:~15,2!.txt echo !x:~20,4!!mes!!x:~15,2!.txt ) 
0
source

All Articles