how to get modified time of the file in batch file DOS

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sonalir
Posts: 4
Joined: 22 Aug 2012 04:08

how to get modified time of the file in batch file DOS

#1 Post by sonalir » 22 Aug 2012 04:16

>for %a in (a.txt) do set FileDate=%~ta ----gives date and time
>set FileDate=08/21/2012 02:36 PM

which is modified date and time

>for %a in (a.txt) do set FileDate=%FileDate:~0,10%-- gives date only
>set FileDate=08/21/2012

but i need only time
how to get it?

Please help

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

Re: how to get modified time of the file in batch file DOS

#2 Post by foxidrive » 22 Aug 2012 04:54

Try this:

for %a in (a.txt) do set filedate=%~ta
echo %FileDate:~-8%

sonalir
Posts: 4
Joined: 22 Aug 2012 04:08

Re: how to get modified time of the file in batch file DOS

#3 Post by sonalir » 22 Aug 2012 05:21

I got with
ECHO %FileDate:~11%

any ways

Thanks a lot

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: how to get modified time of the file in batch file DOS

#4 Post by Squashman » 22 Aug 2012 06:09

sonalir wrote:I got with
ECHO %FileDate:~11%

any ways

Thanks a lot

Either way will work.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: how to get modified time of the file in batch file DOS

#5 Post by Squashman » 22 Aug 2012 06:34

If you want to go the Math route you can break up your file date into variables.

Code: Select all

@echo off
for %%a in (myfile.txt) do (
   for /f "tokens=1-4 delims=: " %%G in ('echo %%~ta') do (
      set date=%%G
      set hour=%%H
      set min=%%I
      set ampm=%%J
   )
)
set date
set hour
set min
set ampm
pause

Remember that you will need to remove the leading zero from hours 1 through 9.

Post Reply