Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

windows batch file rename

Tags:

I have some file such as AAA_a001.jpg, BBB_a002.jpg, CCC_a003.jpg in Windows 7 and I'm trying to use batch to rename these file to a001_AAA.jpg, a002_BBB.jpg, a003_CCC.jpg.

Just to swap the content between _.

I have been searching for a while, but still don't know how to do this. Can anyone help? Thanks.

like image 803
yuchien Avatar asked Dec 23 '12 17:12

yuchien


People also ask

Can you batch rename files in Windows 10?

Right-click on the first file in the folder, then click “Rename.” 3. Type the new name for the file, then press the Tab key on your keyboard. This will simultaneously save the file's new name, then select the following file so you can instantly start typing a new name for that as well.

Can you rename files in CMD?

MS-DOS and Windows command (CMD) line users can change the name of a file or directory using the ren or rename command. Below are examples of how this command can be used. Additional information about each of these commands is found by clicking the command links above.

How do I batch rename files in Windows 11?

Use the File Explorer to navigate to the folder where your files are. Select all the files you want to rename, then right-click them and choose See more options in the context menu. Then, choose PowerRename in the second context menu. You'll now see the PowerRename interface with all your selected files.


2 Answers

Use REN Command

Ren is for rename

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

example

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

it 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 

and so on

IT WILL NOT WORK IF THERE IS SPACES! 

Hope it helps :D

like image 128
Itsproinc Avatar answered Sep 30 '22 07:09

Itsproinc


@echo off pushd "pathToYourFolder" || 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" "%%~nB_%%A%%~xF" ) popd 

Note: The name is split at the first occurrence of _. If a file is named "part1_part2_part3.jpg", then it will be renamed to "part2_part3_part1.jpg"

like image 39
dbenham Avatar answered Sep 30 '22 05:09

dbenham