Hello All,
I'm able to ftp, and unzip a file, now I need to move it to a network drive, it runs without errors but no files turn up in the drive? What am I doing wrong? Please help:
# Here are the variables to change to your needs:
$ftpPath = 'ftp://ftp.financialgo.net/'
$ftpUser = 'aUser'
$ftpPass = 'aPassword'
$localPath = 'D:\Test1\'
# end of changes
# get the file listing of the FTP folder
function Get-FtpDir ($url, $credentials)
{
$request = [Net.FtpWebRequest]::Create($url)
if ($credentials) { $request.Credentials = $credentials }
$request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory
$response = $request.GetResponse()
$reader = New-Object IO.StreamReader $response.GetResponseStream()
$reader.ReadToEnd()
$reader.Close()
$response.Close()
}
# delete a FTP file
function Remove-FtpFile ($url, $credentials)
{
$request = [System.Net.FtpWebRequest]::create($url)
if ($credentials) { $ftprequest.Credentials = $credentials }
$request.Method = [System.Net.WebRequestMethods+Ftp]::DeleteFile
$request.GetResponse()
$request.Close()
}
# MAP PATH TO QDLS\CLEAR CK\COMPPEXT
$password = "aPassword" | ConvertTo-SecureString -asPlainText -Force
$username = "ISUSNET\aUser"
$remotePath = "Z:\"
$credential = New-Object System.Management.Automation.PSCredential($username,$password)
New-PSDrive -Name target -PSProvider FileSystem -Root \\GFT-PROD\ofc -Credential $credential | Out-Null
# Needed for the download
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = New-Object System.Net.NetworkCredential($ftpUser,$ftpPass)
$webclient.BaseAddress = $ftpPath
Get-FTPDir $ftpPath $webclient.Credentials |
? { $_ -like '*.zip' } |
% {
$webClient.DownloadFile($remotePath, $localPath)
# DownloadFile does not provide a success state, so we have to check for the file name
#if (Test-Path $remotePath) { Remove-FtpFile $ftpPath+$_ $webclient.Credentials }
}
# Now remove oldest files but three
Get-ChildItem $localPath -filter *.zip |
sort CreationTime -Descending |
select -Skip 3 |
Remove-Item