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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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.
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.


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 84

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
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…
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…

740 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