Solved Need to cut a part of filename (batch)

April 25, 2012 at 11:30:38
Specs: Windows
I got files named like this:

cam1(word1 word2 wordN) (24-04-2012 00-11-13).mpg
cam2(word1 word2 wordN) (24-04-2012 00-11-13).mpg
cam3(word1 word2 wordN) (24-04-2012 00-11-13).mpg

Need to cut (word1 word2 word3) and replace spaces with _ Renamed files expected:


In the first brackets could be different number of "word". Timestamp is always the same.

See More: Need to cut a part of filename (batch)

Report •

April 26, 2012 at 04:10:49
✔ Best Answer
You list your OS as Windows, I have assumed that you mean one of the Windows NT versions.

Please test the following script. You must provide the path to the input files. If you are satisfied that the script will perform as you wish remove the REM command to rename files.

Good luck

@echo off
setlocal enabledelayedexpansion

for /f "tokens=*" %%1 in ('dir /b /a-d %temp%\*.mpg') do (
    set oldfilename=%%1

        for /f "tokens=1* delims=)" %%1 in ("!oldfilename!") do (
            set first=%%1&set second=%%2

            for /f "tokens=1 delims=(" %%1 in ("!first!") do (
                set lead=%%1
                set newfilename=!lead!!second!
                set newfilename=!newfilename: =_!

REM                REN "%temp%\!oldfilename!" !newfilename!

        echo Oldfilename=!oldfilename!
        echo Newfilename=!newfilename!&echo.

Report •

April 26, 2012 at 09:01:58

Exactly what I needed, thank you.

Where I can read about these things:

: =_!


Report •

April 26, 2012 at 17:58:13
At the command prompt enter any command followed by /? to see the syntax for that command. e.g. Set /? will show the syntax for the Set command.

Report •

Related Solutions

Ask Question