Forfiles is a useful windows command to select a set of files and then run a command on each of the files. It’s similar to the functionality of find command on Linux OS.
The syntax of the forfiles is as follows.
forfiles <Criteria to select files> /C "command to be applied on each of the files selected"
The criteria we can use to select the files:
Modified date or Number of days it was last modified from (option /D)
Search files based on name(option /M)
Look for files in subdirectories also (option /S)
Look for files in a specific directory (option /P)
We can use any combination of the above to select the required files.
Find all excel files modified 10 days back in the current folder and subfolders
forfiles /D -10 /S /M *.xlsx /C "cmd /c echo @path"
In the above command @path is used to print the complete absolute path of the file. Similarly we can use below variables in the command part.
@file – Name of the file(includes extension)
@fname – Name of the file excluding extension
@relpath – Relative path of the file from current folder
@ext – Extension of the file
@fsize – Size of the file
@fdate – Last modified date of the file
@ftime – Last modified time of the file
delete all log files created in the last 1 month
forfiles /D +30 /S /M *.log /C "cmd /c del @file"
Copy/Backup files modified after 1st Jauary 2015
forfiles /D 01/01/2015 /M * /C "cmd /c copy @file D:\backupFolder\"
Get list of all picture files with their size
forfiles /S /M *.jpg /C "cmd /c echo @path @fsize"
This command searches pictures of only jpg type. To search picture of different file type(jpeg, png etc), the command has to be triggered again.
Get list of all exe’s and their last modified date
forfiles /M *.exe /C "cmd /c echo @path @fdate"
Example:
c:\Windows>forfiles /M *.exe /C "cmd /c echo @path @fdate" "c:\Windows\bfsvc.exe" 11/20/2010 "c:\Windows\explorer.exe" 2/24/2011 "c:\Windows\fveupdate.exe" 7/13/2009 "c:\Windows\HelpPane.exe" 7/13/2009 "c:\Windows\hh.exe" 7/13/2009 "c:\Windows\notepad.exe" 7/13/2009 "c:\Windows\regedit.exe" 7/13/2009 "c:\Windows\splwow64.exe" 2/10/2012 "c:\Windows\twunk_16.exe" 6/10/2009 "c:\Windows\twunk_32.exe" 7/13/2009 "c:\Windows\winhlp32.exe" 7/13/2009 "c:\Windows\write.exe" 7/13/2009
Rename file extensions using forfiles command
Read this: Rename File extensions in bulk
List all sub-directories in a given folder
If you want to print all the subdirectories in the current directory
forfiles /m * /c "cmd /c if @isdir==TRUE echo @file"
Does forfiles work with hidden files ? or does it ignore them? It does not seem to be working for me.
why do you say the syntax is:
findstr /C “command to be applied on each of the files selected”
What does findstr have to do with forfiles ?
That was an an error. Corrected it.
Any hints on how to find files larger than a specified size?
example for finding files less than given size:
Super! Thanks for sharing.
I was just practicing a few commands and found this one so I tried it. I thought it would just list all the files less than the given size but it opened all of them, that too one by one.
Pratiksha – replace notepad with echo to list the file names. I updated the command in Barny’s comment.
Is there a way to delete specific folders within a destination, that is X-days old.
Example.
I have a folder named LOG with many folders named by date. I want to make a script to delete only folders named by date, and no others.
This is a script I made, but it deletes ALL folders that are X-days old, and not the specific date folders. Can I use the M parameter to source out folders with specific names. Like yymmdd 150901 -> 150*
@echo off
Set “Target=Path to the LOG-folder”
Set “daysold=30”
If Exist “%Target%” (
forfiles /p “%Target%” /d -%daysold% /c “cmd /c IF @isdir==TRUE RD @Path /S /Q”
)
Hi,
is there a way that i can delete the files with enable flag = y and xdays older using batch script and i need is folder path , file pattern, retention period and flag.?
Im able to delete all the files in specified path older than x days but not able to pass flag value = y
please see the script im using.
@echo off & setlocal
cd Q:\DATARETENTION\WINDOWSBATCHSCRIPTING
For /F “delims=, tokens=1,2,3,4” %%1 in (purge_files.csv) do call :dofunction “%%1” “%%2” “%%3” “%%4”
:dofunction
Set FOLDER_PATH=%4
Set FILE_PATTERN=%1
Set RETENTION_PERIOD=%2
Set ENABLE=%3
FORFILES /P %FOLDER_PATH% /M %FILE_PATTERN% /D -%RETENTION_PERIOD% /C “cmd /c del @file”
Purge_files.csv: which is configuration files which holds, file patterns, retention period, enable flag and folder paths.
*.csv,1,Y,Q:\DATARETENTION\WINDOWSBATCHSCRIPTING\TEST
temp*.csv,1,N,Q:\DATARETENTION\WINDOWSBATCHSCRIPTING\TEST
*.txt,1,N,Q:\DATARETENTION\WINDOWSBATCHSCRIPTING\TEST
*.docx,1,Y,Q:\DATARETENTION\WINDOWSBATCHSCRIPTING\TEST
please let me know how do i delete/ execute my forfiles command only for enable flag = y?
Thanks in advance..
swapna
Hi, could you try it pls?
forfiles /s /d +30 /m * /c “cmd /c echo @path”
the /d with +day is not working for me, but works with –
I tried it on Win10
Thanks!
I agree that /d +60 does not work to find files newer than 60 days. I think I see the problem in the implementation details on the microsoft website. I think they implemented it to look for files with a modification date greater than today’s date *plus* 60 days. SMH. The +60 should indeed trigger the “greater than” test, but the comparison date still should be today’s date *minus* 60 days. Must be the result of a bad specification. And bad testing. And a fortune built on bullying and intellectual theft.
Looks like a defect in the design and testing. Very shoddy. Where the documentation says “the current date plus” it should say (and be implemented as) “the current date minus”:
“Selects files with a last modified date later than or equal to (+) the current date plus the number of days specified, or earlier than or equal to (-) the current date minus the number of days specified.”
How can I remove the quotes ( ” ) and (.\) and also exclude some specific path like i dont want to display Host Intrusion Prevention directory?
“.\DLP”
“.\Endpoint Encryption”
“.\Endpoint Encryption Agent”
“.\Endpoint Encryption for Files and Folders”
“.\FileList.bat”
“.\Host Intrusion Prevention”
“.\List.txt”
“.\DLP\Agent”
“.\DLP\Agent\ad1sr.dll”
“.\DLP\Agent\afsr.dll”
“.\DLP\Agent\aiffsr.dll”
“.\DLP\Agent\asfsr.dll”
“.\DLP\Agent\assr.dll”
“.\DLP\Agent\awsr.dll”
“.\DLP\Agent\bentofio.dll”
“.\DLP\Agent\bkfsr.dll”
“.\DLP\Agent\bmpsr.dll”
“.\DLP\Agent\bzip2sr.dll”
“.\DLP\Agent\cabsr.dll”
forefiles is not working for files older than 1 day i.e. files created on yesterday.
Is there any way we can delete files created on yesterday exlcuding sub directories?
I want to copy the files which was saved for today. I tried the below, not working.
@echo off
forfiles /D +1 /S /M * /C “CMD /C COPY @FILE C:\Users\c-aruldevanesan\Documents\DIPRUN
Can someone help me find where the issue is?
Thanks!
I can see this command works on Windows Server 2003. But MSDN says Applies To: Windows Vista, Windows Server 2008, Windows Server 2012, Windows8. https://technet.microsoft.com/en-us/library/cc753551(v=ws.11).aspx
Can you guys please clarify if this works fine on windows server 2003?
Hi,
can i copy the result of the command into any text file
Great stuff ! Thank you!!!!
I would like to be able to sort the output by date. But mine is currently outputing in mm/dd/yyyy format.
So.. Is there a way to output in yyyy-mm-dd format ?
Im trying to use the forfiles command to delete from the svn all files ending with .LOB- but it fails.
any ideas?
this is the command I enter:
forfiles /s /m *.LOB /c “cmd /c svn delete @relpath”
Hi ,
I want to get the value of @fsize (in the below command ) to store in an object $size using powershell.
forfiles /p C:\environment\cust_config\version1 /m local.properties /c “cmd /c echo @fsize”
Thanks,
Ashraf
I would like to copy all message files (*.msg) located on disk P to disk E in folder email-1
I can not seem to get this to work, any suggestions?
Thanks
Rip
Is there a way to run power shell command instead of cmd?
Hello
I am trying to, delete all empty files from a folder. The following scritp works but is long
Any help to get it quicker ?
for /r “Folder” %%F in (*) do if %%~zF==0 del “%%F”
Thanks
Hi,
My name is Venkatakrishna,
I want to compare the files in two folders using batch script.
For example: If test1.txt is identical in two folders, it should be printed so that the two files are identical
Otherwise it should print the two files separately.
I wrote below script:
@echo off
setlocal
set “folder1=C:\Users\chenvenk\Desktop\comp1”
set “folder2=C:\Users\chenvenk\Desktop\comp2”
set “fileMask=*”
for /f “delims=” %%F in (
‘echo “.”^&forfiles /s /p “%folder1%” /m “%fileMask%” /c “cmd /c if @isdir==TRUE echo @relpath”‘
) do fc “%folder1%\%%~F\%fileMask%” “%folder2%\%%~F\*”
pause
This above script compare the files and sub files data aloso and printing.
we need to compare the two files and say whether the files are same or not.
Pls help me to send me the proper script.
Hi,
Below command used to print the sub directories data but i want to print the directory name only.
Pls modify to send the proper command.
The command prints names of the subdirectories. If you need to print absolute path of the directories, you can use
@path
instead of@file
.If i want to search for a file like this and then navigate to that path how can i make it work?
forfiles /p C:\Windows\ccmcache\ /s /m *test*
(works)
forfiles /p C:\Windows\ccmcache\ /s /m *test* & cd %cd%
not work
Is it possible with forfiles using other syntaxes after the command, cause its seem little hard to get working as it should.
Hi, I am writing the batch script to delete files from remote computer, getting error as UNC path not supported. can anyone help to correct the syntax.
net use \\devicename * /user:domainname\user
forfiles /p “\\x.x.x.x\c$\test\log” /s /m *.log* /D -%NumberOfDays% /C “cmd /c del @path”
pause
Looks like issue with forfiles syntax– is there way to access/search remote system files using forfiles /p command?
forfiles -p D:\Autodesk\Backups -s -m *.* -d -2 -c "cmd /d takeown /F @path"
forfiles -p D:\Autodesk\Backups -s -m *.* -d -2 -c "cmd /c del @path"
This is a huge file, worked once but not after that. Could the size of the file be the issue limiting its execution?