List the files with in two datetime

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
ani
Posts: 10
Joined: 20 Apr 2015 09:42

Re: List the files with in two datetime

#16 Post by ani » 24 Apr 2015 02:09

Thanks Antonio & Dave.

cmd /V:ON /C "for /F "skip=5 tokens=1-7* delims=/: " %a in ('dir /A-D /T:C f:\ani\*.*') do @set h=%d&(if %f equ p.m. set /A "h=(1%d+12-100)">NUL)&set f=%c%b%a!h!%e&if "!f!" geq "20140304034534" if "!f!" leq "20150321231232" echo %a/%b/%c %d:%e %f %g %h"
This command is good, but it is not considering the Time. Please let me know how to use AM/PM timings. Is it 24hr or 12Hr.
Is there any way to check the files in subfolders also. I am using *.* but it is not checking the files in subfolders.

@Dave, Sorry for confusion. Initially we were asked to write batch file , later came to know that we will not be having the permissions to save the files in the client location. So we were trying for single line command :)

Thanks

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

Re: List the files with in two datetime

#17 Post by npocmaka_ » 24 Apr 2015 02:22

Will be much easier with powershell if one-liner is required....

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

Re: List the files with in two datetime

#18 Post by Squashman » 24 Apr 2015 06:49

ani wrote:Thanks Antonio & Dave.

cmd /V:ON /C "for /F "skip=5 tokens=1-7* delims=/: " %a in ('dir /A-D /T:C f:\ani\*.*') do @set h=%d&(if %f equ p.m. set /A "h=(1%d+12-100)">NUL)&set f=%c%b%a!h!%e&if "!f!" geq "20140304034534" if "!f!" leq "20150321231232" echo %a/%b/%c %d:%e %f %g %h"
This command is good, but it is not considering the Time. Please let me know how to use AM/PM timings. Is it 24hr or 12Hr.
Is there any way to check the files in subfolders also. I am using *.* but it is not checking the files in subfolders.

@Dave, Sorry for confusion. Initially we were asked to write batch file , later came to know that we will not be having the permissions to save the files in the client location. So we were trying for single line command :)

Thanks

Certainly looks like the code is taking into consideration the time. Can see that plain as day.

Code: Select all

@set h=%d&(if %f equ p.m. set /A "h=(1%d+12-100)"


As far as saving files, you will have to at some point have this code saved to a file. You can't tell me you are going to memorize this!

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: List the files with in two datetime

#19 Post by Aacini » 27 Apr 2015 06:54

ani wrote:Thanks Antonio & Dave.

cmd /V:ON /C "for /F "skip=5 tokens=1-7* delims=/: " %a in ('dir /A-D /T:C f:\ani\*.*') do @set h=%d&(if %f equ p.m. set /A "h=(1%d+12-100)">NUL)&set f=%c%b%a!h!%e&if "!f!" geq "20140304034534" if "!f!" leq "20150321231232" echo %a/%b/%c %d:%e %f %g %h"
This command is good, but it is not considering the Time. Please let me know how to use AM/PM timings. Is it 24hr or 12Hr.
Is there any way to check the files in subfolders also. I am using *.* but it is not checking the files in subfolders.



A couple points here:

  • As said before, "this method is locale dependant, although a simple change in "%%c%%a%%b" order for the appropriate one (like "%%c%%b%%a") would be enough to fix this point... Perhaps a small adjustment may be required in the "tokens=1-7*" part". The method assume that the output of DIR command have the following format (as shown in the output example):

    Code: Select all

    18/05/2013  02:04 p.m.             1,336  Permutations.bat
    DD/MM/YYYY  HH:MM ?.m.              size  filename

    If the output of DIR command is different in your computer ("locale dependant"), the line requires a small adjustment; for example, if the date order is MM/DD/YYYY or the AM/PM indicator have a space, like in "p. m.".

  • As said before, "this method don't include the seconds... both startDate and endDate must be given in YYYYMMDDHHMM format" as shown in the example: C:\> between 201301010000 201331120000. Try:

    Code: Select all

    if "!f!" geq "201403040345" if "!f!" leq "201503212312" 

  • If you want to check files in subfolders, use /S switch in DIR command, like in: 'dir /A-D /T:C /S f:\ani'.

If after check previous points you still don't get the desired output, insert an ECHO command before the second IF, like in: &ECHO if "!f!" geq "201403040345" ..., so we may review what exactly are the processed values.

Antonio

ani
Posts: 10
Joined: 20 Apr 2015 09:42

Re: List the files with in two datetime

#20 Post by ani » 27 Apr 2015 10:18

Thanks Antonio.
My Cmd and output is a below
cmd /V:ON /C "for /F "skip=5 tokens=1-7* delims=/: " %a in ('dir /s /A-D /T:C f:\ani\*.*') do @set "hour=%d"&(if "%f" equ "p.m." set /A
"hour=(1%d+12)%100")&set fileDate=%c%a%bhour%e&echo if "!fileDate!" geq "20140305122323" if "!fileDate!" leq "20150421150909" echo %a/%b/%c %d:%e %f %g %h"

The output is
if "20150421hour26" geq "20140305122323" if "20150421hour26" leq "20150421150909" echo 04/21/2015 12:26 PM 14,865 Address.docx
if "20150420hour17" geq "20140305122323" if "20150420hour17" leq "20150421150909" echo 04/20/2015 03:17 PM 56,534 Feb-2015.png

Why I am getting filedate as "20150421hour26", actually the file date is 20150421 12:26 PM.
May be for this reason the condition is not checking datetime.

Thanks

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

Re: List the files with in two datetime

#21 Post by Squashman » 27 Apr 2015 13:06

ani wrote:Why I am getting filedate as "20150421hour26", actually the file date is 20150421 12:26 PM.

Because of this code.

Code: Select all

set fileDate=%c%a%bhour%e
The variable hour is not expanding because it is missing the exclamation points for the delayed expansion.

ani
Posts: 10
Joined: 20 Apr 2015 09:42

Re: List the files with in two datetime

#22 Post by ani » 05 May 2015 00:09

Hi,

cmd /V:ON /C "for /F "tokens=1-7* delims=/: " %a in ('dir /s /A-D /T:C f:\ani\') do @set fileDate=%c%a%b%d%e&if "!fileDate!" geq "201403050101" if "!fileDate!" leq "201504211209" echo %a/%b/%c %d:%e %f %g %h"

The output of the above command is

04/20/2015 03:17 PM 45,23 abc.docx
04/21/2015 02:08 PM 34 xyz.png

But the end date is 201504211209, it should not display xyz.png because its time is 02:08 PM which is greater than 1209

Could any one help me out how to handle the Time.

Thanks

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: List the files with in two datetime

#23 Post by Aacini » 06 May 2015 00:10

I don't understand why you modify the posted code and then you ask why it don't works, or why you don't follow the indications given. It is difficult to help you when you can't follow very simple indications. Anyway, I think this is the right code for you:

Code: Select all

cmd /V:ON /C "for /F "skip=5 tokens=1-7* delims=/: " %a in ('dir /S /A-D /T:C f:\ani') do @set hour=%d&(if %f equ PM set /A "hour=(1%d+12-100)">NUL)&set fileDate=%c%a%b!hour!%e&if "!fileDate!" geq "201403050101" if "!fileDate!" leq "201504211209" echo %a/%b/%c  %d:%e %f  %g  %h"

The core problem was that the AM/PM indication in your computer use upper case letters with no points, like in PM, but my original code use lower case letters with points, like in p.m..

If you still have problems with this code, you must post the code enclosed between code tags, that is, enter the word "code" (without quotes) enclosed in square braquets in a line, followed by the code and after it insert a line with "/code" (without quotes) enclosed in square braquets. Do the same thing for the output sample.

I am sorry, but I will not answer future replies from you if you don't include the code used, that must be the same code posted above, or if you don't include an explanation of the changes you did vs. the posted code.

Antonio

ani
Posts: 10
Joined: 20 Apr 2015 09:42

Re: List the files with in two datetime

#24 Post by ani » 06 May 2015 01:45

Thanks Antonio. Problem is solved.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: List the files with in two datetime

#25 Post by dbenham » 06 May 2015 05:07

@Aacini - Amen brother. I gave up long ago.

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

Re: List the files with in two datetime

#26 Post by Squashman » 07 May 2015 11:56

Aacini wrote:I don't understand why you modify the posted code and then you ask why it don't works, or why you don't follow the indications given. It is difficult to help you when you can't follow very simple indications.
+1

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: List the files with in two datetime

#27 Post by Aacini » 07 May 2015 16:07

@dbenham, @Squashman: thank you, guys! :)

Antonio

@ani: may I ask you to select my SO answer?

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

Re: List the files with in two datetime

#28 Post by foxidrive » 08 May 2015 07:44

The world has changed, hasn't it?

You help people freely from your spare time - and so often they don't reply.

Too often they post the same question on multiple forums - and so often they don't reply.

If asked for information - so often they don't give you any,
or argue that you don't need any information (which is ironic seeing they don't have the skills to solve their task, yet suddenly they are experts on what is needed to solve that same task).


Is it because the question-askers are younger people and so many of them don't know how to use the words please and thank you?

Or am I just an old fart and want them 'off my lawn'?

Just an idle comment here - it's a rhetorical question really.

brinda
Posts: 78
Joined: 25 Apr 2012 23:51

Re: List the files with in two datetime

#29 Post by brinda » 08 May 2015 07:55

foxidrive wrote:The world has changed, hasn't it?

You help people freely from your spare time - and so often they don't reply.

Too often they post the same question on multiple forums - and so often they don't reply.

If asked for information - so often they don't give you any,
or argue that you don't need any information (which is ironic seeing they don't have the skills to solve their task, yet suddenly they are experts on what is needed to solve that same task).


Is it because the question-askers are younger people and so many of them don't know how to use the words please and thank you?

Or am I just an old fart and want them 'off my lawn'?

Just an idle comment here - it's a rhetorical question really.


it is always the new hire or younger one in our workplace. gratitude is hidden in their dictionary but at the same time we learn again and again on humanity and fall into the same process afterwards :D

Post Reply