Renameing files

D

Dwight Johnson

I've got some file that are named "blah1(foo).txt" "blah2(foo).txt" etc..
how do I rename them to just "blah.txt"
It's a bunch o' files.
 
M

Matthias Tacke

Dwight said:
I've got some file that are named "blah1(foo).txt" "blah2(foo).txt" etc..
how do I rename them to just "blah.txt"
It's a bunch o' files.

Parsing is easy with a for loop.
Do you want to get of the parenthes *and* the number?

::StripPar.cmd::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off&setlocal
for /F "delims=" %%A in ('dir *.txt /B') do (
for /F "tokens=1 delims=()0123456789" %%B in ("%%~nA") do (
if not exist "%%B%%~xA" (
ren "%%~fA" "%%B%%~xA"
) else (
echo Can't rename "%%~fA" to "%%B%%~xA", already present.
)
)
)
::StripPar.cmd::::::::::::::::::::::::::::::::::::::::::::::::::::::::

If it are only the parenthes, then drop the numbers in the second for.
The first for iterates all through the files, the second splits at any
of the delims, so only the first element is storred in %%B.

==screen=copy========================================================
C:\test\1>..\strippar.cmd
Can't rename "C:\test\1\blah2(foo).txt" to "blah.txt", already present.

C:\test\1>dir blah*.txt
Verzeichnis von C:\test\1

30.10.2004 23:12 2 blah.txt
30.10.2004 23:12 2 blah2(foo).txt
==screen=copy========================================================

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top