Solved Problem converting 12-hr to 24-hr

October 22, 2011 at 16:02:39
Specs: Windows 7
I'm trying to determine if files written to a buffer directory are less than 5KB. My problem lies in that FORFILES outputs time in 12-hr format while echo'ing %time% outputs in 24-hr.

This snippet is where I think I'm running into an issue. (I'm getting the Missing Operand error, and I can't see where I'm going wrong... maybe I just need another set of eyes.)
[code]if NOT %%a==12 (
if %%d==PM (set /a hh=%%a+12) else (set /a hh=%%a)
) else (set /a hh=%%a)[/code]

This is the entire script.
[code]
REM Calls directory = %1
REM File size (bytes) = %2
REM Time check window (min) = %3

forfiles /p %1 /m *.wav /s /d -0 /c "cmd /c echo @fsize @ftime @fname" >> FSizes

echo ; > Problems

REM FORFILES adds an empty line at beginning of file. Remove it.
more +1 FSizes > FSizes2

for /f "tokens=1,2,3,4" %%i in (FSizes2) do (
REM Testing %%l

echo %%j:%%k> FileTimeRaw
for /f "tokens=1,2,3,4 delims=:" %%a in (FileTimeRaw) do (
if NOT %%a==12 (
if %%d==PM (set /a hh=%%a+12) else (set /a hh=%%a)
) else (set /a hh=%%a)
)
set /a mm=%%b
set filetime=%hh%%mm%
)

set /a timediff=%time:~0,2%%time:~3,2%-%filetime%

REM File is too old
if NOT %timediff% GTR %3 (

REM File is too big
if NOT %%i GTR %2 (

REM File is from yesterday
if NOT %timediff% LSS 0 (

echo SMALLFILE >> Problems
)
)
)
)

set x=0

for /f "eol=;" %%i in (Problems) do set /a x=%x%+1

echo %x%

:end
[/code]


See More: Problem converting 12-hr to 24-hr

Report •

#1
October 22, 2011 at 23:41:45
the bold bits are what i can see is wrong for that snippet

if NOT %%a==12 (
if %%d==PM (set /a hh=%%a+12) else (set /a hh=%%a)
) else (set /a hh=%%a)

now what is wrong? well you are trying to use the /a switch which in batch tells the set command that it is a "numerical expression" that needs evaluating
also in batch commands aren't case sensitive so NOT etc can be lowercase
the fixed code would be what is below if i am not mistaken

if not %%a==12 (
if %%d==PM (set /a hh=%%a+12) else (set hh=%%a)
) else (set hh=%%a)

good luck and reply if it works


Report •

#2
October 23, 2011 at 10:02:19
C:\working>(if NOT 12 == 12 (if PM == PM (set /a hh=12+12 ) else (set
hh=12 ) ) else (set hh=12 ) )
Missing operand.

C:\working>set /a timediff=1256-12%b
Divide by zero error.


Report •

#3
October 23, 2011 at 15:32:55
✔ Best Answer
if NOT %%a==12 (
if %%d==PM (set /a hh=%%a+12) else (set hh=%%a)
) else (set hh=%%a)

Another point to ponder... if FileTimeRaw contains hours %%a as a two digit number which is 08 or 09 then Set /a will fail because Set /a considers any number beginning with 0 (zero) to be Octal (base 8) therefore 08 and 09 are invalid. This rule holds true whenever Set /a is used.

Also set /a timediff=%time:~0,2%%time:~3,2%-%filetime%

If %time...% returns 0605 and %filetime% contains 0555 the actual time difference is 10 mins because mins must be calculated in base 60 but because Set /a does the calculation in decimal timediff will be set to 50. The hours (hh) should be calculated in base 24. Unless, of course, if you can accept the decimal result.

Good luck.


Please come back & tell us if your problem is resolved.


Report •

Related Solutions

#4
October 25, 2011 at 00:33:20
btw i suggest changing rem to echo because everyone likes echo

Report •

#5
October 28, 2011 at 10:02:58
Just to update, I got the script to work (mostly). A lot of my problems came from not enabling delayed variable expansion.

Wahine, I see what you're saying about the minutes. Oversight on my part. I converted the hours to minutes and added them to the minutes past the hour. Easier that way and gives the same result.

This is what I ended up with...

REM Calls directory = %1
REM File size (bytes) = %2
REM Time check window (min) = %3

setlocal EnableDelayedExpansion

forfiles /p %1 /m *.wav /s /d +0 /c "cmd /c echo @fsize @ftime @fname" >> FSizes

more +1 FSizes > FSizes2

for /f "tokens=1,2,3,4" %%i in (FSizes2) do (

set ftime=%%j
set ftime=!ftime:~0,-3!
set ftime=!ftime::=!
set mm=!ftime:~-2!
set hh=!ftime:~0,-2!
if !hh!==12 if %%k==AM set hh=0
if not !hh!==12 if %%k==PM set /a hh=!hh!+12

set /a filetime=!hh!*60+!mm!

if %time:~0,2%== 0 set nowhh=0
if %time:~0,2%== 1 set nowhh=1
if %time:~0,2%== 2 set nowhh=2
if %time:~0,2%== 3 set nowhh=3
if %time:~0,2%== 4 set nowhh=4
if %time:~0,2%== 5 set nowhh=5
if %time:~0,2%== 6 set nowhh=6
if %time:~0,2%== 7 set nowhh=7
if %time:~0,2%== 8 set nowhh=8
if %time:~0,2%== 9 set nowhh=9

if %time:~0,2% GTR 9 set /a timediff=%time:~0,2%*60+%time:~3,2%-!filetime!
if %time:~0,2% LEQ 9 set /a timediff=!nowhh!*60+%time:~3,2%-!filetime!

if NOT !timediff! GTR %3 (
if NOT %%i GTR %2 (
echo SMALLFILE >> Problems
)
)
)

if not exist Problems goto end

find /c "SMALLFILE" Problems | more +1 > Problems2

set /p x=<Problems2

echo %x:~21%

:end
del FSizes
del FSizes2
del Problems
del Problems2


Report •

Ask Question