Join for free
Bruce's Avatar
Bruce
Chatterbox
Bruce is offline
Wollongong, Australia
Joined: Apr 2012
Posts: 15,218
Bruce is male  Bruce has posted at least 25 times and has been a member for 3 months or more 
 
21-04-2018, 02:13 AM
1

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.
Bruce's Avatar
Bruce
Chatterbox
Bruce is offline
Wollongong, Australia
Joined: Apr 2012
Posts: 15,218
Bruce is male  Bruce has posted at least 25 times and has been a member for 3 months or more 
 
21-04-2018, 09:00 AM
2

Re: It Nearly Drove Me Mad

I have annotated the file to show what does what and made the variable names more meaningful - it is a help for me as much as anybody else.

@echo off

:: This file copies files with a year title to a back up directory
:: eg "Title 2018.txt" will be copied to directory "2018"
:: "Title Name.txt" will be ignored


setlocal enabledelayedexpansion

:: Set Source Path
set SourceFolder=F:\Test
:: Set Target Path
set FinalPath=F:\Test\


for %%f in (%SourceFolder%\*.txt) do (

:: Get filename without extension
set "FileName=%%~nf"

:: Get last four characters from the filename
set "Name=!FileName:~-4!"

::Get the Name as a number - if not a number it will equal "0"
set /a "NameNum=!Name!"

:: Check that the last four Digits are a number by comparing
if "!Name!"=="!NameNum!" (

::If they are a number then set the path and name for new Folder
set "NewPath=%FinalPath%!Name!\"

:: Copy to the New Folder (Xcopy will create the directory if missing)
xcopy "%%f" "!NewPath!"
)
)
pause
Realist
Chatterbox
Realist is offline
UK
Joined: Mar 2015
Posts: 9,184
Realist is male  Realist has posted at least 25 times and has been a member for 3 months or more 
 
21-04-2018, 05:22 PM
3

Re: It Nearly Drove Me Mad

The use of the !var! type variable is to ensure what is called "Delayed Expansion".

When you use %var% the system substitutes the variable ONCE upon first encounter.

In FOR loops where your variable value is likely to change constantly the %var% structure wouldn't work.

By using !var! you force the system to use "delayed expansion" which means that the variable value is determined EVERY time that line of code is executed.
Fogey
Senior Member
Fogey is offline
Shropshire
Joined: Apr 2012
Posts: 638
Fogey is male  Fogey has posted at least 25 times and has been a member for 3 months or more 
 
21-04-2018, 06:52 PM
4

Re: It Nearly Drove Me Mad

Gosh!
Rehab44
Chatterbox
Rehab44 is offline
Nil
Joined: Jul 2016
Posts: 10,394
Rehab44 is male  Rehab44 has posted at least 25 times and has been a member for 3 months or more 
 
21-04-2018, 07:22 PM
5

Re: It Nearly Drove Me Mad

Yeh...wot he said, right ?
Bruce's Avatar
Bruce
Chatterbox
Bruce is offline
Wollongong, Australia
Joined: Apr 2012
Posts: 15,218
Bruce is male  Bruce has posted at least 25 times and has been a member for 3 months or more 
 
30-04-2018, 08:29 AM
6

Re: It Nearly Drove Me Mad

Originally Posted by Realist ->
The use of the !var! type variable is to ensure what is called "Delayed Expansion".

When you use %var% the system substitutes the variable ONCE upon first encounter.

In FOR loops where your variable value is likely to change constantly the %var% structure wouldn't work.

By using !var! you force the system to use "delayed expansion" which means that the variable value is determined EVERY time that line of code is executed.
You're quite correct I eventually got round to looking it up on one of my favourite sites.

https://ss64.com/nt/delayedexpansion.html
 

Thread Tools


© Copyright 2009, Over50sForum   Contact Us | Over 50s Forum! | Archive | Privacy Statement | Terms of Use | Top

Powered by vBulletin Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.