≡ Menu

Forfiles

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"
15 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.

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

  • swapna

    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

  • 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

    Thanks!

  • john

    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”

  • 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?

    Thanks!

  • 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?

Leave a Comment