Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
hi all
I want to create a batch file for rename multiple files , or rename only a part of file name for examples:
original:
01-one.txt
02-five.txt
rename in:
one.txt
five.txt
.
I have many files
thanks all

Hi lisa_enk
Are all the files in one directory and are they all numbered the same with the number than an hyphen and the the rest of the name you wish to rename them to.

yes,
all file are un the same directory and they are numbered the same whit progressive number .
And all five have hyphen .
the general five is :xx-filename.txt
when xx is progressive number.
and I want to rename in:
filename.txtother file have this form :
xx-subtitle-filename.txt
and I want to rename in:
filename.txtthanks

Hi Lisa
Note you can only run this once because some of your txt files have two hyphens and the hyphen between %%a-%%b would cause an error on the second run@echo off
dir /b *.txt >files.tmp
for /f "tokens=1* delims=-" %%a in (files.tmp) do ren %%a-%%b %%b >nul

Hi Lisa
I realised you did'nt want the second hyphen so this will work without error.@echo off
dir /b *.txt | find "-" > files.tmp
for /f "tokens=1* delims=-" %%a in (files.tmp) do ren %%a-%%b %%b
dir /b *.txt | find "-" > files.tmp
for /f "tokens=1* delims=-" %%a in (files.tmp) do ren %%a-%%b %%b

thanks dtech10 the 2 batch file work fine
but now I have some problem
whit this file :xx - filename.txt
or
xx - subtitle - filename.txt
I think that is a simple modification of the previous ones, but I'm not be able. :-(thanks also to SN

Hi Lisa
Use this it contains the first batch file code and the extra code to delete filenames containing " - "
@echo off
rem Rename files with extra spaces in then
dir /b *.txt | find " - " > files.tmp
for /f "tokens=1* delims=- " %%a in (files.tmp) do ren "%%a - %%b" "%%b"
dir /b *.txt | find " - " > files.tmp
for /f "tokens=1* delims=- " %%a in (files.tmp) do ren "%%a - %%b" "%%b"rem Rename without extra spaces in them
dir /b *.txt | find "-" > files.tmp
for /f "tokens=1* delims=-" %%a in (files.tmp) do ren %%a-%%b %%b
dir /b *.txt | find "-" > files.tmp
for /f "tokens=1* delims=-" %%a in (files.tmp) do ren %%a-%%b %%brem Tidyup
del files.tmp

Here's a Perl script that does exactly what you asked for.
#!perl -w
while (<*>) {
if (/([^\s-]+)$/) {rename $_, $1;}
}

Actually, we may want to change the regex to be a little more expicit.
if (/^\d+[\s-]+(\w+\.txt)$/) {rename $_, $1;}

While I'm at it, this can be done as 1 single line at the command prompt instead of a script, like this:
perl -e "while(<*>){if (/^\d+[\s-]+(\w+\.txt)$/) {rename $_, $1;}}"

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |