Please Help - Script Powershell - Get Files / Change Name / Move to another folder
Hi guys,
Could someone please help me with this question?
I'm trying to use this site example to DOWNLOAD files but I'm having a issue:
https://winscp.net/eng/docs/script_locking_files_while_uploading#rename
I'm having a error when the script try to move the renamed file:
"Error: Error moving file 'G:\temporary_localpath\._filepart' to 'G:localpathnewfilename'
'."
Could someone please please help me with this? Here is the script:
param (
$localPath = "\localpath\",
$remotePath = "/remotepath/",
)
try
{
# Load WinSCP .NET assembly
Add-Type -Path "C:\WinSCP\WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
$sessionOptions.HostName = "****"
$sessionOptions.UserName = "****"
$sessionOptions.Password = "****"
$sessionOptions.SshHostKeyFingerprint = "************"
$session = New-Object WinSCP.Session
try
{
$session.Open($sessionOptions)
$suffix = "_filepart"
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.ResumeSupport.State = [WinSCP.TransferResumeSupportState]::Off
$transferResult =
$session.GetFiles(($remotePath + "*.*"), ($localPath + "*.*" + $suffix), $False, $transferOptions)
$transferResult.Check()
foreach ($transfer in $transferResult.Transfers)
{
$finalName = $transfer.Destination.Substring(0, $transfer.Destination.Length - $suffix.Length)
Write-Host ("Renomeando arquivo {0} para o nome final" -f $transfer.Destination, $finalName)
$session.MoveFile($transfer.Destination, $finalName)
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch [Exception]
{
Write-Host ("Error: {0}" -f $_.Exception.Message)
exit 1
}
Could someone please help me with this question?
I'm trying to use this site example to DOWNLOAD files but I'm having a issue:
https://winscp.net/eng/docs/script_locking_files_while_uploading#rename
I'm having a error when the script try to move the renamed file:
"Error: Error moving file 'G:\temporary_localpath\._filepart' to 'G:localpathnewfilename'
'."
Could someone please please help me with this? Here is the script:
param (
$localPath = "\localpath\",
$remotePath = "/remotepath/",
)
try
{
# Load WinSCP .NET assembly
Add-Type -Path "C:\WinSCP\WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
$sessionOptions.HostName = "****"
$sessionOptions.UserName = "****"
$sessionOptions.Password = "****"
$sessionOptions.SshHostKeyFingerprint = "************"
$session = New-Object WinSCP.Session
try
{
$session.Open($sessionOptions)
$suffix = "_filepart"
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.ResumeSupport.State = [WinSCP.TransferResumeSupportState]::Off
$transferResult =
$session.GetFiles(($remotePath + "*.*"), ($localPath + "*.*" + $suffix), $False, $transferOptions)
$transferResult.Check()
foreach ($transfer in $transferResult.Transfers)
{
$finalName = $transfer.Destination.Substring(0, $transfer.Destination.Length - $suffix.Length)
Write-Host ("Renomeando arquivo {0} para o nome final" -f $transfer.Destination, $finalName)
$session.MoveFile($transfer.Destination, $finalName)
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch [Exception]
{
Write-Host ("Error: {0}" -f $_.Exception.Message)
exit 1
}