Reputation: 21
I'm trying to create a batch file that generates a custom AVIsynth script per each file. Right now the batch file is set to execute from within the folder where the video files exist. What I need to do is get the creation time of the file to generate a timecode burn in. I have no problem getting the info I need. However, if the file was created in the afternoon I need it to be in 24hr time. For example, 2pm needs to display as 14.
I have a working if statement that creates a newth
variable that adds 12 if need be. However, if it doesn't need it the variable persists. On each subsequent iteration of the loop the variable doesn't change.
My example. I have two files the first was created at 2pm the other at 12pm. The 2pm file is read first and the newth
variable becomes 14. So far so good. On the next file the newth
variable should become 12 but instead remains 14. How do I fix this?
@Echo Off & CLS
SetLocal EnableDelayedExpansion
For /F %%a In ('dir *.mpg /b') Do (
ECHO Processing "%%a"
echo %%~ta
set time=%%~ta
set th=!time:~11,2!
set tm=!time:~14,2!
set era=!time:~17,2!
echo !era!
if "!era!"=="PM" (
if !th! LSS 12 ( set /a newth=!th!+12 )
) else ( set /a newth=!th!)
echo !newth!
echo //AviSynth Test Script >scripts/%%a.avs
echo DirectshowSource^("%%~fa"^)>>scripts/%%a.avs
echo LanczosResize^(720,400^) >>scripts/%%a.avs
echo ShowSMPTE^(^) >>scripts/%%a.avs
ECHO Back to Console
Pause
)
It's a little messy because I've been using echo
for debugging. But hopefully the problem is clear.
Upvotes: 2
Views: 6379
Reputation: 57252
here's how you can get the time stamp with seconds:
C:\>powershell -command "& {(gwmi -query """select * from cim_datafile where name = 'C:\\file.txt' """).lastModified;}"
C:\>powershell -command "& {(gwmi -query """select * from cim_datafile where name = 'D:\\other.txt' """).creationdate;}"
I've tried with WMIC
but still cannot get the time stamp.As you are using Win7 you should have powershell installed by default.
Upvotes: 0
Reputation: 79982
There are a few problems with your code. The major one is this sequence
if "!era!"=="PM" (
if !th! LSS 12 ( set /a newth=!th!+12 )
) else ( set /a newth=!th!)
With your first filetime "02:xx PM"
th
=02, era
=PM, so set /a newth=02+12
sets newth
=14
With your second filetime "12:xx PM"
th
=12, era
=PM, so - do nothing, since there's no else
action for !th! LSS 12
Hence, newth
remains at 14.
So - what's the fix? Since you don't use newth
further, we can't say for certain, but it appears you want 24-hour format - 4 digit hhmm
.
DANGER, Will Robinson moment number 1:
You are dealing with numbers starring LEADING ZEROES. All well and good except where the value is 08
or 09
, which batch bizarrely interprets as OCTAL since it begins 0
.
DANGER, Will Robinson moment number 2:
set /a
will suppress leading zeroes, so set /a newth=!th!
will set newth
to 7
for time 07:36 AM
- not 07
...
So - how to overcome all this?
IF !th!==12 SET th=00
SET th=!th: =0!
if "!era!"=="PM" (set /a newth=1!th!+12
SET newth=!newth:~-2!
) else ( set newth=!th!)
This forces 12 AM
to 00 AM
and 12 PM
to 00 PM
Then replace any spaces
with 0
(in case you have leading spaces, not zeroes)
Then, if era
is PM
, add 100 by stringing 1
before the 2-digit
hour number, add 12 and grab the last 2 characters
Otherwise, just use the number in th
Unfortunately, made a little more complicated since you haven't told us whether you use or don't use leading zeroes in your time format. Nevertheless, the incomplete original calculation method is at fault.
DANGER, Will Robinson moment number 3:
time
is a MAGIC VARIABLE - and you know what happened to Mickey when he got involved in things better left alone.
If you set time
in a batch, then %time%
or !time!
will return the value you set. If you don't set it, then the value returned will be the system time. Same goes for DATE
and a number of similar values (see set /?
from the prompt - there's a list at the end)
Upvotes: 1
Reputation: 41224
Here is a method with Wmic - Wmic is in XP pro and above.
@Echo Off & CLS
SetLocal EnableDelayedExpansion
For /F "delims=" %%a In ('dir *.mpg /b') Do (
ECHO Processing "%%a"
set "file=%cd%\%%a"
set "file=!file:\=\\!"
WMIC DATAFILE WHERE name="!file!" get creationdate|find ".">file.tmp
for /f %%a in (file.tmp) do set dt=%%a
set tm=!dt:~8,2!:!dt:~10,2!:!dt:~12,2!
del file.tmp
echo !tm!
echo //AviSynth Test Script >scripts/%%a.avs
echo DirectshowSource^("%%~fa"^)>>scripts/%%a.avs
echo LanczosResize^(720,400^) >>scripts/%%a.avs
echo ShowSMPTE^(^) >>scripts/%%a.avs
ECHO Back to Console
Pause
)
Upvotes: 1