≡ Menu


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"


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"
24 comments… add one
  • William

    Does forfiles work with hidden files ? or does it ignore them? It does not seem to be working for me.

  • DOS

    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 ?

    • admin

      That was an an error. Corrected it.

  • zhanar

    Any hints on how to find files larger than a specified size?

  • barny

    example for finding files less than given size:

    forfiles /S /M *.txt /C "cmd /c if @fsize LSS 1000 echo @file"
    • admin

      Super! Thanks for sharing.

    • Pratiksha

      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.

    • Srini

      Pratiksha – replace notepad with echo to list the file names. I updated the command in Barny’s comment.

  • JAS

    Is there a way to delete specific folders within a destination, that is X-days old.

    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”

  • swapna


    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
    For /F “delims=, tokens=1,2,3,4” %%1 in (purge_files.csv) do call :dofunction “%%1” “%%2” “%%3” “%%4”
    Set FOLDER_PATH=%4
    Set ENABLE=%3


    Purge_files.csv: which is configuration files which holds, file patterns, retention period, enable flag and folder paths.


    please let me know how do i delete/ execute my forfiles command only for enable flag = y?
    Thanks in advance..

  • Takacsi

    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


    • DC

      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.

    • DC

      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.”

  • john

    How can I remove the quotes ( ” ) and (.\) and also exclude some specific path like i dont want to display Host Intrusion Prevention directory?

    “.\Endpoint Encryption”
    “.\Endpoint Encryption Agent”
    “.\Endpoint Encryption for Files and Folders”
    “.\Host Intrusion Prevention”

  • swapnil

    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?

  • Arul

    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?


  • Srikanth

    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?

  • sai

    can i copy the result of the command into any text file

  • Thomas Oatman

    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 ?

  • roniz

    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”

  • Ashraf

    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”


  • RIP

    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?


  • AAmir Saleh

    Is there a way to run power shell command instead of cmd?

  • Mike

    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”


Cancel reply

Leave a Comment