Solved

Powershell to remove older files from an FTP site.

Posted on 2014-10-15
20
233 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
Comment Utility
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 24

Assisted Solution

by:Mohammed Khawaja
Mohammed Khawaja earned 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 24

Assisted Solution

by:Mohammed Khawaja
Mohammed Khawaja earned 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 10

Expert Comment

by:Michael Ian Claridge
Comment Utility
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
Comment Utility
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 24

Expert Comment

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

Author Comment

by:Olavo Carrano
Comment Utility
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
Comment Utility
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
Comment Utility
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 24

Expert Comment

by:Mohammed Khawaja
Comment Utility
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
Comment Utility
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
Comment Utility
Agree you guys should receive those awarded points !
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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 demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

17 Experts available now in Live!

Get 1:1 Help Now