Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

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.

share|improve this question

4 Answers 4

up vote 1 down vote accepted
@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 ("%%F") 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"

share|improve this answer
    
This is exactly what I need. Thanks!! – yuchien Dec 24 '12 at 6:39
    
still a minor problem: it swap ".jpg" too. Ex. part1_part2.jpg => part2.jpg_part1 Orz – yuchien Dec 24 '12 at 6:45
    
@raphael0914 - ??? It works perfectly for me: part1_part2.jpg --> part2_part1.jpg – dbenham Dec 24 '12 at 9:41

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

share|improve this answer
1  
just to swap the content between '_' Do you understand what the question is? – jadkik94 Dec 23 '12 at 18:02
    
I know I should use ren to rename the file, but my problem is I don't know how to do those string manipulations Orz – yuchien Dec 23 '12 at 18:02
    
Not really :/ Please use a good explanation of swap the content? – Itsproinc Dec 23 '12 at 18:05
    
There's a pattern that goes like PART1_PART2.EXT and OP wants to rename all files in a certain directory to PART2_PART1.EXT. I guess :P – jadkik94 Dec 23 '12 at 18:07
    
You should fix the title and the questions.... youre making people confusing... – Itsproinc Dec 23 '12 at 18:07

as Itsproinc said, the REN command works!

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

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 it helps

share|improve this answer

I am assuming you know the length of the part before the _ and after the underscore, as well as the extension. If you don't it might be more complex 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 did not test it. Check out this and that for more info.

IF you want to assume you don't know the positions of the _ and the lengths and the extension, I think you could do something with for loops to check the index of the _, then the last index of the ., wrap it in a goto thing and make it work. If you're willing to go through that trouble, I'd suggest you use WindowsPowerShell (or Cygwin) at least (for your own sake) or install a more advanced scripting language (think Python/Perl) you'll get more support either way.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.