Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 735
  • Last Modified:

Powershell to remove older files from an FTP site.

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
Olavo Carrano
Asked:
Olavo Carrano
  • 7
  • 5
  • 4
  • +1
4 Solutions
 
Joshua GrantomSenior EngineerCommented:
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
 
Mohammed KhawajaManager - Infrastructure: Information TechnologyCommented:
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
 
Olavo CarranoAuthor Commented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Joshua GrantomSenior EngineerCommented:
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
 
Joshua GrantomSenior EngineerCommented:
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
 
Mohammed KhawajaManager - Infrastructure: Information TechnologyCommented:
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
 
Olavo CarranoAuthor Commented:
I presume you mean´t "FTPUSE", right ? So, it should be installed to map an FTP driver to windows..
0
 
Joshua GrantomSenior EngineerCommented:
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
 
Michael Ian ClaridgeActing Service Delivery ManagerCommented:
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
 
Olavo CarranoAuthor Commented:
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
 
Mohammed KhawajaManager - Infrastructure: Information TechnologyCommented:
Using forfiles, just add ".*" to @fname (i.e. @fname.*)
0
 
Olavo CarranoAuthor Commented:
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
 
Olavo CarranoAuthor Commented:
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
 
Olavo CarranoAuthor Commented:
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
 
Mohammed KhawajaManager - Infrastructure: Information TechnologyCommented:
I am glad it is working out for you.  I use FORFILES a lot as it makes writing your own scripts redundant.
0
 
Joshua GrantomSenior EngineerCommented:
Mohammed and I gave the original commands to use FTPUSE and FORFILES I believe we should be awarded some points.
0
 
Olavo CarranoAuthor Commented:
Agree you guys should receive those awarded points !
0

Featured Post

Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

  • 7
  • 5
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now