It Nearly Drove Me Mad
I wanted a batch file that would copy movies from a server to a HDD for my Camper.
Because the TV in the Camper is a cheapy it cannot handle directories with more than a fairly limited number of files (128? ) so I decided to copy them into a directory based on their year of issue.
Most of my movies have the year of release as the last 4 digits of the file name. eg:
A Brilliant Young Mind 2014.mp4
A Monster Calls 2016.mp4
So I decided to put them in a folder with the name of the year. eg
X:\2014\A Brilliant Young Mind 2014.mp4
A batch file is the way to go because there are over a thousand files but a DOS batch file will do it automatically and I thought it would be so easy.
It was not at all easy - it took me hours to work it out - variables are usually designated
%variablename% but for some reason within a "For" loop the have to be
!variablename!. I still don't understand why to be honest because I can't find any instructions/literature on it.
Anyway this now works:
@echo off
:: This file copies files with a numeric title to a back up directory
:: eg "Title 2018.mkv" will be copied to directory "2018"
:: "Title Name.avi" will be ignored
setlocal enabledelayedexpansion
:: Set Source Path
set folderpath=F:\test
:: Set Target Path
set FinalPath=F:\Test\
for %%f in (%folderpath%\*.*) do (
set "foldername=%%~nf"
set "Name=!foldername:~-4!"
set /a "NameNum=!name!"
if "!name!"=="!NameNum!" (
set "NewPath=%FinalPath%!Name!\"
rem mkdir "!NewPath!" 2> NUL <<--- Not Needed!
xcopy "%%f" "!NewPath!"
)
)
pause
Any one know why there is the difference in variable designations? I wonder if ! is the same a %%?
It is probably obvious when you know but I rarely use "for" loops so it is beyond me at the moment but at least I now know what to do if not why.