rename a single or multiple character in a file name

  • Thread starter Thread starter ACMEuser
  • Start date Start date
A

ACMEuser

Hi all
I have many file in a folder.
the files have dot (.) in the name
I want to remove it.

Example:
complex.old.dat -> complexold.dat
or
outputnew.today.dat -> outputnewtoday.dat

Other files have "-.-" in the name ,
I want to rename it whit "dd".

Example:
complex-.-h.c2.dat ->c omplexddh.c2.dat
other-.-1.old ->otherdd1.old

How to do this whit a bach file?

Thanks
regards



FREENET & MAIL by X-Privat: http://www.x-privat.org/mail.php
 
ACMEuser said:
Hi all
I have many file in a folder.
the files have dot (.) in the name
I want to remove it.

Example:
complex.old.dat -> complexold.dat
or
outputnew.today.dat -> outputnewtoday.dat

Other files have "-.-" in the name ,
I want to rename it whit "dd".

Example:
complex-.-h.c2.dat ->c omplexddh.c2.dat
not complex-.-h.c2.dat ->c omplexddhc2.dat ?

That is inconsistent according to your rule.
other-.-1.old ->otherdd1.old

::RemoveDots.cmd:::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off&setlocal EnableDelayedExpansion
for /F "delims=" %%A in ('dir /B ^|findstr "[^.]*\.[^.]*\..*"') do (
set newName=%%~nA
set newName=!newName:-.-=dd!
set newName=!newName:.=!
echo ren "%%A" "!newName!%%~xA"
)
::RemoveDots.cmd:::::::::::::::::::::::::::::::::::::::::::::::::::::

if output seems ok remove the echo in front of the ren command.

==screencopy=========================================================
C:\test\1>..\RemoveDots.cmd
ren "Name.dat.txt" "Namedat.txt"
ren "test-.-name.txt" "testddname.txt"
==screencopy=========================================================

HTH
 
"Matthias Tacke"
::RemoveDots.cmd:::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off&setlocal EnableDelayedExpansion
for /F "delims=" %%A in ('dir /B ^|findstr "[^.]*\.[^.]*\..*"') do (
set newName=%%~nA
set newName=!newName:-.-=dd!
set newName=!newName:.=!
echo ren "%%A" "!newName!%%~xA"
)
::RemoveDots.cmd:::::::::::::::::::::::::::::::::::::::::::::::::::::

if output seems ok remove the echo in front of the ren command.

==screencopy=========================================================
C:\test\1>..\RemoveDots.cmd
ren "Name.dat.txt" "Namedat.txt"
ren "test-.-name.txt" "testddname.txt"
==screencopy=========================================================

HTH

Thanks, your batch run very well.
It's possible to write any comment
to your bach file so I can study it?

Thanks again.
Regards



FREENET & MAIL by X-Privat: http://www.x-privat.org/mail.php
 
ACMEuser said:
"Matthias Tacke"
::RemoveDots.cmd:::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off&setlocal EnableDelayedExpansion
for /F "delims=" %%A in ('dir /B ^|findstr "[^.]*\.[^.]*\..*"') do (
set newName=%%~nA
set newName=!newName:-.-=dd!
set newName=!newName:.=!
echo ren "%%A" "!newName!%%~xA"
)
::RemoveDots.cmd:::::::::::::::::::::::::::::::::::::::::::::::::::::

Thanks, your batch run very well.
It's possible to write any comment
to your bach file so I can study it?
The for /f parses files or in this case the output of the command
dir /B ^|findstr "[^.]*\.[^.]*\..*"

Dir doesn't handle *.*.* as a selection, so I choose to dir all files,
pipe them to findstr which filters them with a regular expression.
[^.]* represents any number of any characters except a dot.

Since the dot has the special meaning of representing any char it has
to be escaped with a backslash to get the literal dot.
So only file names with at least two dots will pass.

for iterates through all files executing the commands following
do ( ) with the actual file name in the var %%A.

set newName=%%~nA
Stores only the name (stripped drive path and extension from it) in
the var newName.

set newName=!newName:-.-=dd!
Replace "-.-" with "dd" (the ! instead of % is necessary to have
actual values - otherwise the content would be evaluated only when
cmd.exe enters the area between the parenthes)

set newName=!newName:.=!
Same with the dot, replaced with nothing.

echo ren "%%A" "!newName!%%~xA"
rename original %%A with newName and origianl extension %%~xA

For details see:
for /?
set /?
setlocal /?
findstr /?

HTH
 
Back
Top