Link to home
Start Free TrialLog in
Avatar of Saha H
Saha H

asked on

Batch file for counting no of files in the folder (run in background automatically and update the count whenever files are added)

Run a .bat file in background automatically and update the count whenever files are added
Avatar of Qlemo
Qlemo
Flag of Germany image

Which purpose is this for precisely? Aou can always run the same script at any time you need the count - it's a fast operation. And shouldn't there be a filter, e.f. based on extension?
Avatar of Saha H
Saha H

ASKER

@echo off
setlocal
REM Default folder; set to . for the current folder:
set folder=C:\Temp
REM override from the command line
if not "%~1"=="" (set folder=%~1)
for /f %%a in ('dir /a:-d /b "%folder%" ^| find.exe /c /v ""') do msg.exe %UserName% "No of files added: %%a"


i got this code already and i want this should run in background and if any file added to that path means, it has to show the count automatically

Thanks in advance..
Avatar of Saha H

ASKER

I want extension for this.  I have to make it as run in background and automatically show the count whenever  files added to that.

Thanks
Something like:
@echo off
setlocal
REM Default folder; set to . for the current folder:
set folder=C:\Temp
REM override from the command line
if not "%~1"=="" (set folder=%~1)
:loop
for /f %%a in ('dir /a:-d /b "%folder%" ^| find.exe /c /v ""') do msg.exe %UserName% "No of files added: %%a"
ping -n 301 127.0.0.1 >nul: REM Waiting 5 minutes
goto loop

Open in new window

Avatar of Saha H

ASKER

Hi Qlemo,

It is not working for me. Its is giving false count of files added to that.

Thanks,
Saha
Avatar of Saha H

ASKER

And it has to show the count whenever new files added to that.
The script just shows a count of files currently in the folder. My assumtion was that you clear out that folder before confirming the message, so each run starts with no files. (But I forgot to suppress the 0 count message.)

You can't trigger the script display "whenever new files added" with cmd batches. We can run a cyclic check, remember the previous count, and only show a message if the count has increased.

A script in PowerShell or VBScript is capable of being triggered on new files, but that would happen with each file. If you add two, you get two messages.

Maybe we can help much better if you explain what the real purpose of everything is here.
Avatar of Saha H

ASKER

Even i want like this,

You can't trigger the script display "whenever new files added" with cmd batches. We can run a cyclic check, remember the previous count, and only show a message if the count has increased.

Suppose in a folder  some files are there , first it has to take that count and  afterwards any files added to that folder means it has to show only the increased count . And it has to run continuously in background for checking any files added or not.

Please help me in this...

Thanks in advance.
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Saha H

ASKER

Thanks.

But this will not work when two files added at a time.
What to do to show the count as two?
It doesn't matter how many files are added, as long as no file gets deleted - the overall number of files need to be greater than in the prior run.
Avatar of Saha H

ASKER

Yes, that is correct  but it has to show the total count right how many added.
Sorry? Can you explain, best with an example?
Avatar of Saha H

ASKER

Thanks a lot Qlemo.

Sorry for delay in reply.

My problem is solved but i want extend it like , can we search files based on specific names?
Because in folder all types of files are available right, Is it possible to search with specific file names?

Once again Thanks for your help!!!
Just add the file mask to your dir. For more than one mask add multiple paths:
... ('dir /a:-d /b "%folder%\*.gif","%folder%\*.bmp" ^| ...

Open in new window

Avatar of Saha H

ASKER

Hi,
Thanks a lot ..

Please can you help me in this..
How to wake a system from sleep with batch command?

Only I need command which will wake a system from sleep.
Ask that as a new question, please. Include the exact state of the machine - sleep (suspend to RAM, not powered off, on stand by) or hibernate (suspend to disk, powered off). And whether you want to do that remotely from another device or  per timer or similar running on the same machine.
Avatar of Saha H

ASKER

Yes i have asked in new question.

Thanks