I need to move files on a remote FTP server from Test
to Tested
. The files are always .csv
, but the name changes as it's timestamped. Using the PSFTP module, I have written the following
$FtpServer = "ftp://myftpserver.com/"
$User = "myusername"
$PWD = "mypassword"
$Password = ConvertTo-SecureString $Pwd -AsPlainText -Force
$FtpCredentials = New-Object System.Management.Automation.PSCredential ($User, $Password)
Set-FTPConnection -Credentials $FtpCredentials -Server $FtpServer -Session MyFtpSession -UsePassive
$FtpSession = Get-FTPConnection -Session MyFtpSession
$ServerPath = "ftp://myftpserver.com/Test"
$fileList = Get-FTPChildItem -Session $FtpSession -Path $ServerPath -Filter *.csv
$archivefolder = "ftp://myftpserver.com/Tested"
foreach ($element in $fileList )
{
$filename = $ServerPath + $element.name
$newfilename = $archivefolder + $element.name
Rename-FTPItem -Path $filename -NewName $newfilename -Session $FtpSession
}
The files do exist in the Test
folder, but not yet in the archive (Tested
) folder. I thought by using a variable to generate what the new file location should be, that would work.
When I try this, I get
Rename-FTPItem : Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (550) File unavailable (e.g., file not found, no access)."
Is there a way to move files while using a wildcard, or a better way to achieve what I'm trying to do?
Thanks in advance
The -NewName
should be a path only, not a URL:
$archivefolder = "/Tested"
(In the -Path
, the URL is acceptable, but it's redundant, you specify the session already using the $FtpSession
)
You are missing a slash between the folder paths and the file names.
$filename = $ServerPath + "/" + $element.name
$newfilename = $archivefolder + "/" + $element.name
So you should call the Rename-FTPItem
like this:
Rename-FTPItem -Path "ftp://myftpserver.com/Test/file.txt" -NewName "/Tested/file.txt" -Session $FtpSession
or like this:
Rename-FTPItem -Path "/Test/file.txt" -NewName "/Tested/file.txt" -Session $FtpSession