Batch file command not working as hoped
Posted: 28 Jun 2011 19:16
Hi,
Surprised to find a forum for batch help! I wonder if anyone could please spare the time and energy to help me?
I'm new to batch files; only programming languages I know are HTML and PHP. I wanted to sort the 400 or so photos of my son by the month and year they were taken, as the info is in the photo file. I googled and found a similar block of code used to sort images by size, and quickly learnt a little about batch file commands to alter it to grab the image month and year data instead, and then create folders using the month and year as the folder name, and sort the files into the respective folders. I found it hard as it's an odd language and doesn't seem as easy and powerful as PHP. It's odd not having {} defining where a command begins and ends! I don't see how it knows when a command finishes and the next command is independent and not to be treating as part of the last one. As for the %~1 and things like that...who knows.
Anyway, I found a flaw: after creating a file containing the image info, this code grabs the date and month in numerical form:
I found it failed to grab the month sometimes, and after much googling and reading the set/? help, finally discovered the /a after set means it is treating numbers beginning with 0 as octals. Since 08 isn't an octal, it was failing when the month was 08. So, I removed the /a option and it now grabs 08 as it should, but doesn't do what it's supposed to do. It creates the folder with correct name (such as 08_2011) but doesn't move the image file into the folder. The CMD prompt just states "can't find specified path" or something like that. If I replace the /a after set, it works albeit without the month present, so folder name becomes "_2011".
Can anyone help? Why doesn't it just assign the data to a variable like PHP?!
Here's the whole code for anyone requiring it:
Surprised to find a forum for batch help! I wonder if anyone could please spare the time and energy to help me?
I'm new to batch files; only programming languages I know are HTML and PHP. I wanted to sort the 400 or so photos of my son by the month and year they were taken, as the info is in the photo file. I googled and found a similar block of code used to sort images by size, and quickly learnt a little about batch file commands to alter it to grab the image month and year data instead, and then create folders using the month and year as the folder name, and sort the files into the respective folders. I found it hard as it's an odd language and doesn't seem as easy and powerful as PHP. It's odd not having {} defining where a command begins and ends! I don't see how it knows when a command finishes and the next command is independent and not to be treating as part of the last one. As for the %~1 and things like that...who knows.
Anyway, I found a flaw: after creating a file containing the image info, this code grabs the date and month in numerical form:
Code: Select all
for /f "tokens=3,4 delims=/" %%a in ('type %info% ^| find.exe /i "File date"') do (set /a month=%%a) & (set /a year=%%b)
I found it failed to grab the month sometimes, and after much googling and reading the set/? help, finally discovered the /a after set means it is treating numbers beginning with 0 as octals. Since 08 isn't an octal, it was failing when the month was 08. So, I removed the /a option and it now grabs 08 as it should, but doesn't do what it's supposed to do. It creates the folder with correct name (such as 08_2011) but doesn't move the image file into the folder. The CMD prompt just states "can't find specified path" or something like that. If I replace the /a after set, it works albeit without the month present, so folder name becomes "_2011".
Can anyone help? Why doesn't it just assign the data to a variable like PHP?!
Here's the whole code for anyone requiring it:
Code: Select all
@echo off
cls
setlocal
:: Temp-/Infofile path/name
set info=%temp%\info.txt
:: Dimension file path/name
set dims=date.txt
:: IrfanView
set iview=C:\Program Files\IrfanView\i_view32.exe
:: File extensions
set filext=*.jpg *.tif
for %1 %%a in (%filext%) do call :EXTRACT "%%a"
goto :END
:EXTRACT
"%iview%" %1 /info="%info%"
for /f "tokens=3,4 delims=/" %%a in ('type %info% ^| find.exe /i "File date"') do (set month=%%a) & (set year=%%b)
echo File: %~1 >> "%dims%"
echo Month: %month% >> "%dims%"
echo Year: %year% >> "%dims%"
set date=%month%_%year%
if exist %date% (echo Moved to existing folder >> "%dims%") & (move "%~1" "%date%\") else (echo Moved to new folder %date% >> "%dims%") & (MD %date%) & (move "%~1" "%date%\")
echo. >> "%dims%"
goto :EOF
:END
if exist %info% del %info%
endlocal