ForFiles command lets you manage or delete folders, complying with some criteria on Windows 11/10. If you want to use ForFiles command on a shared network folder or a mapped network drive, here is how you can do it using the Command Prompt.
ForFiles is a command that you can run via Command Prompt to mainly delete files that go through some custom filters as per your requirements. For example, if you want to make some free space by removing all the files older than one year or six months or so, you can use the ForFiles command to get it done. The only problem is that you can run it on a local computer only. If you want to run the ForFiles command on a shared network drive or folder, you have to map it first.
How to use ForFiles command on Shared Network folder
The ForFiles command to delete, rename, move filer or folders on a Shared Network folder or Mapped network drive in Windows 11/10. To use ForFiles command on the Shared Network folder, follow these steps-
- Search for cmd in the Taskbar search box.
- Click on the Run as administrator option.
- Click the Yes button.
- Enter the net use command to map a drive.
- Enter the forfiles command with filters to delete a folder.
At first, you have to open Command Prompt with administrator privilege. For that, search for cmd in the Taskbar search box. When the search result appears, make sure the Command Prompt is selected. If so, click on the respective Run as administrator option. Next, select the Yes option to open Command Prompt as administrator.
Now, you have to map a network drive to run the ForFiles command. For that, type the following command and press the Enter button:
net use F: \\networkShare\files /user:yourusername password
Don’t forget to replace the F with the original drive that you want to map, yourusername and password with your username and password.
To find a list, enter the following command-
ForFiles /?
It shows all commands with examples so that you can use them on a mapped network folder on Windows 10.
The full syntax you can use is:
forfiles [/P pathname] [/M searchmask] [/S] [/C command] [/D [+ | -] [{<date> | <days>}]]
You can use the ForFiles command like this:
ForFiles /p "F:\folder-path" /s /d -180 /c "cmd /c del /q @file"
You need to replace the F:\folder-path with the actual folder path that you want to delete.
The above command lets you delete all files that are folder than 180 days or six months and available in the mentioned folder.
There are other commands you can use alongside ForFiles.
To list all of the files in the current directory that are at least one year old, type:
ForFiles /S /M *.* /D -365 /C "cmd /c echo @file is at least one year old."
This post will show you in detail how to schedule ForFiles command on Shared Network drive.
Read next: How to remove Map Network Drive and Disconnect Network Drive options from This PC.