Batch renaming issue

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
lyronva
Posts: 2
Joined: 07 Feb 2015 11:20

Batch renaming issue

#1 Post by lyronva » 07 Feb 2015 11:27

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

[newbie]
Posts: 6
Joined: 07 Feb 2015 15:02

Re: Batch renaming issue

#2 Post by [newbie] » 07 Feb 2015 15:17

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

Squashman
Expert
Posts: 4198
Joined: 23 Dec 2011 13:59

Re: Batch renaming issue

#3 Post by Squashman » 07 Feb 2015 16:25

Code: Select all

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

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: Batch renaming issue

#4 Post by foxidrive » 07 Feb 2015 21:22

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

dbenham
Expert
Posts: 2397
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Batch renaming issue

#5 Post by dbenham » 08 Feb 2015 00:10

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

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: Batch renaming issue

#6 Post by foxidrive » 08 Feb 2015 08:39

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.

Dos_Probie
Posts: 233
Joined: 21 Nov 2010 08:07
Location: At My Computer

Re: Batch renaming issue

#7 Post by Dos_Probie » 08 Feb 2015 09:45

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

lyronva
Posts: 2
Joined: 07 Feb 2015 11:20

Re: Batch renaming issue

#8 Post by lyronva » 08 Feb 2015 09:47

Thanks very much to everyone for the various approaches - they all have their advantages for certain situations -

B

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: Batch renaming issue

#9 Post by foxidrive » 08 Feb 2015 10:07

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. :)

Post Reply