Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Powershell to remove older files from an FTP site.

Posted on 2014-10-15
20
Medium Priority
?
567 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

715 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