Solved

Powershell to remove older files from an FTP site.

Posted on 2014-10-15
20
277 Views
Last Modified: 2016-09-22
Hi ,

I need a PowerShell SQL server script in order to remove files older than 15 days off an FTP Folder, no matter how many files are there, they need to be deleted.

I´ve tried to change the code below and I´ve also found many others similar codes without having any success. Files are not being deleted.

The scripts below, deletes from a local folder (C:). In my case , the "FilePath" should be my "ftp.server.com" root folder.


$Now= Get-Date
$Days = "15"
$LastWrite = $Now.AddDays(-$Days)
$Extension = "*.zip"

$FilePath = ??

$Files = Get-Childitem $FilePath -Include $Extension -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}
foreach ($File in $Files)
{
      if ($File -ne $NULL)
      { Remove-Item $File.FullName }
}

My knowledge on PowerShell is very limited, so I really appreciate any help.!

Thanks in advance.

Olavo.
0
Comment
Question by:Olavo Carrano
  • 7
  • 5
  • 4
  • +1
20 Comments
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40382621
You can map the ftp server as a drive letter and then use that in your existing script.

$command = @'
ftp use z: ftp://ftp.server.com/ password /user:username
'@
Invoke-Expression -Command:$command

$Now= Get-Date
$Days = "15"
$LastWrite = $Now.AddDays(-$Days)
$Extension = "*.zip"

$FilePath = "Z:\"

$Files = Get-Childitem $FilePath -Include $Extension -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}
foreach ($File in $Files)
{
      if ($File -ne $NULL)
      { Remove-Item $File.FullName }
}

Open in new window

1
 
LVL 25

Assisted Solution

by:Mohammed Khawaja
Mohammed Khawaja earned 125 total points
ID: 40382686
Once you map it as a drive, use FORFILES command to identify files older than 15 days and then delete them.
forfiles -p z: -m *.zip -d -15 -c "cmd  /c del /q @path"
0
 

Author Comment

by:Olavo Carrano
ID: 40382704
The first solution didn´t work, files have not been deleted...

Where should I put the forfiles command ? replacing foreach or inside the loop ?

Sorry about that question, but its my first time using PowerShell..

Thanks a lot :-)
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40382709
these commands arent really native to powershell but to command line

Technically you could create a .bat file with this in it
ftp use z: ftp://ftp.server.com/ password /user:username
forfiles -p z: -m *.zip -d -15 -c "cmd  /c del /q @path" 

Open in new window


And it will do what you need. Powershell is not involved.
1
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40382711
And when you said it didnt work, what didnt work? did the drive map? if you go to my computer do you see the Z: drive?
0
 
LVL 25

Assisted Solution

by:Mohammed Khawaja
Mohammed Khawaja earned 125 total points
ID: 40382714
Sorry.  Forfiles is a windows commands and you can copy creat a batch file to do the job.  As long as the ftp drive is mapped you could run the command from a command or save it as a batch file and run it.
0
 

Author Comment

by:Olavo Carrano
ID: 40382878
I presume you mean´t "FTPUSE", right ? So, it should be installed to map an FTP driver to windows..
0
 
LVL 16

Assisted Solution

by:Joshua Grantom
Joshua Grantom earned 125 total points
ID: 40382897
yes, my mistake adding a space.

ftpuse z: ftp://ftp.server.com/ password /user:username
forfiles -p z: -m *.zip -d -15 -c "cmd  /c del /q @path" 

Open in new window

0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 10

Expert Comment

by:Michael Ian Claridge
ID: 40383362
Hello,

You can use my script, see if its suitable for you, an example of its use would be:

'.\FilePurge.ps1 -FileExtension "zip" -FileAge "15" -FilePath "C:\TEMP\Logs"

http://procode.technology/?page_id=115

Regards

Michael
0
 

Author Comment

by:Olavo Carrano
ID: 40383436
The command line "ftpuse" works fine, which means the driver shows up on Windows Explorer. However , when using the powershell , driver is not being mapped and the job runs but doesn´t show any errors.

FTPUSE opens a cmd window even using start /B to run in background and when combined with the forfiles on a batch it doesnt work as expected as the windows remains opened apparently waiting for any command in order to start the forfiles.. Any ways to resolve it on a batch ?

I'll be performing a few more tests ...
0
 
LVL 25

Expert Comment

by:Mohammed Khawaja
ID: 40384923
Using forfiles, just add ".*" to @fname (i.e. @fname.*)
0
 

Author Comment

by:Olavo Carrano
ID: 40392047
Unfortunattely, FTPUSE and forfiles not working combined on a batch file...

Below is what I need, map the driver and remove the files. It only works manually. Not sure, what else
should be changed to run automatically on a schedule batch.

--------------------------- Batch File.bat

ftpuse s: ftp.car.com pwd /user:user
FORFILES /p s:\ /s /m *.* /d -15 /c "CMD /C del /Q /F @FILE

----------------------------------------------
0
 

Accepted Solution

by:
Olavo Carrano earned 0 total points
ID: 40394376
I´ve completed the scripts and it´s working fine now. Thanks everyone for helping !

echo off
%comspec% /c ftpuse.exe s: ftp.cars.com pwd /user:user
FORFILES /p s:\ /s /m *.exe /d -15 /c "CMD /C del /Q /F @path"
%comspec% /c ftpuse.exe s: /DELETE
0
 

Author Comment

by:Olavo Carrano
ID: 40394405
I've requested that this question be closed as follows:

Accepted answer: 0 points for Olavo Carrano's comment #a40394376

for the following reason:

working
0
 
LVL 25

Expert Comment

by:Mohammed Khawaja
ID: 40394393
I am glad it is working out for you.  I use FORFILES a lot as it makes writing your own scripts redundant.
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40394407
Mohammed and I gave the original commands to use FTPUSE and FORFILES I believe we should be awarded some points.
0
 

Author Comment

by:Olavo Carrano
ID: 40394413
Agree you guys should receive those awarded points !
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now