Using name of a file to rename a folder (BATCH)
Moderator: DosItHelp
-
- Posts: 38
- Joined: 04 Apr 2012 03:11
Using name of a file to rename a folder (BATCH)
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?
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?
Re: Using name of a file to rename a folder (BATCH)
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!"
)
)
Re: Using name of a file to rename a folder (BATCH)
Some artists have a - in their name.
AC-DC
Olivia Newton-John
Is that the case here too?
AC-DC
Olivia Newton-John
Is that the case here too?
-
- Posts: 38
- Joined: 04 Apr 2012 03:11
Re: Using name of a file to rename a folder (BATCH)
Will that effect the example code given then foxi?
Re: Using name of a file to rename a folder (BATCH)
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.
Re: Using name of a file to rename a folder (BATCH)
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.
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%%%"
-
- Posts: 38
- Joined: 04 Apr 2012 03:11
Re: Using name of a file to rename a folder (BATCH)
Awesome i think ive got that working now! Will test further! Thanks guys.
-
- Posts: 38
- Joined: 04 Apr 2012 03:11
Re: Using name of a file to rename a folder (BATCH)
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?
Re: Using name of a file to rename a folder (BATCH)
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.
-
- Posts: 38
- Joined: 04 Apr 2012 03:11
Re: Using name of a file to rename a folder (BATCH)
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?
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?
Re: Using name of a file to rename a folder (BATCH)
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
-
- Posts: 38
- Joined: 04 Apr 2012 03:11
Re: Using name of a file to rename a folder (BATCH)
@ 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
: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
Re: Using name of a file to rename a folder (BATCH)
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
-
- Posts: 38
- Joined: 04 Apr 2012 03:11
Re: Using name of a file to rename a folder (BATCH)
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!
where you placed it but will add the other goto :eof tonight and test... thanks Foxi... I owe you a few beers!