Batch file command not working as hoped

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
chappers
Posts: 2
Joined: 28 Jun 2011 18:53

Batch file command not working as hoped

#1 Post by chappers » 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:

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

chappers
Posts: 2
Joined: 28 Jun 2011 18:53

Re: Batch file command not working as hoped

#2 Post by chappers » 30 Jun 2011 05:33

Fixed it. For some reason, after removing the /a from the set command, I then had to remove the forward slash from in front of the folder name variable to which the file was saved. So (move "%~1" "%date%\") had to become (move "%~1" "%date%")

I have no idea why.

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: Batch file command not working as hoped

#3 Post by trebor68 » 03 Jul 2011 00:51

The info file 'info.txt' will have this following row (here in German environment):
File date/time = 29.06.2011 / 12:36:41

I think that is in the English environment:
File date/time = 29/06/2011 / 12:36:41

When you use the 'FOR' command with the value "delims=/" than are following value in tokens:
token 1: "File date"
token 2: "time = 29"
token 3: "06"
token 4: "2011 "
token 5: " 12:36:41"

With the command "CD" you will create the folder "06_2011", but you will move the files to the folder "06_2011 ". Not possibel.

=============

Here my solution for this problem. Please use the following "FOR" command (with value "delims=/ "):

Code: Select all

for /f "tokens=6,7 delims=/ " %%a in ('type %info% ^| find /i "File date"') do (set month=%%a) & (set year=%%b)


Here the value in tokens:
token 1: "File"
token 2: "date"
token 3: "time"
token 4: "="
token 5: "29"
token 6: "06"
token 7: "2011"
token 8: "12:36:41"

The value "delims=" with no <space> will only have one token.
The value "delims=/ " will use the seperators "/" and <space>. The <space> seperator is anytime the last seperator.
With this value "delims=/-." you can seperated the date in different countries i.E. "2011/07/03", "2011-07-03" or "03.07.2011".
I will hope that this will work correct for you.

Post Reply