Format datetime from a files modified date

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
daillest319
Posts: 27
Joined: 31 Jan 2012 14:45

Format datetime from a files modified date

#1 Post by daillest319 » 29 Aug 2013 13:25

Is there anyway to format the datetime from the modified date of a file to the format below i tried multiple things but have had no luck. What im trying to accomplish here is to copy any files greater than or less than a date range a user inputs in the following format 120101 120731.
YYMMDDstart YYMMDDend



currently the date modiified displays like so..
1/1/2013 7:26 AM

I need it to be in this format -> YYMMDD (130101)



Code: Select all

@echo on
setlocal EnableDelayedExpansion


if "%2" neq "" goto begin
echo Usage: %0 YYMMDDstart YYMMDDend
echo/
echo For example: %0 120101 120731
rem set /p DT="Enter Start Date And End Date: "
rem %0 %DT%
rem hardcoded date for example
%0 120101 120731
goto :EOF


:begin

for %%f in ("F:\TEST & sample\sampleh\Final\test\*tse.*") do (
   for /F "tokens=1-4 delims=." %%a in ("%%f") do (
      set filedatetime=%%~tF
    if %%~tf geq %1 if %%~tf leq %2 (
     pause
         copy /Y "%%f" "F:\TEST & sample\sampleh\Final\copiedfiles\"
      )
   )
)


Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Format datetime from a files modified date

#2 Post by Endoro » 29 Aug 2013 14:37

you can try this:

Code: Select all

@echo off & SETLOCAL
SET "var=1/1/2013 7:26 AM"
FOR /f "tokens=1-3delims=/ " %%a IN ("%var%") DO (
   SET "month=0%%a"
   SET "day=0%%b"
   SET "year=%%c"
)
SET "month=%month:~-2%"
SET "day=%day:~-2%"
SET "year=%year:~-2%"
ECHO %year%%month%%day%

I don't know if the month is on the first or second place in your date.
Please modify it by yourself.

daillest319
Posts: 27
Joined: 31 Jan 2012 14:45

Re: Format datetime from a files modified date

#3 Post by daillest319 » 30 Aug 2013 06:49

For some reason on my code below. i cant save %%~tF as a variable. which is grabbing the date modifeid date and time of the file. is there something im missing inorder to set it as a variable?

like so...
set filedatetime=%%~tF

as of right now if i echo %filedatetime% it comes up blank. this is the reason why i cant format it.

npocmaka_
Posts: 516
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: Format datetime from a files modified date

#4 Post by npocmaka_ » 30 Aug 2013 07:33

with

Code: Select all

WMIC DATAFILE WHERE name="C:\\somefile" get LastModified


you'll get the modification date in format

YYYYMMDDHHMMSS.millliseconds (e.g. 20130730153904.211738+180)


and you'll need just to parse it.
The result does not depend on the time settings.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Format datetime from a files modified date

#5 Post by foxidrive » 30 Aug 2013 08:16

daillest319 wrote:like so...
set filedatetime=%%~tF


Context is important.

Echoing within a loop, for example, will work with delayed expansion.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Format datetime from a files modified date

#6 Post by penpen » 30 Aug 2013 12:32

daillest319 wrote:For some reason on my code below. i cant save %%~tF as a variable. which is grabbing the date modifeid date and time of the file. is there something im missing inorder to set it as a variable?

like so...
set filedatetime=%%~tF

as of right now if i echo %filedatetime% it comes up blank. this is the reason why i cant format it.
And in addition to using delayed expansion you should notice that the variable names of a for loop are case sensitive,
so the information you want to get is not stored within the variable %%F.
You should use %%f, so it seems that %%~tf is what you wanted to use.

penpen

Post Reply