batch set /a command problem

June 28, 2011 at 18:31:03
Specs: Windows XP

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.

Anyway, I found a flaw: after creating a file containing the image info, this code grabs the date and month in numerical form:

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:

@echo off

:: 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

"%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

if exist %info% del %info%


See More: batch set /a command problem

Report •

June 30, 2011 at 04:58:02
Changing (move "%~1" "%date%\") to (move "%~1" "%date%") - removing the back slash at the end of %date% fixed it for some reason, but with set /a it wasn't a problem, it was only after removing the /a that it complained it couldn't find the specified path. Weird.

Report •
Related Solutions

Ask Question