I am trying to rename files in multiple subdirectories, and break out the file name and the folder one level higher than the file into variables with dos under Windows 7 (or xp/vista).
1. Find *.mkv files.
2. For each file, put file name (minus extension) into a variable
3. For each file, get the folder name the file is in and put that into a variable.
I am having problems mainly with the getting of the folder that has the file in it b/c I can't be sure what token it will be.
:: GET LIST OF ALL FOLDERS CONTAINING A *.MKV FILE AND LOOP THROUGH EACH
FOR /f "tokens=* delims=" %%a in ('dir/s/b/a-d \*.mkv') do (
:: CHECK TO SEE IF A FILE HAS ALREADY BEEN PROCESSED, IF SO SKIP IT AND MOVE ON
IF EXIST "%%~dpa%%~na02%%~xa" (
ECHO File %%~na02%%~xa already exists...skipping it.
:: IF NEED TO BE PROCESSED RENAME THE FILE ADDING 02 TO THE MOVIE
ren "%%a" "%%~na02%%~xa"
I run it the first time on a directory containing only a movie.mkv and it works great, it renames the movie.mkv to movie02.mkv. However, the next time around my logic looks for *.mkv and finds movie02.mkv, then I adds an 02 to that making it movie0202.mkv.
So, if I can find out the name of the folder holding the movie.mkv, that will always be the movie name and I can set that to say if exist %MOVIEFOLDER%02.mkv, I will know if I processed it.
This all leads me to where I have no idea how to get the folder holding that file (I am sure I have to not use tokens=* but don't know how to say "last token".)
Any ideas? Does that make any sense? Thanks to all.