forfiles is a cmd command that selects a file (or a set of files) and executes a given piece of code for each of them. It is useful for batch processing. Use this tag for batch file problems related to the command.
Questions tagged [forfiles]
96 questions
79
votes
10 answers
How do I write a Windows batch script to copy the newest file from a directory?
I need to copy the newest file in a directory to a new location. So far I've found resources on the forfiles command, a date-related question here, and another related question. I'm just having a bit of trouble putting the pieces together! How do I…

Devrin
- 1,090
- 1
- 10
- 11
23
votes
8 answers
Forfiles Batch Script (Escaping @ character)
I'm working on a batch script that will let me delete files older then a set period using forfiles. For now, I'm aiming at printing the files that will be deleted.
The forfiles invocation I'm using works flawlessly from a cmd.exe shell, but as soon…

Paradox
- 435
- 2
- 4
- 9
21
votes
4 answers
How to get the most recent file using a batch file?
I have a list of zip files with date and time appended like yyyymmdd_hhmmss_Demos.zip. Now how to get the most recently added zip file in the source dir. I need to copy this file in the target using copy command.
I found some info about forfiles,…

azzaxp
- 697
- 5
- 11
- 26
18
votes
3 answers
forfiles with UNC path
I am trying to use forfiles to delete files that are older than 7 days. The files are in a UNC path. Below is the script that I am using.
Forfiles -p \\devexpress\C$\FULL\ -s -m *.* -d -7 -c "cmd /c del /q @path"
But I get an error mentioning that…

kla
- 251
- 1
- 4
- 9
12
votes
2 answers
forfiles without cmd /c
I can run a forfiles command with
cmd /c, as expected
C:\>forfiles /c "cmd /c ping /a"
IP address must be specified.
However if I remove the cmd /c, it no longer recognizes any arguments, only
the base command
C:\>forfiles /c "ping /a"
Usage:…

Zombo
- 1
- 62
- 391
- 407
11
votes
6 answers
How to delete *.* excluding some extensions?
I'm trying to make a batch file on Windows for deleting all the files in the current directory but excluding 4 file extensions (log, sdb, SDK, bat).
I have tried the Forfiles command on Windows but this delete everything on my current folder (even…

Francisco Chavez
- 111
- 1
- 1
- 3
9
votes
2 answers
How to use multiple commands in batch using forfiles command?
@ECHO OFF
SET backdir=backup
SET snapshotdir=snapshots
SET worldprefix=world_
SET itdate=%date:~10,4%-%date:~4,2%-%date:~7,2%
SET hour=%time:~0,2%
IF "%hour:~0,1%" == " " SET hour=0%hour:~1,1%
echo Current date: %itdate%. Current hour: %hour%.…

Brandon
- 456
- 2
- 6
- 19
7
votes
2 answers
How to tell forfiles to execute command in PATH?
I'm missing something (obvious?) about escaping my strings or spaces in the following Windows Server 2k3 batch command.
FORFILES -m *.wsp -c "CMD /C C:\Program^ Files\Common^ Files\Microsoft^ Shared\web^ server^ extensions\12\bin\stsadm.exe^ -o^…

Noel
- 2,061
- 4
- 31
- 47
6
votes
2 answers
FORFILES date -after- (date calc in cmd file)
I'd like to use FORFILES in a cmd file, to act on all files after a given date thru "today". I can use something like forfiles /d +07/10/2013 /c "cmd /c echo @fname" to act on everything after 7/10/13, but what I want is to just be able to…

Debra
- 197
- 1
- 7
4
votes
1 answer
Why can't we use a CALL :label command in the FORFILES script?
This snippet of code
forfiles /P %pathname% /M *.log /c "cmd /c echo @file"
will happily list out a bunch of files.
We could replace the ECHO with RENAME or MOVE or many other combinations of internal commands: the exception being a CALL to a label…

ilinkcs
- 142
- 8
4
votes
2 answers
Use forfiles to delete a folder and its contents
I am attempting to purge a public directory, so when an item is older than 30 days it should be deleted. I have a batch file setup with the following command:
forfiles /p "C:\PATH\USERS\PATH\" /s /m *.* /c "cmd /c Del @path /q" /d -30
This works…

Wally
- 57
- 1
- 1
- 10
3
votes
4 answers
Forfiles - spaces in folder path
I am running a batch file and I have one forfiles command in it
FORFILES -p%spinputarchrootpath% -m*.csv -d-365 -c"CMD /C DEL @FILE"
%spinputarchrootpath% variable maps to a folder location (Y:\Temp Documents\testfolder).
Now the above command is…

Sahil
- 85
- 1
- 4
- 8
3
votes
2 answers
How do you run a command with a full path from the Windows forfiles command?
I am trying to run the following command:
forfiles /p ..\Schemas /m *.xsd /c "cmd /c ""C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\xsd.exe @path /classes"""
However, it fails with:
ERROR: Invalid argument/option -…

Sam Rueby
- 5,914
- 6
- 36
- 52
3
votes
1 answer
How to get ERRORLEVEL of a command in forfiles?
I need to handle files older than a day. Thereforw I use this:
forfiles -m %%~nxf /C "cmd /c start /wait /MIN 7z.exe t %%f" /d +1
Now I'd like to check if the progress of 7z.exe succeeded. How can I get the errorlevel of the command inside the…

mediii
- 97
- 2
- 12
3
votes
2 answers
How to escape the `@` variable replacement in `forfiles`?
I am using a forfiles command line like this (Windows 7 x64):
forfiles /P "C:\root" /M "*.txt" /C "cmd /C echo @relpath"
How can I escape the replacement of @relpath (relative path to currently iterated item) to get @relpath output literally?…

aschipfl
- 33,626
- 12
- 54
- 99