Deleting my file by last date modified
hi ,
I need to delete the files which are modified before 5 days .
plz let me know the cmd so that i can use that as the batch file.
ie. i am copying an file(each of different name ) daily in an particular location and after week its takes too much of size in the disk, so i need to keep the files only a week older and rest should be deleted
May 12th, 2012 1:08am
Hi,
Assuming you are running Server 2008 or later, here's a simple script I make use of as a scheduled task on our IIS and Exchange servers. Just save it with a name of something like Remove-OldFiles.ps1 and run it as follows:
.\Remove-OldFiles.ps1 -Path C:\inetpub\logfiles\w3svc1 -Days 5
Here's the code:
param(
[parameter(Position=0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, mandatory=$true)][string]$Path,
[parameter(Position=0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, mandatory=$true)][int32]$Days
)
begin {
$Path += '\*'
$Days = $Days * -1
$now = Get-Date
$filesRemove = Get-ChildItem -Path $Path | where {($_.LastWriteTime -lt $now.AddDays($Days)) -and -not ($_.Mode -match 'd')}
foreach ($file in $filesRemove) {
Write-Output $file.FullName
Remove-Item $file.FullName
}
}
Cheers,
Lain
Free Windows Admin Tool Kit Click here and download it now
May 12th, 2012 2:42am
Hello,
Please find the below link and write your own batch file based on your requirement.
http://technet.microsoft.com/en-us/library/cc753551(v=ws.10).aspx
*Note: Don't
run any scripts in live environment without testing it in your test lab/environment.
Regards, Ravikumar P
May 12th, 2012 2:50am
You can use robocopy
@echo off:: dele-old-files.cmd
setlocal
cls
set days=80
set folder=c:\temp
if not exist %folder% goto :ERR
:: create temporary folders
set tempfolder=%folder%\_%random%_
mkdir "%tempfolder%" || goto :ERR
robocopy "%folder%" "%tempfolder%" /minage:%days% /r:1 /w:1 /xd * /move
:: if temporary folder is empty will be deleted an go to the END
rd "%tempfolder%" && goto :EOF
:: temporary folder not empty
echo.&echo.&echo.
:: start safe files part *** for security reason ask
echo All the file in %tempfolder% will be deleted
set /p continue=continue?[y or n]
(echo.%continue% | findstr /i /b "y" )&& goto :CONTINUE
::
robocopy "%tempfolder%" "%folder%" /r:1 /w:1 /xd * /move
rd "%tempfolder%"
goto :EOF
:: end safe files part *** now is too late
:CONTINUE
:: create an empty folder
set emptyfolder=%folder%\_empty%random%_
mkdir "%emptyfolder%" || goto :ERR
::delete all the files and folders in "%tempfolder%"
echo Delete all the files an folders in "%tempfolder%"
robocopy "%emptyfolder%" "%tempfolder%" /r:1 /w:1 /mir
::delete temporary folders
rd /s /q "%emptyfolder%"
rd /s /q "%tempfolder%"
goto :EOF
:ERR
echo Error
Gastone Canali >http://www.armadillo.it
Se alcuni post rispondono al tuo quesito (non necessariamente i miei), ricorda di contrassegnarli come risposta e non dimenticare di contrassegnare anche i post utili . GRAZIE!
Free Windows Admin Tool Kit Click here and download it now
May 12th, 2012 5:21am


