windows batch file rename
Asked Answered
L

9

35

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.

Logjam answered 23/12, 2012 at 17:42 Comment(0)
A
12
@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"

Amata answered 23/12, 2012 at 18:49 Comment(2)
still a minor problem: it swap ".jpg" too. Ex. part1_part2.jpg => part2.jpg_part1 OrzLogjam
@raphael0914 - ??? It works perfectly for me: part1_part2.jpg --> part2_part1.jpgAmata
C
57

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

Carl answered 23/12, 2012 at 17:57 Comment(12)
just to swap the content between '_' Do you understand what the question is?Landscape
I know I should use ren to rename the file, but my problem is I don't know how to do those string manipulations OrzLogjam
Not really :/ Please use a good explanation of swap the content?Carl
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 :PLandscape
I want to swap the content between '_', Ex: AAA_BBB.txt => BBB_AAA.txtLogjam
sorry for the confusing =PLogjam
I still dont understand... well at least i helped .. :(Carl
So thats mean you swapping the contect of File a to file b? is that the explanation?Carl
@Carl OP is trying to do exactly what you are doing but without having to write it for every filename in the folder he's on.Landscape
=="? and what is op i know op is operator ( in minecraft )Carl
@Carl Come on, stay! OP means original poster. Wait here a bit and you'll see a good answer coming, and you'll benefit as well.Landscape
If you try to run this in batch script you get the error The syntax of the command is incorrect.Amido
A
12
@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"

Amata answered 23/12, 2012 at 18:49 Comment(2)
still a minor problem: it swap ".jpg" too. Ex. part1_part2.jpg => part2.jpg_part1 OrzLogjam
@raphael0914 - ??? It works perfectly for me: part1_part2.jpg --> part2_part1.jpgAmata
A
12

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

Armful answered 9/11, 2013 at 6:39 Comment(1)
Note to others ... this only works with backslashes and will throw an error with forward slashes "cannot find file"Futures
L
1

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.

Landscape answered 23/12, 2012 at 18:50 Comment(0)
S
0

I rename in 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 

)
Scrivenor answered 21/3, 2017 at 23:24 Comment(0)
B
0

The PowerShell way works for me and easier to understand and change. However, got errors like rename-item : Source and destination path must be different until I changed it a bit. To rename files of the folder only:

dir | Where-Object {$.Name -match 'replaceMe'} | Rename-Item -NewName { $.Name -replace 'replaceMe','myNewText' }

To rename files of the folder and sub folders recursively:

Get-ChildItem -Recurse | Where-Object {$.Name -match '.Core.'} | Rename-Item -NewName { $.Name -replace '.Core.','.Framework.' }

Burford answered 7/6, 2021 at 9:24 Comment(0)
R
0

You can try this one code:

echo off
cd D:\TEMP
for %%A IN (*.jpg) do (
 call :dothings "%%A"
)
cd D:\2
goto :eof

:dothings
set A=%1
set P=%A:~1,12%
set Q=%A:~14,4%
set B="%Q% %P%.jpg"
ren %a% %b%
goto :eof

it will rename for example "Untitled-227 1965.jpg" to "1965 Untitled-227.jpg"

Rhoda answered 21/8, 2021 at 8:42 Comment(0)
A
0

You can do this with the free online tool https://batch-edit.com. Insert the "Rename File" operator between the read and write steps. Make sure to enable the "Use Regex" option. For the pattern, insert (?'part1'.*)_(?'part2'.*) to capture the first and the second part of the file name. Use ${part2}_${part1} as the replacement to switch the first with the second part of the file name.

Disclaimer: I'm the author of BatchEdit.

Anthropologist answered 11/5, 2024 at 21:16 Comment(0)
G
-1

I found this solution via PowerShell :

dir | rename-item -NewName {$_.name -replace "replaceME","MyNewTxt"}

This will rename parts of all the files in the current folder.

Gonad answered 22/5, 2020 at 14:47 Comment(1)
there is something strange going on here, since this mangled the file names. Perhaps the -replace param takes in regex or somethingIgnatzia

© 2022 - 2025 — McMap. All rights reserved.