1.

Solve : how to count all files from a perticular directory according to there date & tim?

Answer»

how to count all files from a perticular directory according to there date & time.. Can anyone have solution for this
Currently i am able to count files but not by there specific (user defined) date & time..... I want such code in batch file.... What do you mean "according to date and time"? Do you mean count all the files earlier than or later than a specified date and time? Or all those that have a certain date? Or do not? Please state your requirement more clearly. And state your local date and time format.
Salmon Trout
: Yes i want to search files between to specific data like from 20 Sept 2011 to 22 Sept 2011.....!! & Date format is mm/dd/yyyy & time format is 12Hrs clock..
And one more thing which i forgot to mention is that - i need such code where i can search n no of directories...
CarlCarson
Thanku you Carl however given syntax is not satisfying my need.

please give example of date and time format used in dir like these

09/22/2011 19:46

06/21/2004 05:39 AM

or describe thus

dd/mm/yyyy HH:MM:SS

I require to know if time format includes leading zero for hour less than 10 e.g. 08 and if seconds are shown.
One more question, is the date in the title or are you going off of the date created stored in the file attributes? I think they will both follow the same basic coding, but pulling the information in would be different. Also, if you are pulling the date from the title of the file, is a standard naming CONVENTION used. i.e. Do all of the files start with mm/dd/yyyy title blah.txt?

The basics of the code would create a numeric value out of the date and then compare.
Code: [Select][this should be set within a FOR command]
set FileDate=[however date is defined]
set /a NumYears=!FileDate:~6,4!*365
set /a NumMonths=!FileDate:~0,2!*30
set /a FileDateCheck=!NumYears!+!NumMonths!+!FileDate:~3,2!

[use above calculations to determine the check against numbers for the "threshold" dates]

if !FileDateCheck! lss !HiThresh! (
if !FileDateCheck! gtr !LoThresh! (
set /a NumFiles=!NumFiles!+1
)
)

[after the FOR command ends]
echo %NumFiles%

I probably made that way more complicated than it needs to be, but it does work. There are probably ways for batch to already see dates numerically and compare them accurately, but I have never delved into that so I don't know.

Also, TAKE a look at the variables within variables thread that I started. The final code that I submitted should show the basics on the FOR command that you would want to use for something like this (as far as selecting different directories).

In order to search multiple directories, would the directories be predefined or would you like to input the directories each time you run the program? Please give as much information on what you are trying to do as possible, as Salmon Trout can attest to the fact that it is very difficult to perform psychic debugging (I'm pretty sure it was you who said that or was told that in a previous thread) .Quote from: Raven19528 on September 23, 2011, 12:42:29 AM

if you are pulling the date from the title of the file, is a standard naming convention used. i.e. Do all of the files start with mm/dd/yyyy title blah.txt?

I have a suspicion that they don't... of course it would be much easier if they did.

Quote
as Salmon Trout can attest to the fact that it is very difficult to perform psychic debugging (I'm pretty sure it was you who said that or was told that in a previous thread

It is indeed, but to be fair (I know I've ranted about TLI before) many questioners just plain don't know what information is required. Also a complication is that many people who post answers about batch date/time stuff are under the (grossly) mistaken impression that the whole world uses US format dates and times and that therefore the same chunks of string slicing code will always work regardless.

We don't yet know which date/time the OP wants to use: creation, last access, or last written. I would not bother with last access for various reasons (not enabled on every system, notoriously slow to be updated). The only one I think you can be sure of getting access to is modification time. Not all file systems can record creation and last access times, and not all file systems record them in the same manner. For example, the resolution of create time on FAT is 10 milliseconds, while write time has a resolution of 2 seconds and access time has a resolution of 1 day, so it is REALLY the access date. The NTFS file system delays updates to the last access time for a file by up to 1 hour after the last access.

Anyhow what I would seek to do is get a file date and time as a string YYYYMMDD which could then be treated as a number and therefore used for lss leq equ geq gtr numerical comparisons so that

09/23/2011 would process to 20110923 which is easy enough to implement in batch. If you want HH:MM (and :SS) (and milliseconds!) as well, batch arithmetic won't cut it because you only get 32 bits of precision. So my question to the OP about time format was a waste of time really. Personally I would be using something else like VBScript or Powershell for this.

However I have a distinct feeling that the OP doesn't just want pointers on what to do in their batch file they are going to write, rather they want some person to write the thing for them and then debug it by the psychic method...

Code: [Select]@echo off
setlocal enabledelayedexpansion

set date1=20050601
set date2=20050731

echo Searching for files dated

echo from %date1:~0,4% %date1:~4,2% %date1:~6,2%
echo to %date2:~0,4% %date2:~4,2% %date2:~6,2%

REM Topmost folder to search
cd /d c:\batch

REM /tc creation date
REM /ta last access date
REM /tw last written date

for /f "delims=" %%A in ('dir /b /s /tm') do (

REM Get full drive and path
set filename=%%~dpnxA

REM Get file date
set filedate=%%~tA

REM Uncomment next line if date format is DD/MM/YYYY
REM set filedatenumber=!filedate:~6,4!!filedate:~0,2!!filedate:~3,2!

REM Uncomment next line if date format is DD/MM/YYYY
REM set filedatenumber=!filedate:~6,4!!filedate:~3,2!!filedate:~0,2!

if !filedatenumber! geq %date1% (
if !filedatenumber! leq %date2% (
echo %%~tA %%~dpnxA
)
)
)

Code: [Select]Searching for files dated
from 2005 06 01
to 2005 07 31
06/06/2005 20:56 c:\Batch\999nums.btm
10/06/2005 00:07 c:\Batch\bass1.cmd
03/07/2005 20:35 c:\Batch\dvdburn1a.bat
03/07/2005 21:16 c:\Batch\dvdburn2.bat
04/07/2005 17:49 c:\Batch\dvdburn3.bat
27/07/2005 17:29 c:\Batch\INSTALL.LOG
01/06/2005 18:36 c:\Batch\jt-new.btm
23/06/2005 17:41 c:\Batch\jt-url.btm
21/06/2005 17:31 c:\Batch\looptest.btm
09/06/2005 23:05 c:\Batch\mininova-sfu.cmd
01/06/2005 18:36 c:\Batch\newsurls.txt
09/07/2005 15:52 c:\Batch\nopix.btm
24/07/2005 16:43 c:\Batch\RenameAsNumbers.bat
09/07/2005 15:59 c:\Batch\subhide.btm
10/06/2005 00:08 c:\Batch\testme.bas
08/07/2005 20:57 c:\Batch\urlget.bat
07/07/2005 20:54 c:\Batch\number-txt\999nums.btm
03/07/2005 00:50 c:\Batch\tempdir\readme.txt
03/07/2005 00:50 c:\Batch\tempdir\shmnview.chm
03/07/2005 00:24 c:\Batch\tempdir\shmnview.exe
For a GUI approach, I use a free tool called Agent Ransack which offers some advantages over Windows Search


Modularizing your programs, even if it is just using some "::" labels in batch can also help in the long run, as many times you will find that programs you write will have similar components.

Code: [Select]@echo off
setlocal enabledelayedexpansion
set origdir=%~dp0
set NumFiles=0

::Find out the dates you want to find between or use default values
set /p date1=Enter beginning date of search parameters in mm/dd/yyyy format. {default is 01/01/1900} -
if "%date1%"=="" set date1=01/01/1900
set /p date2=Enter ending date of search parameters in mm/dd/yyyy format. {default is 12/31/4567} -
if "%date2%"=="" set date2=12/31/4567

::Set those dates up properly
set date1=%date1:~6,4%%date1:~0,2%%date1:~3,2%
set date2=%date2:~6,4%%date2:~0,2%%date2:~3,2%

::Determine directories
:loop
cls
set /p tardir=Enter full path name of the target directory-
echo %tardir% >> paths.txt
cls
set /p yn1=Would you like to enter another target directory? {y.n}
if /i "%yn1%"=="y" goto loop
cls

::Embedded FOR commands allows the program to process all directories at once
::Using most of Salmon Trout's FOR command with minor adjustments
for /f "delims=" %%B in (paths.txt) do (
cd %%B
for /f "delims=" %%A in ('dir /b /s /tm') do (

REM Get full drive and path
set filename=%%~dpnxA

REM Get file date
set filedate=%%~tA

REM Uncomment next line if date format is DD/MM/YYYY
REM set filedatenumber=!filedate:~6,4!!filedate:~0,2!!filedate:~3,2!

REM Uncomment next line if date format is DD/MM/YYYY
REM set filedatenumber=!filedate:~6,4!!filedate:~3,2!!filedate:~0,2!

if !filedatenumber! geq %date1% (
if !filedatenumber! leq %date2% (
echo %%~tA %%~dpnxA >> %origdir%\Results.txt
set /a NumFiles=!NumFiles!+1
)
)
)
)

::Show numerical results, delay, show Results file
cls
echo The number of files in selected directories that match search parameters is %NumFiles%
ping 1.1.1.1 -n 1 -w 3000>NUL ::this is my delay string, others may have different ways they institute a delay
type %origdir%\Results.txt
pause
You have done a great deal to add flesh to the skeleton that I posted, just a couple of things...

1. I made a mistake by putting identical comments before the alternative string slicing lines in the loop

see the fix...

Quote
REM Uncomment next line if date format is DD/MM/YYYY
REM set filedatenumber=!filedate:~6,4!!filedate:~0,2!!filedate:~3,2!

REM Uncomment next line if date format is MM/DD/YYYY
REM set filedatenumber=!filedate:~6,4!!filedate:~3,2!!filedate:~0,2!

2. It might be an idea to check that date2 is not the same as, or before date1 and if so, emit a message and ask the user to input the start and end dates again...

Quote
::Set those dates up properly
set date1=%date1:~6,4%%date1:~0,2%%date1:~3,2%
set date2=%date2:~6,4%%date2:~0,2%%date2:~3,2%

3. The DIR command which is run by the FOR loop may as well ignore any folders by using the /a-d switch

for /f "delims=" %%A in ('dir /b /s /tm /a-d') do (

4. This is a personal thing, but I really don't like using double colons to start comment lines. They are not a documented or supported part of batch syntax, and being broken labels, they will screw up any structure using brackets that they appear in. (That is they will make the script crash)



Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

D:\users\A511928>d:

D:\users\A511928>cd\

D:\>fcnt.bat
Searching for files dated
from 2011 21 01
to 2011 26 31
Parameter format not correct - "m".

Salmon Trout - I am getting above error while ur file ...There is another way to go about doing this, utilizing the robocopy /l command. The /l option tells robocopy to only list the files that would be copied, which can then be output to another file. Robocopy also has /MINAGE and /MAXAGE options that allow you to do exactly what you are asking. I would need to play around with it a little more, but it is an alternative if you cannot find where the "m" parameter error is coming from.

Also, copy and paste here the exact code you are using so we might be able to see what is causing the error.

Thanks.Try this for a much simpler code overall. Most of your code is actually in gathering the input and error checking at this point.

Code: [Select]@echo off
setlocal enabledelayedexpansion

rem Find out the dates you want to find between or use default values
:dateset
set /p date1=Enter beginning date of search parameters in mm/dd/yyyy format. {default is 01/01/1900} -
if "%date1%"=="" set date1=01/01/1900
set /p date2=Enter ending date of search parameters in mm/dd/yyyy format. {default is 12/31/4567} -
if "%date2%"=="" set date2=12/31/4567
cls

rem Set those dates up properly and check start date is before end date
set date1=%date1:~6,4%%date1:~0,2%%date1:~3,2%
set date2=%date2:~6,4%%date2:~0,2%%date2:~3,2%
if %date1% gtr %date2% (
echo Start date must be before end date
echo Please enter dates again
ping 1.1.1.1 -n 1 -w 3000>nul
goto dateset
)

rem Determine directories [feel free to setup some error checking here as well]
:loop
cls
set /p tardir=Enter full path name of the target directory-
echo %tardir% >> paths.txt
cls
set /p yn1=Would you like to enter another target directory? {y.n}
if /i "%yn1%"=="y" goto loop
cls

rem Here is the big change in the code
for /f "delims=" %%A in (paths.txt) do (
robocopy %%A c:\temp /l /s /MINAGE:%date1% /MAXAGE:%date2%>>Results.txt
)

rem Show Results file
type %origdir%\Results.txt
pause


Discussion

No Comment Found