thr333 wrote:
Spot on avery_larry your code works !!
Thankyou ++++
I needed to change line 5 to append (>>)
rather than overwrite (>) the text file.
Good catch.
Quote:
I made the code echo the results line-by-line in the console
and added pauses between each line so I could visualize the runtime.
Yes sir, she kept outputting and outputting and outputting
every line with relative path names.
Great Stuff.
As you can see, I'm a DOS n00b.
It would be nice for my learning
if you could explain to me what each line is doing.
Code:
for /f "tokens=*" %%a in ('cd') do set "parent=%%~a"
fairly standard for expression to use the ouput of a command to set a variable. In this case, the command is 'cd' and the output of the command is stored in the variable "parent", with a backslash appended. This variable is used later. Note now that I recognize I could have just used %cd%\ instead.
Code:
for /f "tokens=*" %%a in ('dir /s /b *.mp3') do (
set "file=%%~a"
call echo %%file:%parent%=%%>>listall.txt
)
This is one for loop. Once again, we're using the output of a command to run the for loop against. The "tokens=*" portion simply accomodates long file names. The 'dir /s /b *.mp3' portion you should already understand. The rest of the for loop is done on each filename returned by the dir command, one at a time.
This just sets a variable to use in the next line of code. I can't do the substitutions and double expansion with the %%a variable. Wrapping the variable name, equal sign, and variable value inside double quotes does 2 things:
1) It helps the set command deal with some special characters.
2) It helps to make sure you don't accidentally add any spaces to the variable.
Code:
call echo %%file:%parent%=%%>>listall.txt
This is where the magic happens. At the basic level, we're using a simple substring substitution to eliminate the parent directories. If your current directory was c:\tmp then we're trying to do this:
echo %file:c:\tmp\=% Which substitutes nothing (blank or nul) for c:\tmp\
The rest of it I'm not going to explain very well, because it's quite complicated. The %parent% is the variable that contains the parent directory, which we want to blank out of the lines, effectively we are stripping "c:\my music\parent folder" out of the dir's ouput. That's what this portion does:
:%parent%=
Using the "call" function allows us to evaluate the variable string twice. The first evaluation will expand any normal looking variables (single % surround) and it will evaluate any double percents as single percents. Something like this:
Code:
call echo %%file:%parent%=%%>>listall.txt becomes:
echo %file:c:\parentfolder\=%>>listall.txt
And THEN the echo command is run, which echos the %file% variable (which we just set to %%~a) and replaces "c:\parentfolder" with nul.
Quote:
What are the brackets doing from lines 3 to 6 . . .
The parentheses will group the commands together, so that everything in the parentheses is done to each line of the for loop.
Quote:
. . . and why are you indenting
lines 4 and 5.
Just because it's easier to read and keep track of the parentheses -- the groupings.
Quote:
Ummm, one last thing...
So to make your batch also rename
listall.txt to
[the name of the parent folder].m3uI add this code to the end of yours:
set foldervar=%CD%
set foldervar=%foldervar:*\=%
set foldervar=%foldervar:*\=%
set foldervar=%foldervar:*\=%
set foldervar=%foldervar:*\=%
set foldervar=%foldervar:*\=%
ren listall.txt "%foldervar%.m3u"
Is there a better way to code this function
or do I really need to add all those lines ?
I'm pretty sure the answer has something to do with the
FOR command.
--thr333
Well, you could do something similar to the above and strip out the parent folder from the current folder something like this:
**untested**
Code:
set "folder=%cd%"
cd ..
call ren "%folder%\templaylist.m3u" "%%folder:%cd%\=%%"
Or you can use a for loop like this:
Code:
CD %1
Dir /o:n /s /b *.mp3 > TempPlaylist.m3u
Set "foldervar=%CD%"
for /l %%a in (1,1,10) do call set "foldervar=%%foldervar:*\=%%"
Ren TempPlaylist.m3u "%foldervar%.m3u"
Nice use, by the way, of a wildcard substring replace. I'd forgotten that you can do that.