Deleting old files from Network Share


I want to delete the files which are older than 3 days from my network share drive on daily basis.

Can any one guide me with the script that meets my requirement.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Batch in test mode, it will only show the del commands without actually deleting anything; remove the uppercase ECHO to run it for real:
forfiles.exe /p "<BaseDir>" /m * /s /d -3 /c "cmd.exe /c if @isdir==FALSE (ECHO del /f @path)"

Open in new window

Powershell in test mode, it will only show what would be removed; remove the -WhatIf argument at the end to run it for real:
Get-ChildItem "<BaseDir>" -File -Recurse | ? {$_.LastWriteTime -lt (Get-Date).AddDays(-3)} | Remove-Item -WhatIf

Open in new window

Naitik GamitSoftware DeveloperCommented:
Look at this steps and description for : How to Delete Files Older than X Days on Windows
Al_Hassan_GroupAuthor Commented:
Hi oBdA,

Whether the Path of the shared folder has to be given in <BaseDir> or @path field....

HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

In both scripts, you only need to replace <BaseDir> with the root folder where you want to start the search and destroy.
@path is a forfiles placeholder.
Al_Hassan_GroupAuthor Commented:
Its giving error, which I have attached below
Al_Hassan_GroupAuthor Commented:
The above is script is working absolutely fine in the local PC but not on the network share, My requirement is for the network share
Sorry, never tried forfiles against a UNC.
This will automatically map the next available drive letter if BaseDir is a UNC, and remove it once the script is done.
@echo off
set BaseDir=\\server\share\folder

set Drive=
if "%BaseDir:~0,2%"=="\\" (
	for /f "tokens=2" %%a in ('net.exe use * "%BaseDir%" ^| find /i "connected to %BaseDir%"') do set Drive=%%a
	if not defined Drive (
		echo Could not connect to '%BaseDir%'!
		goto :eof
if defined Drive (
	set SearchRoot=%Drive%\
) else (
	set SearchRoot="%BaseDir%"
forfiles.exe /p %SearchRoot% /m * /s /d -3 /c "cmd.exe /c if @isdir==FALSE (ECHO del /f @path)"
if defined Drive (
	net.exe use %Drive% /delete

Open in new window

Bill PrewIT / Software Engineering ConsultantCommented:
If you can use a small free utility, DELAGE32 is a great one I use for this sort of thing.  Seems to handle network UNC paths okay.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Instead of doing that search for a free drive letter, just use
pushd %BaseDir%
forfiles.exe /p . /m * /s /d -3 /c "cmd.exe /c if @isdir==FALSE (ECHO del /f @path)"

Open in new window

That maps a drive (using Z: and going backwards). and changes the current directory, performs the FORFILES, and then reverts the mapping and current folder.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.