Using $session.MoveFile

Advertisement

Mitakue
Joined:
Posts:
1
Location:
Paris

Using $session.MoveFile

Hello,

I need to write a PowerShell script to download some files of remote Unix server and after download I want to archive files in Archive folder.
The download works perfectly but the archive using $Session.MoveFile doesn't work.
This is my code:
$fileInfos = $session.EnumerateRemoteFiles($InputDir, $FilePattern, [WinSCP.EnumerationOptions]::MatchDirectories)
 
if ($fileInfos.Count -gt 0)
{           
   foreach ($fileInfo in $fileInfos)
   {
       Write-host("`r------ Action  --------------------`r")         
 
       # Download files           
       $transferResult = $session.GetFiles($fileInfo.FullName, "$OutputDir\", $False, $transferOptions)                   
 
       # Test file not Found
       if ($($transferResult.Transfers.count) -ne 0)
       { 
           #"[$todaylog] - [Action] $Action Transfer $filesCount files on $($transferResult.Transfers.count) : $($FileInfo.Name)" >> $LogFile
           Write-host("$todaylog Download $filesCount file(s) : $($FileInfo.Name)")
           $filesCount=$filesCount+1
 
           $file = $OutputDir+ "\" + $fileInfo.Name
 
           $separator = "."
           $parts = $fileInfo.Name.Split($separator)
 
           $file_basename = $parts[0]
           $file_extension = "." + $parts[1]
 
           #Destination de l'archive
           $DestinationFile = $ArchiveDir + "\" + $file_basename + "_" + $todayDate + $file_extension
           #Taille du Fichier
           $filesize=[math]::Round($($file.length / 1kb),2)
           #Comptage des lignes du fichier
           $lines = (get-content $file | measure-object -line).lines
           #$session.MoveFile($FilePattern, $DestinationFile)                                                                                        
           foreach ($transfer in $transferResult.Transfers)
           {
               # Success or error?
               if ($transfer.Error -eq $Null)
               {
                   Write-Host "    $InputDir/$fileInfo          " 
                   Write-Host "Upload of $($transfer.FileName) succeeded, moving to backup"
                   # Upload succeeded, move source file to backup
                   $session.MoveFile(("$InputDir/$fileInfo", $ArchiveDir)).Check()
               }
               else
               {
                   Write-Host "Upload of $($transfer.FileName) failed: $($transfer.Error.Message)"
               }
           }
My SMA Opcon job is failed but I don't have any errors appears.
I have tried some syntax but I despair.
It's probably nothing at all but I can't find a solution.

Have you some idea?

Kind Regards,
Mitakue

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
38,209
Location:
Prague, Czechia

Re: Using $session.MoveFile

Some hints:
  • Why "$InputDir/$fileInfo"? Use $transfer.FileName or $fileInfo.FullName
  • Use slash, not backslash, in $DestinationFile
What does it mean "is failed", when you "I don't have any errors"?

If you need further help, please attach a full session log file showing the problem (using the latest version of WinSCP).

To generate the session log file, set Session.SessionLogPath. Submit the log with your post as an attachment. Note that passwords and passphrases not stored in the log. You may want to remove other data you consider sensitive though, such as host names, IP addresses, account names or file names (unless they are relevant to the problem). If you do not want to post the log publicly, you can mark the attachment as private.

Reply with quote

Advertisement

You can post new topics in this forum