Normalizing Folder / Directory Names

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ferris881
Posts: 4
Joined: 26 Apr 2012 15:39

Normalizing Folder / Directory Names

#1 Post by ferris881 » 26 Apr 2012 16:06

I have an auto downloader that will drop audio files I need into a folder it creates. My problem with that is the names will vary day to day and week to week.

Here is an example

//Path_to_Folder/Program Name2 (2) DD.MM.YY 22
- OR -
//Path_to_Folder/REVISED Program Name2 (2) DD.MM.YY 22

Some things I know will always be there are "Program Name" and "DD.MM.YY" the rest are up in the air. So I have devised a less elegant way of hangleing it

Code: Select all

@Echo Off

P:
cd "Path_to_Folder"

#Grab today's date
SET WEEKDAY=%date:~0,3%
SET MONTH=%date:~4,2%
SET DAY=%date:~7,2%
SET YEAR=%Date:~12,2%

#find the directory with wildcards
cd "*Program Name? (?) %MONTH%.%DAY%.%YEAR%*"

#Copy all audio files
copy /Y /B *.mp3 "I:\Shows\Dick Clark Calendar\"
exit


Now I'm for a script that would look take a variable %MONTH%.%DAY%.%YEAR% save it, so I could simply rename the folder from:
//Path_to_Folder/Program Name2 (2) DD.MM.YY 22
to:
//Path_to_Folder/Program Name DD.MM.YY

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Normalizing Folder / Directory Names

#2 Post by Fawers » 26 Apr 2012 16:20

Foxidrive posted a code on this thread, and part of the code was about getting the date in a similar - if not the same - format.

When I ECHO %date% on my command line, it outputs the date in the DD/MM/YYYY format.

According to foxidrive's code, we could do something like this:

Code: Select all

for /f "tokens=2-4 delims=/ " %%a in ("%date%") do (
  set dd=%%a
  set mm=%%b
  set yy=%%c
)
set yy=%yy:~2%

This will give us the DDMMYY format.

Note: if your system's date format is MMDDYYYY, replace dd=%%a with mm=%%a, and vice versa.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Normalizing Folder / Directory Names

#3 Post by foxidrive » 26 Apr 2012 16:21

Show us some real life examples and what you want to rename them to. This is folder names only, right?

ferris881
Posts: 4
Joined: 26 Apr 2012 15:39

Re: Normalizing Folder / Directory Names

#4 Post by ferris881 » 26 Apr 2012 23:50

Yes, we are talking just folders. If my program can predict the folder name, then the rest of the program can find the files and process them.

Here is the list of file folder names for Dick Clark.

  • Dick Clark Cal6 (7) 05.05.12 1
  • Dick Clark Cal3 (7) 04.25.12
  • Dick Clark Cal1 (7) and CUE SHEET 04.30.1
  • Dick Clark Cal4 (7) 04.26.12 3
  • Dick Clark Cal5 (7) 05.04.12 3
  • Dick Clark Cal2 (7) 04.24.12 4
  • Dick Clark Cal7 (7) 05.06.12 1
  • Dick Clark Cal5 (7) 04.27.12 1
  • Dick Clark Cal7 (7) 04.28.12 1
  • Dick Clark Cal4 (7) 05.03.12 2

I know know the following:
  • "Dick Clark Cal" <- Will always appear in the name, not always the first thing as I've seen REVISED in front rarely, but on occasions.
  • # [1-7] <- This is the day of the week in numeric form Monday = 1
  • (#) <- Unknown
  • "and CUE SHEET" <- On Random Days
  • MM.DD.YY <- Date of Air
  • # [0..] <- A Random number I don't care about

So I would love to take out the extra information I don't need and keep just the program name and the date it airs

From: Dick Clark Cal6 (7) 05.05.12 1
To: Dick Clark Cal 05.05.12

The problem would be capturing the date and the name of the show in variables and renaming the whole thing.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Normalizing Folder / Directory Names

#5 Post by foxidrive » 27 Apr 2012 07:00

This uses GNUsed for Windows (download it and place it on the path) - it writes renfolder.bat and then it will run it if you remove the REM

Try it out and view renfolder.bat before you enable it.


Code: Select all

@echo off 
dir /ad /b |find /v /i "and CUE SHEET" |sed "s/\(.*\) (.).\(........\).*/ren \x22&\x22 \x22\1 \2\x22/i" >renfolder.bat
dir /ad /b |find    /i "and CUE SHEET" |sed "s/\(.*\) (.) and CUE SHEET \(........\).*/ren \x22&\x22 \x22\1 \2\x22/i" >>renfolder.bat
REM call renfolder.bat
pause


This is what it created from your sample text - I edited one line because it is not complete (cue sheet line was missing a year digit). I suspect that was a typo.

ren "Dick Clark Cal6 (7) 05.05.12 1" "Dick Clark Cal6 05.05.12"
ren "Dick Clark Cal3 (7) 04.25.12" "Dick Clark Cal3 04.25.12"
ren "Dick Clark Cal4 (7) 04.26.12 3" "Dick Clark Cal4 04.26.12"
ren "Dick Clark Cal5 (7) 05.04.12 3" "Dick Clark Cal5 05.04.12"
ren "Dick Clark Cal2 (7) 04.24.12 4" "Dick Clark Cal2 04.24.12"
ren "Dick Clark Cal7 (7) 05.06.12 1" "Dick Clark Cal7 05.06.12"
ren "Dick Clark Cal5 (7) 04.27.12 1" "Dick Clark Cal5 04.27.12"
ren "Dick Clark Cal7 (7) 04.28.12 1" "Dick Clark Cal7 04.28.12"
ren "Dick Clark Cal4 (7) 05.03.12 2" "Dick Clark Cal4 05.03.12"
ren "Dick Clark Cal1 (7) and CUE SHEET 04.30.12" "Dick Clark Cal1 04.30.12"


Edit: changed /ad in the DIR command
Last edited by foxidrive on 27 Apr 2012 14:23, edited 1 time in total.

ferris881
Posts: 4
Joined: 26 Apr 2012 15:39

Re: Normalizing Folder / Directory Names

#6 Post by ferris881 » 27 Apr 2012 13:24

I had to edit your script just a little, for some reason my computer didn't like

Code: Select all

dir /a-d /b


so I had to use

Code: Select all

dir /-a /d /b


and I'm getting Directories and files when it searches.. I'm fining that is very annoying.

ferris881
Posts: 4
Joined: 26 Apr 2012 15:39

Re: Normalizing Folder / Directory Names

#7 Post by ferris881 » 27 Apr 2012 13:25

Sorry hit enter before I could say thanks for that code.. It would have taken me many hours of searching on Google before I even got close to that.

:D :D :D

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Normalizing Folder / Directory Names

#8 Post by foxidrive » 27 Apr 2012 14:25

You're welcome.

Note the change I made in the code above.
It should be this to get directories:

dir /ad /b


I was testing from a file rather than using DIR. :)

I reread your specs and realised that the number after the title wasn't needed, so this will remove that too.
If "revised " appears at the very beginning (case insensitive) then it will be removed too.

Code: Select all

@echo off
dir /ad /b |find /v /i "and CUE SHEET" |sed "s/\(.*\). (.).\(........\).*/ren \x22&\x22 \x22\1 \2\x22/i" >renfolder.bat
dir /ad /b |find    /i "and CUE SHEET" |sed "s/\(.*\). (.) and CUE SHEET \(........\).*/ren \x22&\x22 \x22\1 \2\x22/i" >>renfolder.bat
sed "s/\x22revised /\x22/2i" renfolder.bat >renfolder2.bat
REM call renfolder2.bat
REM del renfolder?.bat 2>nul

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Normalizing Folder / Directory Names

#9 Post by foxidrive » 27 Apr 2012 15:04

Here's another variation that writes the date as YY.MM.DD which sorts correctly in a directory listing, if that is useful to you. Example below.

Code: Select all

@echo off
dir /ad /b |find /v /i "and CUE SHEET" |sed "s/\(.*\). (.).\(..\).\(..\).\(..\).*/ren \x22&\x22 \x22\1 \4.\2.\3\x22/i" >renfolder.bat
dir /ad /b |find    /i "and CUE SHEET" |sed "s/\(.*\). (.) and CUE SHEET \(..\).\(..\).\(..\).*/ren \x22&\x22 \x22\1 \4.\2.\3\x22/i" >>renfolder.bat
sed "s/\x22revised /\x22/2i" renfolder.bat >renfolder2.bat
REM call renfolder2.bat
REM del renfolder?.bat 2>nul



ren "Dick Clark Cal2 (7) 04.24.12 4" "Dick Clark Cal 12.04.24"
ren "Dick Clark Cal3 (7) 04.25.12" "Dick Clark Cal 12.04.25"
ren "Dick Clark Cal4 (7) 04.26.12 3" "Dick Clark Cal 12.04.26"
ren "Dick Clark Cal4 (7) 05.03.12 2" "Dick Clark Cal 12.05.03"
ren "Dick Clark Cal5 (7) 04.27.12 1" "Dick Clark Cal 12.04.27"
ren "Dick Clark Cal5 (7) 05.04.12 3" "Dick Clark Cal 12.05.04"
ren "Dick Clark Cal6 (7) 05.05.12 1" "Dick Clark Cal 12.05.05"
ren "Dick Clark Cal7 (7) 04.28.12 1" "Dick Clark Cal 12.04.28"
ren "Revised Dick Clark Cal7 (7) 05.06.12 1" "Dick Clark Cal 12.05.06"
ren "Dick Clark Cal1 (7) and CUE SHEET 04.30.12" "Dick Clark Cal 12.04.30"


and sorted:

Dick Clark Cal 12.04.24
Dick Clark Cal 12.04.25
Dick Clark Cal 12.04.26
Dick Clark Cal 12.04.27
Dick Clark Cal 12.04.28
Dick Clark Cal 12.04.30
Dick Clark Cal 12.05.03
Dick Clark Cal 12.05.04
Dick Clark Cal 12.05.05
Dick Clark Cal 12.05.06

Post Reply