Forefiles Script

Posted on 2014-10-15
Last Modified: 2014-10-15
I have a script to delete files older than 3 months but it suddenly stopped working?  I'm not sure what's going on..
Here's the script:
Forfiles -p"D:\share\share\share" -s -m*.* -d-3 -c"CMD /c Del @File  : date -30 days"
I'm getting an error that says invalid arguement/option -pD:\...
Question by:WellingtonIS
  • 5
  • 3
LVL 43

Expert Comment

by:Steve Knight
ID: 40382060
Looks to me just like a space missing beteen the /p and first " of the path though difficult to tell on mobile.


Author Comment

ID: 40382070
No I don't think there's supposed to a space there.
LVL 43

Expert Comment

by:Steve Knight
ID: 40382111
Have you tried?  on my PC it errors if there isn't:

C:\Users\stephen>forfiles /p "d:"
"IBM Support.pdf"

C:\Users\stephen>forfiles /p"d:"
ERROR: Invalid argument/option - '/pd:'.
Type "FORFILES /?" for usage.
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.


Author Comment

ID: 40382124
yes.  I think I found my issue. I'll let you know - thanks

Author Comment

ID: 40382139
Nope it didn't work.  I need something to run every 30 days to delete files older than 3 months.  Thats what this was doing.  I'm just not sure what went wrong suddnely
LVL 83

Accepted Solution

oBdA earned 500 total points
ID: 40382500
Several issues. You do need a space after each option (tested on Server 2008 R2 and Windows 7), you should use a slash for the options (makes it easier to read, since the date specifiers accept a "-" as well), and most of all, since you're recursing, you need @Path, not @File. Finally, the "  : date -30 days" doesn't throw an error, but it has nothing lost in the command line.
And if you want to delete files older then 3 months, you need (about) "/d -90", not /d-3.
Try if this works for you; it will only display the files in question, not delete anything. Replace the "ECHO" with "DEL" to run it for real:
forfiles.exe /p "D:\share\share\share" /s /m *.* /d -90 /c "CMD /c ECHO @Path"

Open in new window

LVL 43

Expert Comment

by:Steve Knight
ID: 40382512
thanks oBdA, helps when sitting at a PC then trying to remember settings... I didn't look at the rest since the error was related to the space and the command apparently worked before!


Author Comment

ID: 40382534
Many thanks I'll try it.

Author Closing Comment

ID: 40382553
Thanks - I'll set this up to run once a month.

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
GPO and server 2008 R2 7 34
VB Script: restart service function 30 36
Admin account lockout 10 36
GPO not showing IE10 in GP Preferences 14 38
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
This tutorial will show how to push an installation of Backup Exec to an additional server in both 2012 and 2014 versions of the software. Click on the Backup Exec button in the upper left corner. From here, select Installation and Licensing, then I…
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…

806 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question