List the files with in two datetime
Moderator: DosItHelp
List the files with in two datetime
Hi all,
I want to display the files between two datetimes (YYYYMMDDHHmmss).
Tried using two commands
1.
set newerthan="2015-03-05 11:26:00"
set olderthan="2015-03-10 16:55:00"
find f:\new\* -type 'f' -newermt "$newerthan" ! -newermt "$olderthan"
2. @echo off
setlocal
set "drive=%~d1"
set "folder=%~pnx1\"
set /a "start=%~2, end=%~3"
wmic datafile where "drive='%drive%' and path='%folder:\=\\%' and creationdate>'%start%' and creationdate<'%end%'" get creationdate, name, size
Able to display the files with in two dates (YYYYMMDD). But it is not considering the time.
Any help. Thanks in advance
I want to display the files between two datetimes (YYYYMMDDHHmmss).
Tried using two commands
1.
set newerthan="2015-03-05 11:26:00"
set olderthan="2015-03-10 16:55:00"
find f:\new\* -type 'f' -newermt "$newerthan" ! -newermt "$olderthan"
2. @echo off
setlocal
set "drive=%~d1"
set "folder=%~pnx1\"
set /a "start=%~2, end=%~3"
wmic datafile where "drive='%drive%' and path='%folder:\=\\%' and creationdate>'%start%' and creationdate<'%end%'" get creationdate, name, size
Able to display the files with in two dates (YYYYMMDD). But it is not considering the time.
Any help. Thanks in advance
Re: List the files with in two datetime
This DosTips question looks to be related to this StackOverflow question.
And here is my StackOverflow answer.
Working with timestamps in WMI queries is a pain. You must either specify a date in the format 'YYYYMMDD', or date and time in the format 'YYYYMMDDhhmmss.ffffffSZZZ'
Where
When expressing a date, the string must be exactly 6 characters long.
For example, January 15, 2015 is expressed as '20150115'.
When expressing a date-time, the string must be exactly 25 characters long.
For example, January 15, 2015, 3:14 PM Eastern Standard Time is expressed as '20150115151400.000000-300'.
So your existing query does not have to change. You just need to make sure your %start% and %end% strings are properly formatted date-time values.
Dave Benham
And here is my StackOverflow answer.
Working with timestamps in WMI queries is a pain. You must either specify a date in the format 'YYYYMMDD', or date and time in the format 'YYYYMMDDhhmmss.ffffffSZZZ'
Where
- YYYY = full four digit year
- MM = two digit month
- DD = two digit day of month
- hh = two digit hours in 24 hour format
- mm = two digit minutes
- ss = two digit seconds
- .ffffff = six digit fractional seconds (microseconds)
- SZZZ = time zone expressed as minutes offset from UTC time
- S = time zone sign, either + or -
- ZZZ = three digit minutes
- S = time zone sign, either + or -
When expressing a date, the string must be exactly 6 characters long.
For example, January 15, 2015 is expressed as '20150115'.
When expressing a date-time, the string must be exactly 25 characters long.
For example, January 15, 2015, 3:14 PM Eastern Standard Time is expressed as '20150115151400.000000-300'.
So your existing query does not have to change. You just need to make sure your %start% and %end% strings are properly formatted date-time values.
Dave Benham
Re: List the files with in two datetime
Thanks Dave for your reply.
I tried by giving DateTime as 20150422102030.000000-300, but I got the error as
"invalid number. numbers are limited to 32-bits of precision"
Could you please let me know any other alternate solution.
Thanks,
I tried by giving DateTime as 20150422102030.000000-300, but I got the error as
"invalid number. numbers are limited to 32-bits of precision"
Could you please let me know any other alternate solution.
Thanks,
Re: List the files with in two datetime
Don't use SET /A when defining your start and end values.
The value is a string, so you must use the simple SET command
Dave Benham
The value is a string, so you must use the simple SET command
Dave Benham
Re: List the files with in two datetime
Another much simpler (and faster!) approach than WMIC would be this one:
For example:
Yes, I know that this method don't include the seconds and it 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. However, this solution may be enough for a certain number of users...
Antonio
Code: Select all
@echo off
setlocal EnableDelayedExpansion
if "%~2" neq "" goto begin
echo Usage: %0 YYYYMMDDHHMMstart YYYYMMDDHHMMend
goto :EOF
:begin
for %%F in (*.*) do (
for /F "tokens=1-6 delims=/: " %%a in ("%%~TF") do (
set "hour=%%d"
if "%%f" equ "p.m." set /A "hour=(1%%d+12)%%100"
set "fileDate=%%c%%a%%b!hour!%%e"
)
if "!fileDate!" geq "%~1" if "!fileDate!" leq "%~2" echo %%~TF %%F %%~ZF
)
For example:
Code: Select all
example.bat 201503051126 201503101655
Yes, I know that this method don't include the seconds and it 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. However, this solution may be enough for a certain number of users...
Antonio
Re: List the files with in two datetime
@Aacini - you are looking at last modified dates, but ani is looking at creation dates.
Dave Benham
Dave Benham
Re: List the files with in two datetime
You are right Dave. This point is fixed in the code below:
Perhaps a small adjustment may be required in the "tokens=1-7*" part, depending on the locale.
Antonio
Code: Select all
@echo off
setlocal EnableDelayedExpansion
if "%~2" neq "" goto begin
echo Usage: %0 YYYYMMDDHHMMstart YYYYMMDDHHMMend
goto :EOF
:begin
for /F "skip=5 tokens=1-7* delims=/: " %%a in ('dir /A-D /T:C *.*') do (
if "%%h" equ "" goto break
set "hour=%%d"
if "%%f" equ "p.m." set /A "hour=(1%%d+12)%%100"
set "fileDate=%%c%%a%%b!hour!%%e"
if "!fileDate!" geq "%~1" if "!fileDate!" leq "%~2" echo %%a/%%b/%%c %%d:%%e %%f %%g %%h
)
:break
Perhaps a small adjustment may be required in the "tokens=1-7*" part, depending on the locale.
Antonio
Re: List the files with in two datetime
Thanks Dave & Antonio.
I tried to remove /A, but I am getting
"Invalid alias Verb".
I need a single command to execute at run prompt and list the files.
Thanks,
I tried to remove /A, but I am getting
"Invalid alias Verb".
I need a single command to execute at run prompt and list the files.
Thanks,
Re: List the files with in two datetime
Only SET /A can assign multiple numeric values with one statement.
You must use a separate SET statement for each string assignment.
Dave Benham
You must use a separate SET statement for each string assignment.
Code: Select all
set "start=%~2"
set "end=%~3"
Dave Benham
Re: List the files with in two datetime
As we are restricted to save .bat files in the client system,
we need to use a single line command which run at the cmd prompt to list the files with in the two datetime
Thanks
we need to use a single line command which run at the cmd prompt to list the files with in the two datetime
Thanks
Re: List the files with in two datetime
Need to list the files using the below command with in the datetime
wmic datafile where "drive='f:' and path='\\xyx' and lastmodified>20150219023402 and lastmodified<20150422120099" get creationdate, name,size
This command is not working. Could any one make changes to the existing command
Thanks
wmic datafile where "drive='f:' and path='\\xyx' and lastmodified>20150219023402 and lastmodified<20150422120099" get creationdate, name,size
This command is not working. Could any one make changes to the existing command
Thanks
Re: List the files with in two datetime
The suggestion I made had nothing to do with changing the command line - it was strictly a change to the batch script itself.
Using all the same variables as you were before
Eliminating most of the variables, only preserving the one needed to double up the back slashes.
Both scripts above are called exactly as you have been doing, except you must make sure the 2nd and 3rd parameters are properly formatted date-times
Assuming the batch script is named test.bat, then here is an example of how it is invoked. I have tested both scripts with this command line below, and they work :
Dave Benham
Using all the same variables as you were before
Code: Select all
@echo off
setlocal
set "drive=%~d1"
set "folder=%~pnx1\"
set "start=%~2"
set "end=%~3"
wmic datafile where "drive='%drive%' and path='%folder:\=\\%' and creationdate>'%start%' and creationdate<'%end%'" get creationdate, name, size
Eliminating most of the variables, only preserving the one needed to double up the back slashes.
Code: Select all
@echo off
setlocal
set "folder=%~pnx1\"
wmic datafile where "drive='%~d1' and path='%folder:\=\\%' and creationdate>'%~2' and creationdate<'%~3'" get creationdate, name, size
Both scripts above are called exactly as you have been doing, except you must make sure the 2nd and 3rd parameters are properly formatted date-times
Assuming the batch script is named test.bat, then here is an example of how it is invoked. I have tested both scripts with this command line below, and they work :
Code: Select all
test c:\test 20150101000000.000000-300 20150228132313.983674-300
Dave Benham
Re: List the files with in two datetime
Thanks Dave for you reply.
But i dont want to create any .bat file.
Just use the command at the Command Prompt
c:\Users\Desktop> wmic datafile where "drive='f:' and path ='\\xyz\\' and creationdate>20140502233423 and creationdate< 20150423233445" get creationdate
Thanks
But i dont want to create any .bat file.
Just use the command at the Command Prompt
c:\Users\Desktop> wmic datafile where "drive='f:' and path ='\\xyz\\' and creationdate>20140502233423 and creationdate< 20150423233445" get creationdate
Thanks
Re: List the files with in two datetime
ani wrote:Thanks Dave for you reply.
But i dont want to create any .bat file.
Just use the command at the Command Prompt
c:\Users\Desktop> wmic datafile where "drive='f:' and path ='\\xyz\\' and creationdate>20140502233423 and creationdate< 20150423233445" get creationdate
Thanks
Then why did your original question ask for help on correcting a batch script
Your tone has been respectful, and I appreciate that. But it is very irritating when a person asking for help is not clear on what he/she is looking for, and constantly changes his/her mind
Also, try to take more time to think about the answers already provided. I have already given you all the information you need to answer your latest question.
Your original post had fairly sophisticated batch code, so I assumed you were experienced and could follow my terse answers. It appears I misinterpreted your experience level. Sorry about that. I'm assuming someone else must have written that original code.
As I have already pointed out (look at my first answer), you must properly format the date-time to be exactly 25 characters long, including the fractional seconds to exactly 6 decimal places. And you must also adjust for your local time zone. And the date-times must be treated as strings with single quotes around them.
I'll assume you are in my time zone, which is the U.S. Eastern time zone, so both date-times would be in Eastern Daylight Time (-240). Obviously you would need to substitute your actual local time zone adjustments.
Code: Select all
c:\Users\Desktop> wmic datafile where "drive='f:' and path ='\\xyz\\' and creationdate>'20140502233423.000000-240' and creationdate<'20150423233445.000000-240'" get creationdate
Once you understand these answers, I'd appreciate it if you go back to your original StackOverflow question and accept my answer. Thanks.
Dave Benham
Re: List the files with in two datetime
ani wrote:Thanks Dave for you reply.
But i dont want to create any .bat file.
Just use the command at the Command Prompt
Do you want a one-liner for the Command Prompt? Here it is:
Code: Select all
cmd /V:ON /C "for /F "skip=5 tokens=1-7* delims=/: " %a in ('dir /A-D /T:C') 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 "201301010000" if "!f!" leq "201312310000" echo %a/%b/%c %d:%e %f %g %h"
You may use previous line in a simpler way if you define a DOSKEY MACRO with it. To do that, copy the following in a file called BETWEEN.TXT:
Code: Select all
between=cmd /V:ON /C "for /F "skip=5 tokens=1-7* delims=/: " %a in ('dir /A-D /T:C $3') 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 "$1" if "!f!" leq "$2" echo %a/%b/%c %d:%e %f %g %h"
Then, install the macro executing the following line:
Code: Select all
doskey /MACROFILE=BETWEEN.TXT
After that you may list the desired files entering "between startDate endDate [folder]"; both startDate and endDate must be given in YYYYMMDDHHMM format and the third optional parameter is the folder. For example:
Code: Select all
C:\> between 201301010000 201331120000
17/05/2013 07:57 p.m. 566 AsyncInput.bat
25/04/2013 08:35 p.m. 1,200 CopyRename.bat
06/02/2013 09:35 p.m. 847 FixedPointArit.bat
12/05/2013 04:42 p.m. 2,154 GetDateTime.bat
12/05/2013 07:57 p.m. 817 GetDateTime.txt
18/05/2013 02:04 p.m. 1,336 Permutations.bat
18/05/2013 05:10 p.m. 690 PositionsNbaseM.bat
03/05/2013 05:03 p.m. 780 SelectFolder.bat
Antonio
PS - Of course, you may also define a DOSKEY macro to use wmic in a simpler way...