简体   繁体   中英

Windows Batch:Count the number of files deleted using forfiles command

I have written a simple batch job for copying the files from one folder to another that are older than x-number of days using forfiles . But I need to count the number of files that have been copied. I tried various ways to do this but was unsuccessful. Could any body help me trough this?

@Echo off

Echo Starting the script for copying files to other folder.

set dt=%date:~10,4%-%date:~4,2%-%date:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%

set filesMovedCount=0

forfiles /p C:\symphonybackup\symphonybackup\Symphony\SymphonyArcheive /s /m *.* /d -30 /c "cmd /c echo @path >> C:\symphonybackup\symphonybackup\Symphony\%dt%.log & cmd /c copy @path C:\symphonybackup\symphonybackup\Symphony\SymphonyTemp" 

Echo filesmovedcount:%filesMovedCount%

Echo finised copying the files

You could just pipe all of your copied file names to a file and then count the rows in the file. Here I added the additional stuff, you can just add the commands where the output gets piped to logfile where indicated. When done, it will echo the count and you delete the file.

@echo off
forfiles /p "rest of copy commands" >>logfile.txt
setlocal EnableDelayedExpansion
set "string=findstr /R /N "^^" logfile.txt | find /C ":""

for /f %%a in ('!string!') do set count=%%a
echo %count% files copied
del /Q logfile.txt

Mayby late answer to this, but when I try this:

set "string=findstr /R /N "^^" logfile.txt | find /C ":""

for /f %%a in ('!string!') do set count=%%a

I get one extra result since I get one empty row. So I did need have to add -1 to the result. Atleast on my system...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM