Deleting old files from Network Share

Hi,

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.

Thanks.
Al_Hassan_GroupAsked:
Who is Participating?
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.

oBdACommented:
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

0
Naitik GamitSoftware DeveloperCommented:
Look at this steps and description for : How to Delete Files Older than X Days on Windows

http://www.howtogeek.com/131881/how-to-delete-files-older-than-x-days-on-windows/
0
Al_Hassan_GroupAuthor Commented:
Hi oBdA,

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

Thanks.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

oBdACommented:
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.
0
Al_Hassan_GroupAuthor Commented:
Its giving error, which I have attached below
error.jpg
0
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
0
oBdACommented:
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
setlocal
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%'!
		pause
		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

0
Bill PrewCommented:
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.

http://www.horstmuc.de/wbat32.htm
http://www.horstmuc.de/win/delage.htm


~bp
0
QlemoBatchelor, 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)"
popd

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.
0

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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.