complicated rename

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
crashnburn
Posts: 3
Joined: 29 Nov 2011 10:13

complicated rename

#1 Post by crashnburn » 29 Nov 2011 10:25

Hello all. I have been trying to make a batch file that will rename my google analytics pdf files. So far I have only been able to come up with a mess of a batch file that only renames one. Currently the name of the pdf's are in this format: "Analytics_www.oursite.com_20111127_(Top_Exit_Pages)". I would like them to be cut down to just "Top_Exit_Pages". I have been playing around and this is what I have come up with.


@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

for /F %%a in ('dir /b *.pdf') do set FileName=%%~na
echo %FileName% > temp.txt

for /f "tokens=2 delims=(" %%a in (temp.txt) do echo %%a > temp2.txt

for /f "tokens=1 delims=)" %%a in (temp2.txt) do set new=%%a

ren %FileName%.pdf %new%.pdf

del temp.txt
del temp2.txt

I have it pulling out the name and then renaming the file. The only issue I have is that it does not loop to rename the rest of the files. There are 8 total, same format but they are: Referring_Sites, Top_Landing_Pages, Top_Content, Keywords, All_Traffic, Map_Report, Overview, Top_Exit_Pages. The only thing that changes is the date, Analytics_www.oursite.com_20111127_(Top_Exit_Pages). The way I have it set up this shouldn't matter but I wanted to leave as much info just in case. Thank you all for your help in advance.
Last edited by crashnburn on 29 Nov 2011 14:44, edited 1 time in total.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: complicated rename

#2 Post by Ed Dyreen » 29 Nov 2011 13:05

'
hi crashnburn

I've looked at your post several times, but I don't really understand what you are asking.
I can help you with the loop , and I'm sure someone else can help you with the rest.

Code: Select all

:loop ()
:: (
   if /i ["compare"] == ["false"] goto :loop
:: )
The file writes are confusing me and I wonder whether it's required.

Code: Select all

@echo off &setlocal enableDelayedExpansion

for /f %%? in (

   '2^>nul dir /b *.pdf'

) do (
   set "$old.fileName=%%~n?"
   set "$new.fileName=%%~n?"

   set "$new.fileName=!$new.fileName:*(=!"
   set "$=!$new.fileName:*)=!"
   call set "$new.fileName=%%$new.fileName:)!$!=%%"

   echo.ren "!$old.fileName!.pdf" "!$new.fileName!.pdf"
)

pause
exit /b 0
example result:

Code: Select all

ren "(file).pdf" "file.pdf"
Druk op een toets om door te gaan. . .
Is that what you want ?, if so, remove the echo command on ren.

crashnburn
Posts: 3
Joined: 29 Nov 2011 10:13

Re: complicated rename

#3 Post by crashnburn » 29 Nov 2011 14:41

As shown in my post I am pretty novice with batch files. What my code does right now is copies the file name to a text file, "Analytics_www.oursite.com_20111127_(Top_Exit_Pages)". Then it pulls the last half into a temp file, Top_Exit_Pages)". The last part gets rid of the end and places only "Top_Exit_Pages" into another temp file. All of this just to get the variable for the final name. It is probably very unnecessary, but it is the only way I figured out how to do it. I have yet to test your solution because I am not sure how to place the loop (again very novice). A little more information might help out:
I have one folder with 8 pdf files.
All of the file names are in the format above.
The only part of the files that change on a daily bases are the series of numbers (dates).
All the individual file names are listed in my previous post, they replace the (Top_Exit_Pages) portion of the file name.
I hope that I have explained it a little better. I cannot thank you enough for your help. This simple task gets very tedious when you have to do it multiple times a day everyday. Thank you again.

crashnburn
Posts: 3
Joined: 29 Nov 2011 10:13

Re: complicated rename

#4 Post by crashnburn » 29 Nov 2011 14:50

UPDATE!!!!

I just tried your code and it worked great!!!! I can't thank you enough you are a life saver!

Post Reply