Page 1 of 1

Batch renaming issue

Posted: 07 Feb 2015 11:27
by lyronva
This seems like it would be relatively simple but I haven't made it work yet - sorry if it's an easy fix for you guys, I only occasionally use DOS these days.

See description of problem below - I have a large number of files where I would like to rename by removing the character string "Chapter " so the only thing that remains is the person's name and the number. Is there a way to do this in MSDOS in a batch file, or just from the cmd prompt? Or Windows even?

Before:
Gerald-Chapter 1.doc
Josephine-Chapter 2.doc
Phyllis-Chapter 3.doc
etc.

After:
Gerald-1.doc
Josephine-2.doc
Phyllis-3.doc
etc.

This would be trivial if the "Chapter " string were at the beginning of the filename, but I'm having trouble wildcarding this.

Thanks in advance -

B

Re: Batch renaming issue

Posted: 07 Feb 2015 15:17
by [newbie]
hi,
put the batch file in same directory as your *.docs

Code: Select all

@echo off & setlocal enabledelayedexpansion & title %~n0
cls

:: same directory as the file type
set "Directory=%~dp0"
:: file type
set "Type=doc"

pushd "%Directory%" || goto :eof

for /f "delims=" %%i in ('dir /b /a-d "*-Chapter *.%Type%"') do (
    set "FileName=%%~ni"
   
    echo.
    echo. FileName: !FileName!
   
    REM old file backup
    echo !FileName!%%~xi>>%~n0_backup.txt

    REM remove characters
    set NewFileName=!FileName:Chapter =!

    REM output and rename old in new
    echo. NewFileName: !NewFileName!.%Type%
    echo ren "%%i" !NewFileName!.%Type%
    echo._______________________________
)
pause 

if it does, remove the echo in front ren

Re: Batch renaming issue

Posted: 07 Feb 2015 16:25
by Squashman

Code: Select all

@echo off
Setlocal enabledelayedexpansion
for %%G in (*chapter*.doc) do (
Set newname=%%G
Set newname=!newname:chapter =!
Rename "%%~G" "!newname!"
)

Re: Batch renaming issue

Posted: 07 Feb 2015 21:22
by foxidrive
The guys look to have solved your issue, I just want to add that a file manager tool
called Total Commander has a multi-rename tool which is so useful for such things that it is worth installing it just to use that part.

It can process files over an entire tree all at once as well as only a single file or all marked files, and is uncrippled shareware.

It has an undo feature, you can add regular expressions, changing character case, adding an incrementing counter in all sort of formats, adding directory names. The list goes on.

The window shows you the preview of how the files will be renamed before you commit the changes, which is helpful also.

Here is an example of how it looks and I simply changed House to Mansion with servants

Image

Re: Batch renaming issue

Posted: 08 Feb 2015 00:10
by dbenham
You could use my JREN.BAT utility that renames via regular expression search and replace. It is pure script that runs on any Windows machine from XP onward.

Code: Select all

jren "-Chapter (\d+\.doc)$" "-$1"


Dave Benham

Re: Batch renaming issue

Posted: 08 Feb 2015 08:39
by foxidrive
dbenham wrote:You could use my JREN.BAT utility that renames via regular expression search and replace. It is pure script that runs on any Windows machine from XP onward.

Code: Select all

jren "-Chapter (\d+\.doc)$" "-$1"



Something that would really be a useful addition is to write an UNDO batch file at the same time - coz so much can go wrong with renaming.

Maybe even a series of a timestamped undo files - for those who rename in stages, and you don't always see the errors in batch renaming immediately.

Re: Batch renaming issue

Posted: 08 Feb 2015 09:45
by Dos_Probie
Thanks for the info on your Jren utility, you wouldn't happen to have a download of the file would you?

~DP

dbenham wrote:You could use my JREN.BAT utility that renames via regular expression search and replace. It is pure script that runs on any Windows machine from XP onward.

Code: Select all

jren "-Chapter (\d+\.doc)$" "-$1"


Dave Benham

Re: Batch renaming issue

Posted: 08 Feb 2015 09:47
by lyronva
Thanks very much to everyone for the various approaches - they all have their advantages for certain situations -

B

Re: Batch renaming issue

Posted: 08 Feb 2015 10:07
by foxidrive
Dos_Probie wrote:Thanks for the info on your Jren utility, you wouldn't happen to have a download of the file would you?


It's a batch script at the link that Dave provided. Copy and paste. :)