Solved

Powershell to remove older files from an FTP site.

Posted on 2014-10-15
20
318 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
This article will help you understand what HashTables are and how to use them in PowerShell.
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

776 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