0

I have a folder and using batch file I want to know whether its modified(any file created or modified in it) today or not.

Below is command to get count of all files in a folder. I am new in batch files and don't know how to get it, pl help.

set MyFolder=D:\folder1
SET file_Cnt=0
for %%o IN (%MyFolder%/*.*) DO (      
      SET /A file_Cnt=file_Cnt+ 1    
)
echo %file_Cnt%

Edit :- I got this solution but only problem is I want to set a variable(for total count of files modified) here which can be used later.

forfiles /S /P "%MyFolder%" /M "*" /D +0 /C "cmd /C if @isdir==FALSE echo _" | find /C "_"

Edit2:- I have tried below commands to set the variable but not working.

forfiles /S /P "%MyFolder%" /M "*" /D +0 /C "cmd /C if @isdir==FALSE echo _" | find /C "_"
echo %_%      --getting echo off message

for /F %%N in ('forfiles /S /P "%MyFolder%" /M "*" /D +0 /C "cmd /C if @isdir==FALSE echo _" ^| find /C "_"') do set "NUMBER=%%N"
echo %NUMBER% --getting echo off message
echo %%N --getting echo off message
Mona
  • 21
  • 5
  • The last approach (`for /F`) should work even if no files are found. Have you tried this separately, or within some other code? Perhaps you are facing a totally different problem (e. g., lack of [delayed expansion](http://ss64.com/nt/delayedexpansion.html))... – aschipfl Mar 31 '18 at 13:01
  • I also have tried delayed expansion but still getting echo off – Mona Apr 02 '18 at 06:45
  • And I don't think this condition is working at all – Mona Apr 02 '18 at 07:09
  • Perhaps you should ask [@mandeep](https://stackoverflow.com/users/7820310/mandeep?tab=questions) to collaborate. – Compo Apr 02 '18 at 08:22
  • Thanks @Compo, actually I am following mandeep's question. And I resolved it. – Mona Apr 02 '18 at 10:06
  • @Mona, if you've found a solution to your own, and consequently mandeep's issue, why not post the solution in this and/or those questions, marking your own as accepted. – Compo Apr 02 '18 at 10:50
  • I've posted my answer @Compo, its working. Last thing which I want to get filename(s) which has been modified, if you know please answer this. – Mona Apr 02 '18 at 11:00

6 Answers6

2

I would suggest you to use PowerShell for this.

Get-ChildItem -Path 'D:\Folder1' | Where-Object -FilterScript { $_.LastWriteTime.Date -eq (Get-Date).Date}

To call from command prompt.

PowerShell.exe -c "Get-ChildItem -Path 'D:\Folder1' | Where-Object -FilterScript { $_.LastWriteTime.Date -eq (Get-Date).Date}|measure | select -expand count" 
Prasoon Karunan V
  • 2,916
  • 2
  • 12
  • 26
1

The following command gets the list of all the files from the current folder that are created on the same day.

for /F "tokens=2" %i in ('date /t') do dir /T:C | findstr /C:%i /B

Hope this helps !

nghiep truong
  • 310
  • 3
  • 8
1

If you just want to output filename, you can try this command

for /F "tokens=2" %i in ('date /t') do dir /T:C "C:\Test" | findstr /C:%i /B | for /f "tokens=5" %j in ('more') do @echo %j

Or if you want to get the count, you can try this command

for /F "tokens=2" %i in ('date /t') do dir /T:C "C:\Test" | findstr /C:%i /B | find /c ":"
nghiep truong
  • 310
  • 3
  • 8
  • not working @nghiep... and where did you pass current date – Mona Mar 31 '18 at 08:28
  • ('date /t) is where I put the current date. You can try this command "date /t" in your command line window. The output will be like this "Sat 03/31/2018" – nghiep truong Mar 31 '18 at 08:33
0
 for /F %%N in ('forfiles /S /P "%MyFolder%" /M "*" /D +0 /C "cmd /C if @isdir==FALSE echo _" ^| find /C "_"') do (
     // we can simply get %%N here or we can also set some variable here if required.  
    )
Mona
  • 21
  • 5
-1

I think the answer for this question could possible help you, they have scripts for getting the LastModified, LastAccessed and DateCreated from folders and files.

Batch file : get the creation date of a folder

Eze Ahuna
  • 13
  • 1
  • 5
-3

Below is my test result. What the error in your command, @Mona ?

commandline

nghiep truong
  • 310
  • 3
  • 8
  • when I write for /F "tokens=2" %i in ('date /t') do dir /T:C "%MyFolder%" | findstr /C:%i /B | find /c ":" then its not running and when I simply write dir /T:C "%MyFolder%" | findstr /C:%i /B | find /c ":" then getting error FINDSTR: argument missing after /C .... and I am working on a batch file... – Mona Mar 31 '18 at 09:22
  • In your batch file, we need to write %%i instead of %i. For example, I create test.bat with the following content. set MyFolder="C:\Test" for /F "tokens=2" %%i in ('date /t') do dir /T:C "%MyFolder%" | findstr /C:%%i /B | find /c ":" > C:\Test\a.txt. Then open C:\Test\a.txt to view the result after executing batch file – nghiep truong Mar 31 '18 at 15:43