Using name of a file to rename a folder (BATCH)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
phoenix_Rising
Posts: 38
Joined: 04 Apr 2012 03:11

Using name of a file to rename a folder (BATCH)

#1 Post by phoenix_Rising » 16 Apr 2012 21:22

Hi guys,

Maybe a relatively simple one?

I have a bunch of audio files in a folder than are all labelled in the same way:

ARTIST - ALBUM - TRACK.ape

a few examples from the folder...

c:\test\JOHNSMITH - The best of John Smith - Smithy.ape
c:\test\JOHNSMITH - The best of John Smith - Smithingtonape
c:\test\JOHNSMITH - The best of John Smith - Smithywithy.ape

I want to rename the folder that they are in (C:\TEST) to:

c:\JOHNSMITH - The best of John Smith\JOHNSMITH - The best of John Smith - Smithy.ape
c:\JOHNSMITH - The best of John Smith\JOHNSMITH - The best of John Smith - Smithingtonape
c:\JOHNSMITH - The best of John Smith\JOHNSMITH - The best of John Smith - Smithywithy.ape

How do i read this information using the FINDSTR command i've tried a few things but can't tell it to read everything up to the 2nd '-' sign! any ideas?

I will need to be able to use it within a loop so that the batch can look at a whole list of folders and pull the info from any track with a .ape extension and then rename the folder..

Any ideas?

Aacini
Expert
Posts: 1886
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Using name of a file to rename a folder (BATCH)

#2 Post by Aacini » 16 Apr 2012 22:01

Code: Select all

@echo off
setlocal EnableDelayedExpansion
cd \test
for %%f in (*.ape) do (
   for /F "tokens=1-2 delims=-" %%a in ("%%f") do (
      set "targetDir=\%%a-%%b"
      if not exist "!targetDir!" md "!targetDir!"
      move "%%f" "!targetDir!"
   )
)

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

Re: Using name of a file to rename a folder (BATCH)

#3 Post by foxidrive » 17 Apr 2012 00:41

Some artists have a - in their name.

AC-DC
Olivia Newton-John


Is that the case here too?

phoenix_Rising
Posts: 38
Joined: 04 Apr 2012 03:11

Re: Using name of a file to rename a folder (BATCH)

#4 Post by phoenix_Rising » 17 Apr 2012 06:09

Will that effect the example code given then foxi?

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: Using name of a file to rename a folder (BATCH)

#5 Post by Squashman » 17 Apr 2012 06:42

phoenix_Rising wrote:Will that effect the example code given then foxi?

Yes. If you look at the FOR command you will notice it using the DELIMS option and is using the HYPHEN as the delimiter.

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

Re: Using name of a file to rename a folder (BATCH)

#6 Post by foxidrive » 17 Apr 2012 07:11

I had a similar job so adapted the batch file I used for your task.

This should rename each folder in the tree to the leading portion of the first .ape filename in each folder.
The code expects there to be two occurences of " - " in the .APE filename and will use the leading section up to the second " - ".

So c:\temp\ containing a filename of "abc-def - 123 & xxx - my trackname here.ape" will be renamed to "abc-def - 123 & xxx"

As always, test it on some sample folders and files first.


Code: Select all

@echo off
for /f "delims=" %%a in ('dir /ad /b /s') do (
set "flag="
echo "%%a"
for /f "delims=" %%b in ('dir "%%a\*.ape" /a-d /b /s 2^>nul ') do (
if not defined flag call :next "%%a" "%%b"
set flag=1
)
)
pause
GOTO:EOF

:next
set "name=%~nx2"
set "num=0"
set "c=0"
:loop
set /a num=num+1
call set "name2=%%name:~%num%,3%%"
if "%name2%"==" - " set /a c=c+1
if "%name2%"=="" goto :EOF
if not %c% GTR 1 goto :loop
call ren "%~1" "%%name:~0,%num%%%"

phoenix_Rising
Posts: 38
Joined: 04 Apr 2012 03:11

Re: Using name of a file to rename a folder (BATCH)

#7 Post by phoenix_Rising » 17 Apr 2012 08:06

Awesome i think ive got that working now! Will test further! Thanks guys.

phoenix_Rising
Posts: 38
Joined: 04 Apr 2012 03:11

Re: Using name of a file to rename a folder (BATCH)

#8 Post by phoenix_Rising » 23 Apr 2012 11:08

On further investigation it seems strange that i can save your code Foxi as a bat file and run it and it works...however when i try and run it from within my existing main batch file it dosnt seem to rename the folders? Any ideas? I've just tried DisableDelayedExpansion on it in case Enable was causing any problems with it... Any idea why it wont run within my code?

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

Re: Using name of a file to rename a folder (BATCH)

#9 Post by foxidrive » 23 Apr 2012 14:30

phoenix_Rising wrote:Any idea why it wont run within my code?


Without seeing how you have placed the code with your other lines, all I can do is guess.
Is the last line below the code above a GOTO :EOF

If you have placed the code above some other lines and there is no GOTO :EOF then all sorts of weird things probably happen.

phoenix_Rising
Posts: 38
Joined: 04 Apr 2012 03:11

Re: Using name of a file to rename a folder (BATCH)

#10 Post by phoenix_Rising » 24 Apr 2012 05:30

I have injected your code half way through my batch file.

I take it that goto :EOF will terminate the batchfile once the loop completes? for the batch file to continue after the loop completes how would i proceed? Any pointers?

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: Using name of a file to rename a folder (BATCH)

#11 Post by Squashman » 24 Apr 2012 05:40

phoenix_Rising wrote:I have injected your code half way through my batch file.

I take it that goto :EOF will terminate the batchfile once the loop completes? for the batch file to continue after the loop completes how would i proceed? Any pointers?


Is it really that hard for you to show us the code so we can see how you put the code into your existing batch file?
Read this and start learning what all those commands in your scripts are doing.
http://ss64.com/nt/goto.html

phoenix_Rising
Posts: 38
Joined: 04 Apr 2012 03:11

Re: Using name of a file to rename a folder (BATCH)

#12 Post by phoenix_Rising » 24 Apr 2012 06:09

@ Squashman - The Batch is VERY long... (probably due to my code not being as efficient as it could be) Heres is 'What i deem' the relevant code before and after where i have injected Foxi's example code:

:APE_TRACK_COUNT
setlocal DISABLEDELAYEDEXPANSION
if exist %DRIVE%\ojw\logs\APE_Count.txt (del /q %DRIVE%\ojw\logs\APE_Count.txt) else (echo file not found!)
for /f "delims=" %%a in ('dir %DRIVE%\ojw\completed_album\*.ape /s /b') do echo %%~dpna>>%DRIVE%\ojw\logs\APE_Count.txt

:FOLDER_RENAMING
for /f "delims=" %%a in ('dir %DRIVE%\ojw\completed_album /ad /b /s') do (
set "flag="
echo "%%a"
for /f "delims=" %%b in ('dir "%%a\*.ape" /a-d /b /s 2^>nul ') do (
if not defined flag call :next "%%a" "%%b"
set flag=1
)
)
GOTO:EOF

:next
set "name=%~nx2"
set "num=0"
set "c=0"
:loop
set /a num=num+1
call set "name2=%%name:~%num%,3%%"
if "%name2%"==" - " set /a c=c+1
if "%name2%"=="" goto :EOF
if not %c% GTR 1 goto :loop
call ren "%~1" "%%name:~0,%num%%%"

:CHECK_WHETHER_ANYTHING_FAILED
setlocal DISABLEDELAYEDEXPANSION
cls
if exist %DRIVE%\ojw\logs\REPORT.bat (del /q %DRIVE%\ojw\logs\REPORT.bat) else (echo file not found!)
if exist %DRIVE%\OJW\COMPLETED_Album\FAILREPORT.txt (del /q %DRIVE%\OJW\COMPLETED_Album\FAILREPORT.txt) else (echo file not found!)
echo @ECHO OFF>>%DRIVE%\OJW\LOGS\REPORT.bat
echo :REPORTINGMENU>>%DRIVE%\OJW\LOGS\REPORT.bat
echo cls>>%DRIVE%\OJW\LOGS\REPORT.bat
echo COLOR CE>>%DRIVE%\OJW\LOGS\REPORT.bat
echo echo.>>%DRIVE%\OJW\LOGS\REPORT.bat
echo echo.>>%DRIVE%\OJW\LOGS\REPORT.bat
echo echo ====================================================================>>%DRIVE%\OJW\LOGS\REPORT.bat
echo echo Here is a list of TRACKS that have FAILED the conversion process..>>%DRIVE%\OJW\LOGS\REPORT.bat
echo echo ====================================================================>>%DRIVE%\OJW\LOGS\REPORT.bat
echo echo.>>%DRIVE%\OJW\LOGS\REPORT.bat
echo echo.>>%DRIVE%\OJW\LOGS\REPORT.bat
echo echo FAILED TRACK LIST:>>%DRIVE%\OJW\LOGS\REPORT.bat
echo echo.>>%DRIVE%\OJW\LOGS\REPORT.bat
echo ******************************************************************************>>%DRIVE%\OJW\COMPLETED_Album\FAILREPORT.txt
fc %DRIVE%\OJW\LOGS\WAV_Count.txt %DRIVE%\OJW\Logs\APE_Count.txt >>%DRIVE%\OJW\COMPLETED_Album\FAILREPORT.txt
echo type %DRIVE%\OJW\COMPLETED_Album\FAILREPORT.txt>>%DRIVE%\OJW\LOGS\REPORT.bat
echo echo press 1 to leave>>%DRIVE%\OJW\LOGS\REPORT.bat
echo Set /P _leave=>>%DRIVE%\OJW\LOGS\REPORT.bat
echo If "%%_leave%%"=="" goto :REPORTINGMENU>>%DRIVE%\OJW\LOGS\REPORT.bat
echo If /i "%%_leave%%"=="1" goto :EXIT>>%DRIVE%\OJW\LOGS\REPORT.bat
echo goto :REPORTINGMENU>>%DRIVE%\OJW\LOGS\REPORT.bat
echo :EXIT>>%DRIVE%\OJW\LOGS\REPORT.bat
echo exit>>%DRIVE%\OJW\LOGS\REPORT.bat
CALL %DRIVE%\OJW\LOGS\REPORT.bat
echo exit>>%DRIVE%\OJW\LOGS\REPORT.bat
CALL %DRIVE%\OJW\LOGS\REPORT.bat

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

Re: Using name of a file to rename a folder (BATCH)

#13 Post by foxidrive » 24 Apr 2012 11:15

In that section the two changes below should help.


phoenix_Rising wrote:@ Squashman - The Batch is VERY long... (probably due to my code not being as efficient as it could be) Heres is 'What i deem' the relevant code before and after where i have injected Foxi's example code:

:APE_TRACK_COUNT
setlocal DISABLEDELAYEDEXPANSION
if exist %DRIVE%\ojw\logs\APE_Count.txt (del /q %DRIVE%\ojw\logs\APE_Count.txt) else (echo file not found!)
for /f "delims=" %%a in ('dir %DRIVE%\ojw\completed_album\*.ape /s /b') do echo %%~dpna>>%DRIVE%\ojw\logs\APE_Count.txt

:FOLDER_RENAMING
for /f "delims=" %%a in ('dir %DRIVE%\ojw\completed_album /ad /b /s') do (
set "flag="
echo "%%a"
for /f "delims=" %%b in ('dir "%%a\*.ape" /a-d /b /s 2^>nul ') do (
if not defined flag call :next "%%a" "%%b"
set flag=1
)
)
GOTO :CHECK_WHETHER_ANYTHING_FAILED


:next
set "name=%~nx2"
set "num=0"
set "c=0"
:loop
set /a num=num+1
call set "name2=%%name:~%num%,3%%"
if "%name2%"==" - " set /a c=c+1
if "%name2%"=="" goto :EOF
if not %c% GTR 1 goto :loop
call ren "%~1" "%%name:~0,%num%%%"
GOTO:EOF

:CHECK_WHETHER_ANYTHING_FAILED
setlocal DISABLEDELAYEDEXPANSION
cls
if exist %DRIVE%\ojw\logs\REPORT.bat (del /q %DRIVE%\ojw\logs\REPORT.bat) else (echo file not found!)
if exist %DRIVE%\OJW\COMPLETED_Album\FAILREPORT.txt (del /q %DRIVE%\OJW\COMPLETED_Album\FAILREPORT.txt) else (echo file not found!)
echo @ECHO OFF>>%DRIVE%\OJW\LOGS\REPORT.bat
echo :REPORTINGMENU>>%DRIVE%\OJW\LOGS\REPORT.bat
echo cls>>%DRIVE%\OJW\LOGS\REPORT.bat
echo COLOR CE>>%DRIVE%\OJW\LOGS\REPORT.bat
echo echo.>>%DRIVE%\OJW\LOGS\REPORT.bat
echo echo.>>%DRIVE%\OJW\LOGS\REPORT.bat
echo echo ====================================================================>>%DRIVE%\OJW\LOGS\REPORT.bat
echo echo Here is a list of TRACKS that have FAILED the conversion process..>>%DRIVE%\OJW\LOGS\REPORT.bat
echo echo ====================================================================>>%DRIVE%\OJW\LOGS\REPORT.bat
echo echo.>>%DRIVE%\OJW\LOGS\REPORT.bat
echo echo.>>%DRIVE%\OJW\LOGS\REPORT.bat
echo echo FAILED TRACK LIST:>>%DRIVE%\OJW\LOGS\REPORT.bat
echo echo.>>%DRIVE%\OJW\LOGS\REPORT.bat
echo ******************************************************************************>>%DRIVE%\OJW\COMPLETED_Album\FAILREPORT.txt
fc %DRIVE%\OJW\LOGS\WAV_Count.txt %DRIVE%\OJW\Logs\APE_Count.txt >>%DRIVE%\OJW\COMPLETED_Album\FAILREPORT.txt
echo type %DRIVE%\OJW\COMPLETED_Album\FAILREPORT.txt>>%DRIVE%\OJW\LOGS\REPORT.bat
echo echo press 1 to leave>>%DRIVE%\OJW\LOGS\REPORT.bat
echo Set /P _leave=>>%DRIVE%\OJW\LOGS\REPORT.bat
echo If "%%_leave%%"=="" goto :REPORTINGMENU>>%DRIVE%\OJW\LOGS\REPORT.bat
echo If /i "%%_leave%%"=="1" goto :EXIT>>%DRIVE%\OJW\LOGS\REPORT.bat
echo goto :REPORTINGMENU>>%DRIVE%\OJW\LOGS\REPORT.bat
echo :EXIT>>%DRIVE%\OJW\LOGS\REPORT.bat
echo exit>>%DRIVE%\OJW\LOGS\REPORT.bat
CALL %DRIVE%\OJW\LOGS\REPORT.bat
echo exit>>%DRIVE%\OJW\LOGS\REPORT.bat
CALL %DRIVE%\OJW\LOGS\REPORT.bat

phoenix_Rising
Posts: 38
Joined: 04 Apr 2012 03:11

Re: Using name of a file to rename a folder (BATCH)

#14 Post by phoenix_Rising » 24 Apr 2012 12:22

i had tried it with the additional GOTO :CHECK_WHETHER_ANYTHING_FAILED
where you placed it but will add the other goto :eof tonight and test... thanks Foxi... I owe you a few beers! :)

Post Reply