1.

Solve : Renaming files and deleting strings?

Answer»

I haven't touched batch files in some years so I have become a bit rusty.

Let's say I have a folder of files:
Code: [Select]this.is.the.first.8647.txt
this.is.the.second.1825.txt
this.is.the.third.2750.txt
How would I go about renaming each of these files to:
- Replace all periods with spaces besides the extension
and
- delete the rest of the name starting at the 4 digit NUMBER

So it would end up like:
Code: [Select]this is the first.txt
this is the second.txt
this is the third.txt

Would that be too complex for just batch?Remove the two echos if it PRINTS the right things to the console and then run it for real.

Code: [Select]@echo off
for %%a in (*.txt) do (
for /f "tokens=1-4 delims=." %%b in ("%%a") do (
echo ren "%%a" "%%b %%c %%d %%e.tmp"
)
)
echo ren *.tmp "*%%~XA"
pause
Works great.

Now, would there be a way to detect how many periods there are and do the same?
Kind of like
Code: [Select]this.is.the.first.1234.file.once.txt
this.is.the.second.6126.file.twice.htrss.asd.txt
this.is.the.third.6123.file.thrice.awg.txt
to
Code: [Select]this is the first.txt
this is the second.txt
this is the third.txt
The same code will work for those files too.hmm, how about if the files were like this

Code: [Select]this.is.the.first.waf.hnb.j.1234.file.once.txt
this.is.the.second.afd.n.n.eq.6126.file.twice.htrss.asd.txt
this.is.the.third.6123.file.thrice.awg.txt

Pretty MUCH what I'm trying to say is that I constantly receive lots of files with periods instead of spaces, the periods range from 2 to 12 usually, and anything starting from the 4 digit number is unnecessary for the file I want renamed.

I'm sorry if I'm bugging you with this.So it's more complex than your first post. It always helps to see accurate information.

This works here with your sample files. It uses VBS via WSH.

Code: [Select]@echo off
for /f "delims=" %%a in ('dir *.txt /b /a-d') do (
call :Search_and_replace "%%a"
for /f "delims=" %%b in ('cscript /nologo _.vbs') do ren "%%a" "%%b"
)
del _.vbs
pause

goto :EOF

:Search_and_replace
set s=regex.replace("%~1","$1.txt")
>_.vbs echo set regex=new regexp
>>_.vbs echo regex.global=True
>>_.vbs echo regEx.IgnoreCase=True
>>_.vbs echo regex.pattern="(.*).[0-9][0-9][0-9][0-9].*"
>>_.vbs echo wscript.stdOut.write %s%
Hi All,

Just wondering if someone could help me with renaming multiple files. I have an excel spreadsheet that has two columns, the first column shows the old file name and the second shows the new file name.

What I am wanting to do is to change all the file names from the new name back to the old name... I have approximately 200 and I done this manually last time and it took a long time! As you can probably imagine! Can someone provide me with a few suggestions...

Your help would much appreciated.

BenThis might be a solution for you: http://download.cnet.com/Bulk-Rename-Utility/3000-2248_4-10174242.html#rateitQuote from: BenAshby22 on May 07, 2013, 11:09:44 AM

I have an excel spreadsheet that has two columns, the first column shows the old file name and the second shows the new file name.

What I am wanting to do is to change all the file names from the new name back to the old name... I have approximately 200 and I done this manually last time and it took a long time! As you can probably imagine!

Copy the columns into a temporary spreadsheet and use a formula to assemble a batch command in a column then copy the column into Notepad and save it as a batch and then run it.



Discussion

No Comment Found