error loading file from * nix with the incorrect special symbol in the file name for Windows

Advertisement

zerohalf@...
Guest

error loading file from * nix with the incorrect special symbol in the file name for Windows

I'm trying to download files from *nix with powershell script using WinSCPnet.dll:
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
               Protocol = [WinSCP.Protocol]::Sftp
               HostName = "<srvr_url>"
               UserName = "<user>"
               Password = "<pass>"
               SshHostKeyFingerprint = "ssh-rsa 2048 tw5wuHrOrBUrBIsv3j9HQdJ8stWQzPKI6J881afD5P0="
            }
***
$fileInfos =
   $session.EnumerateRemoteFiles(
      $RemoteDir, "*$FileMask.xml",
      ([WinSCP.EnumerationOptions]::EnumerateDirectories -bor
         [WinSCP.EnumerationOptions]::AllDirectories))
 
foreach ($fileInfo in $fileInfos)
{
   $localFilePath = $fileInfo.Name -replace "[\0\\r\""\*\\a\/\\b\:\t\<\n\>\\v\?\\f\\\|\']+",''
   $localFilePath =
      [WinSCP.RemotePath]::TranslateRemotePathToLocal(
         [WinSCP.RemotePath]::Combine($RemoteDir,$localFilePath), $RemoteDir, $LocalDir)
 
   Write-Host $localFilePath
   Write-Host "Downloading file $($fileInfo.FullName)..."
   # Download file
   $remoteFilePath = [WinSCP.RemotePath]::EscapeFileMask($fileInfo.FullName)
   $transferResult = $session.GetFiles($remoteFilePath, $localFilePath)
   # Did the download succeeded?
   if (!$transferResult.IsSuccess)
   {
      # Print error (but continue with other files)
      Write-Host (
         "Error downloading file $($fileInfo.FullName): " +
         "$($transferResult.Failures[0].Message)")
   }
}
and catch error:
get_adm.ps1 : 'WinSCP.TransferEventArgs.Destination':
Exception when calling "Create" with "2" arguments: "The path contains invalid characters."
line: 1 character: 1
+ <script_name>.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,<script_name>.ps1
...and catch same error if $localFilePath declared as in example:
$localFilePath =
                [WinSCP.RemotePath]::TranslateRemotePathToLocal(
                    $fileInfo.FullName, $remotePath, $localPath)

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
40,645
Location:
Prague, Czechia

zerohalf@... wrote:

*nix filename like this 1-8190PIR2_PDQ_*all_1-8H41KJ.xml
In Windows, * is not allowed in file name. If you want to download such file, you will have to give it a different name. Normally WinSCP would do it automatically. But as you explicitly specify the target file name to contain * in $localFilePath, you force WinSCP to fail.

Reply with quote

zerohalf@...
Guest

thnx, but answer not for my question

I've asked what I have to do for avoid error when just downloading file. In case of this error winscp.dll ignores targetfilename

Reply with quote

martin
Site Admin
martin avatar

Re: thnx, but answer not for my question

OK, I have probably misunderstood the problem.
Where exactly do you get the exception? I'm not sure what Create does the error refer to.

Reply with quote

Advertisement

zerohalf@...
Guest

zerohalf@... wrote:

Error raised after this
$transferResult = $session.GetFiles
In my opinion, this happens when trying to create a temporary file with the same name as in *nix containing special characters

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
40,645
Location:
Prague, Czechia

zerohalf@... wrote:

Error raised after this
$transferResult = $session.GetFiles
What does it mean "after this"? What line throws the error? 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

martin
Site Admin
martin avatar

Re: log

You didn't answer my question and there's no error in the log.
I do not think the problem has anything to do with WinSCP.
If you believe it is, we need more information.

Reply with quote

Advertisement

You can post new topics in this forum