EnumerateRemoteFiles and FileExists not working correcly for file with parenthesis in file name

Advertisement

wwoiseau@hotmail.com
Joined:
Posts:
2

EnumerateRemoteFiles and FileExists not working correcly for file with parenthesis in file name

I am using the EnumerateRemoteFiles and FileExists in a PowerShell script and it is not always working correctly for file with parenthesis () in file name especially when I add the a wildcard * to find many files.

PowerShell script:
...
function fct_Open
{
   param 
   (
      [string]$OutLogPathFile
   )
 
   $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
       Protocol   = [WinSCP.Protocol]::ftp
       UserName   = removed for security
       Password   = removed for security 
       HostName   = removed for security
   }
   
   $OutboundPathFile = $FolderLocalLog + '\' + $OutLogPathFile
   $session.SessionLogPath = $OutboundPathFile
   
   #The value can be -1 = Reduced, 0 = Normal, 1 = Debug 1 or 2 = Debug 2 logging levels
   $session.DebugLogLevel = 2
    
   # Connect
   $session.Open($sessionOptions)
}
 
   $folder = "/BI_Broadcast/TEST/DailyProduction/"
 
   Write-Host ""
   Write-Host ""
 
   Write-Host "=============================================================================================="
 
   $strLogFileOutput = $ScriptName + '_' + $TargetServerName + (Get-Date -Format "yyyyMMdd-HHmmss") + '_1.log'
   $session = New-Object WinSCP.Session
   fct_Open $strLogFileOutput
 
   $mask = "Daily*.csv"
   $files1 = $session.EnumerateRemoteFiles($folder, $mask, [WinSCP.EnumerationOptions]::None)
   Write-Host "++++1-EnumerateRemoteFiles'$mask':"
   Write-Host "$Files1"
 
   # Disconnect, clean up
   $session.Dispose()
 
   Write-Host "=============================================================================================="
 
   $strLogFileOutput = $ScriptName + '_' + $TargetServerName + (Get-Date -Format "yyyyMMdd-HHmmss") + '_2.log'
   $session = New-Object WinSCP.Session
   fct_Open $strLogFileOutput
 
   $mask = "Daily_(test-alain).csv"
   $files2 = $session.EnumerateRemoteFiles($folder, $mask, [WinSCP.EnumerationOptions]::None)
   Write-Host "++++2-EnumerateRemoteFiles'$mask':"
   Write-Host "$Files2"
 
   # Disconnect, clean up
   $session.Dispose()
 
   Write-Host "=============================================================================================="
 
   $strLogFileOutput = $ScriptName + '_' + $TargetServerName + (Get-Date -Format "yyyyMMdd-HHmmss") + '_3.log'
   $session = New-Object WinSCP.Session
   fct_Open $strLogFileOutput
 
   $mask = "Daily_%28test-alain%29.csv"
   $files3 = $session.EnumerateRemoteFiles($folder, $mask, [WinSCP.EnumerationOptions]::None)
   Write-Host "++++3- EnumerateRemoteFiles '$mask':"
   Write-Host "$Files3"
 
   # Disconnect, clean up
   $session.Dispose()
 
   Write-Host "=============================================================================================="
 
   $strLogFileOutput = $ScriptName + '_' + $TargetServerName + (Get-Date -Format "yyyyMMdd-HHmmss") + '_4.log'
   $session = New-Object WinSCP.Session
   fct_Open $strLogFileOutput
 
   $mask = "Daily_((test-alain)).csv"
   $files4 =
       $session.EnumerateRemoteFiles($folder, $mask, [WinSCP.EnumerationOptions]::None)
   Write-Host "++++4-EnumerateRemoteFiles'$mask':"
   Write-Host "$Files4"
 
   # Disconnect, clean up
   $session.Dispose()
 
   Write-Host "=============================================================================================="
 
   $strLogFileOutput = $ScriptName + '_' + $TargetServerName + (Get-Date -Format "yyyyMMdd-HHmmss") + '_5.log'
   $session = New-Object WinSCP.Session
   fct_Open $strLogFileOutput
 
   $mask = "Daily_test222-alain.csv"
   $files5 = $session.EnumerateRemoteFiles($folder, $mask, [WinSCP.EnumerationOptions]::None)
   Write-Host "++++5-EnumerateRemoteFiles'$mask':"
   Write-Host "$Files5"
 
   # Disconnect, clean up
   $session.Dispose()
 
   Write-Host "=============================================================================================="
 
   $strLogFileOutput = $ScriptName + '_' + $TargetServerName + (Get-Date -Format "yyyyMMdd-HHmmss") + '_6.log'
   $session = New-Object WinSCP.Session
   fct_Open $strLogFileOutput
 
   $mask = "Daily[*].csv"
   $files6 = $session.EnumerateRemoteFiles($folder, $mask, [WinSCP.EnumerationOptions]::None)
   Write-Host "++++6-EnumerateRemoteFiles'$mask':"
   Write-Host "$Files6"
 
   # Disconnect, clean up
   $session.Dispose()
 
   Write-Host "=============================================================================================="
 
   $strLogFileOutput = $ScriptName + '_' + $TargetServerName + (Get-Date -Format "yyyyMMdd-HHmmss") + '_7.log'
   $session = New-Object WinSCP.Session
   fct_Open $strLogFileOutput
 
   $mask = "Daily_(*.csv"
   $files7 = $session.EnumerateRemoteFiles($folder, $mask, [WinSCP.EnumerationOptions]::None)
   Write-Host "++++7-EnumerateRemoteFiles'$mask':"
   Write-Host "$Files7"
 
   # Disconnect, clean up
   $session.Dispose()
 
   Write-Host "=============================================================================================="
 
   $strLogFileOutput = $ScriptName + '_' + $TargetServerName + (Get-Date -Format "yyyyMMdd-HHmmss") + '_8.log'
   $session = New-Object WinSCP.Session
   fct_Open $strLogFileOutput
 
   $mask = "Daily_(test-alain)*.csv"
   $files8 = $session.EnumerateRemoteFiles($folder, $mask, [WinSCP.EnumerationOptions]::None)
   Write-Host "++++8-EnumerateRemoteFiles'$mask':"
   Write-Host "$Files8"
 
   # Disconnect, clean up
   $session.Dispose()
 
   Write-Host "=============================================================================================="
 
   $strLogFileOutput = $ScriptName + '_' + $TargetServerName + (Get-Date -Format "yyyyMMdd-HHmmss") + '_9.log'
   $session = New-Object WinSCP.Session
   fct_Open $strLogFileOutput
 
   $mask = "Daily_(test-alain)[*].csv"
   $files9 = $session.EnumerateRemoteFiles($folder, $mask, [WinSCP.EnumerationOptions]::None)
   Write-Host "++++9-EnumerateRemoteFiles'$mask':"
   Write-Host "$Files9"
 
   # Disconnect, clean up
   $session.Dispose()
 
   Write-Host "=============================================================================================="
 
   $strLogFileOutput = $ScriptName + '_' + $TargetServerName + (Get-Date -Format "yyyyMMdd-HHmmss") + '_10.log'
   $session = New-Object WinSCP.Session
   fct_Open $strLogFileOutput
 
   $mask = "Daily_test222-alain*.csv"
   $files10 = $session.EnumerateRemoteFiles($folder, $mask, [WinSCP.EnumerationOptions]::None)
   Write-Host "++++10-EnumerateRemoteFiles'$mask':"
   Write-Host "$Files10"
 
   # Disconnect, clean up
   $session.Dispose()
 
   Write-Host "=============================================================================================="
 
   $strLogFileOutput = $ScriptName + '_' + $TargetServerName + (Get-Date -Format "yyyyMMdd-HHmmss") + '_11.log'
   $session = New-Object WinSCP.Session
   fct_Open $strLogFileOutput
 
   $mask = "Daily_test222-alain[*].csv"
   $files11 = $session.EnumerateRemoteFiles($folder, "$mask", [WinSCP.EnumerationOptions]::None)
   Write-Host "++++11-EnumerateRemoteFiles'$mask':"
   Write-Host "$Files11"
 
   # Disconnect, clean up
   $session.Dispose()
 
   Write-Host "=============================================================================================="
 
   $strLogFileOutput = $ScriptName + '_' + $TargetServerName + (Get-Date -Format "yyyyMMdd-HHmmss") + '_12.log'
   $session = New-Object WinSCP.Session
   fct_Open $strLogFileOutput
 
   $mask = "Daily*.csv"
   Write-Host "++++12- if exists condition for '$folder$mask':"
   if ($session.FileExists("$folder$mask"))
   {
       Write-Host "EXIST"
   }
   else
   {
       Write-Host "NOT exist"
   }
 
   # Disconnect, clean up
   $session.Dispose()
 
   Write-Host "=============================================================================================="
 
   $strLogFileOutput = $ScriptName + '_' + $TargetServerName + (Get-Date -Format "yyyyMMdd-HHmmss") + '_13.log'
   $session = New-Object WinSCP.Session
   fct_Open $strLogFileOutput
 
   $mask = "Daily_(test-alain).csv"
   Write-Host "++++13- if exists condition for '$folder$mask':"
   if ($session.FileExists("/BI_Broadcast/TEST/DailyProduction/$mask"))
   {
       Write-Host "EXIST"
   }
   else
   {
       Write-Host "NOT exist"
   }
 
   # Disconnect, clean up
   $session.Dispose()
 
   Write-Host "=============================================================================================="
 
   $strLogFileOutput = $ScriptName + '_' + $TargetServerName + (Get-Date -Format "yyyyMMdd-HHmmss") + '_14.log'
   $session = New-Object WinSCP.Session
   fct_Open $strLogFileOutput
 
   $mask = "Daily_%28test-alain%29.csv"
   Write-Host "++++14- if exists condition for '$folder$mask':"
   if ($session.FileExists("$folder$mask"))
   {
       Write-Host "EXIST"
   }
   else
   {
       Write-Host "NOT exist"
   }
 
   # Disconnect, clean up
   $session.Dispose()
 
   Write-Host "=============================================================================================="
 
   $strLogFileOutput = $ScriptName + '_' + $TargetServerName + (Get-Date -Format "yyyyMMdd-HHmmss") + '_15.log'
   $session = New-Object WinSCP.Session
   fct_Open $strLogFileOutput
 
   $mask = "Daily_((test-alain)).csv"
   Write-Host "++++15- if exists condition for '$folder$mask':"
   if ($session.FileExists("$folder$mask"))
   {
       Write-Host "EXIST"
   }
   else
   {
       Write-Host "NOT exist"
   }
 
   # Disconnect, clean up
   $session.Dispose()
 
   Write-Host "=============================================================================================="
 
   $strLogFileOutput = $ScriptName + '_' + $TargetServerName + (Get-Date -Format "yyyyMMdd-HHmmss") + '_16.log'
   $session = New-Object WinSCP.Session
   fct_Open $strLogFileOutput
 
   $mask = "Daily_test222-alain.csv"
   Write-Host "++++16- if exists condition for '$folder$mask':"
   if ($session.FileExists("$folder$mask"))
   {
       Write-Host "EXIST"
   }
   else
   {
       Write-Host "NOT exist"
   }
 
   # Disconnect, clean up
   $session.Dispose()
 
   Write-Host "=============================================================================================="
 
   $strLogFileOutput = $ScriptName + '_' + $TargetServerName + (Get-Date -Format "yyyyMMdd-HHmmss") + '_17.log'
   $session = New-Object WinSCP.Session
   fct_Open $strLogFileOutput
 
   $mask = "Daily[*].csv"
   Write-Host "++++17- if exists condition for '$folder$mask':"
   if ($session.FileExists("$folder$mask"))
   {
       Write-Host "EXIST"
   }
   else
   {
       Write-Host "NOT exist"
   }
 
   # Disconnect, clean up
   $session.Dispose()
 
   Write-Host "=============================================================================================="
 
   $strLogFileOutput = $ScriptName + '_' + $TargetServerName + (Get-Date -Format "yyyyMMdd-HHmmss") + '_18.log'
   $session = New-Object WinSCP.Session
   fct_Open $strLogFileOutput
 
   $mask = "Daily_(*.csv"
   Write-Host "++++18- if exists condition for '$folder$mask':"
   if ($session.FileExists("$folder$mask"))
   {
       Write-Host "EXIST"
   }
   else
   {
       Write-Host "NOT exist"
   }
 
   # Disconnect, clean up
   $session.Dispose()
 
   Write-Host "=============================================================================================="
 
   $strLogFileOutput = $ScriptName + '_' + $TargetServerName + (Get-Date -Format "yyyyMMdd-HHmmss") + '_19.log'
   $session = New-Object WinSCP.Session
   fct_Open $strLogFileOutput
 
   $mask = "Daily_(test-alain)*.csv"
   Write-Host "++++19- if exists condition for '$folder$mask':"
   if ($session.FileExists("$folder$mask"))
   {
       Write-Host "EXIST"
   }
   else
   {
       Write-Host "NOT exist"
   }
 
   # Disconnect, clean up
   $session.Dispose()
 
   Write-Host "=============================================================================================="
   
   $strLogFileOutput = $ScriptName + '_' + $TargetServerName + (Get-Date -Format "yyyyMMdd-HHmmss") + '_20.log'
   $session = New-Object WinSCP.Session
   fct_Open $strLogFileOutput
 
   $mask = "Daily_(test-alain)[*].csv"
   Write-Host "++++20- if exists condition for '$folder$mask':"
   if ($session.FileExists("$folder$mask"))
   {
       Write-Host "EXIST"
   }
   else
   {
       Write-Host "NOT exist"
   }
 
   # Disconnect, clean up
   $session.Dispose()
 
   Write-Host "=============================================================================================="
 
   $strLogFileOutput = $ScriptName + '_' + $TargetServerName + (Get-Date -Format "yyyyMMdd-HHmmss") + '_21.log'
   $session = New-Object WinSCP.Session
   fct_Open $strLogFileOutput
 
   $mask = "Daily_test222-alain*.csv"
   Write-Host "++++21- if exists condition for '$folder$mask':"
   if ($session.FileExists("$folder$mask"))
   {
       Write-Host "EXIST"
   }
   else
   {
       Write-Host "NOT exist"
   }
 
   # Disconnect, clean up
   $session.Dispose()
 
   Write-Host "=============================================================================================="
 
   $strLogFileOutput = $ScriptName + '_' + $TargetServerName + (Get-Date -Format "yyyyMMdd-HHmmss") + '_22.log'
   $session = New-Object WinSCP.Session
   fct_Open $strLogFileOutput
 
   $mask = "Daily_test222-alain[*].csv"
   Write-Host "++++22- if exists condition for '$folder$mask':"
   if ($session.FileExists("$folder$mask"))
   {
       Write-Host "EXIST"
   }
   else
   {
       Write-Host "NOT exist"
   }
 
   # Disconnect, clean up
   $session.Dispose()
PowerShell Result:
==============================================================================================
++++1-EnumerateRemoteFiles'Daily*.csv':
Daily_(test-alain).csv Daily_Prod_Paper_-_Interface_Detail_ED_(20230313).csv Daily_Prod_Paper_-_Interface_Detail_ED_20230313.csv Daily_Prod_Paper_Interface_Detail_ED_(20230313).csv Daily_Prod_Paper_Interface_Detail_ED_20230313.csv Daily_Prod_Pulp_-_Interface_Detail_ED_(20230313-20230314)_(P102).csv Daily_test222-alain.csv
==============================================================================================
++++2-EnumerateRemoteFiles'Daily_(test-alain).csv':

==============================================================================================
++++3- EnumerateRemoteFiles 'Daily_%28test-alain%29.csv':

==============================================================================================
++++4-EnumerateRemoteFiles'Daily_((test-alain)).csv':

==============================================================================================
++++5-EnumerateRemoteFiles'Daily_test222-alain.csv':
Daily_test222-alain.csv
==============================================================================================
++++6-EnumerateRemoteFiles'Daily[*].csv':

==============================================================================================
Exception calling "EnumerateRemoteFiles" with "3" argument(s): "parsing "^Daily_(.*[.]csv$" - Not enough )'s."
At C:\Batch_Production\ConnectFTPTest\TestWindSCP2.ps1:194 char:4
+    $files7 = $session.EnumerateRemoteFiles($folder, $mask, [WinSCP.En ...
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException
Exception calling "EnumerateRemoteFiles" with "3" argument(s): "parsing "^Daily_(.*[.]csv$" - Not enough )'s."
At C:\Batch_Production\ConnectFTPTest\TestWindSCP2.ps1:194 char:4
+    $files7 = $session.EnumerateRemoteFiles($folder, $mask, [WinSCP.En ...
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException

++++7-EnumerateRemoteFiles'Daily_(*.csv':

==============================================================================================
++++8-EnumerateRemoteFiles'Daily_(test-alain)*.csv':

==============================================================================================
++++9-EnumerateRemoteFiles'Daily_(test-alain)[*].csv':

==============================================================================================
++++10-EnumerateRemoteFiles'Daily_test222-alain*.csv':
Daily_test222-alain.csv
==============================================================================================
++++11-EnumerateRemoteFiles'Daily_test222-alain[*].csv':

==============================================================================================
++++12- if exists condition for '/BI_Broadcast/TEST/DailyProduction/Daily*.csv':
NOT exist
==============================================================================================
++++13- if exists condition for '/BI_Broadcast/TEST/DailyProduction/Daily_(test-alain).csv':
EXIST
==============================================================================================
++++14- if exists condition for '/BI_Broadcast/TEST/DailyProduction/Daily_%28test-alain%29.csv':
NOT exist
==============================================================================================
++++15- if exists condition for '/BI_Broadcast/TEST/DailyProduction/Daily_((test-alain)).csv':
NOT exist
==============================================================================================
++++16- if exists condition for '/BI_Broadcast/TEST/DailyProduction/Daily_test222-alain.csv':
EXIST
==============================================================================================
++++17- if exists condition for '/BI_Broadcast/TEST/DailyProduction/Daily[*].csv':
NOT exist
==============================================================================================
++++18- if exists condition for '/BI_Broadcast/TEST/DailyProduction/Daily_(*.csv':
NOT exist
==============================================================================================
++++19- if exists condition for '/BI_Broadcast/TEST/DailyProduction/Daily_(test-alain)*.csv':
NOT exist
==============================================================================================
++++20- if exists condition for '/BI_Broadcast/TEST/DailyProduction/Daily_(test-alain)[*].csv':
NOT exist
==============================================================================================
++++21- if exists condition for '/BI_Broadcast/TEST/DailyProduction/Daily_test222-alain*.csv':
NOT exist
==============================================================================================
++++22- if exists condition for '/BI_Broadcast/TEST/DailyProduction/Daily_test222-alain[*].csv':
NOT exist
==============================================================================================
  • TestWindSCP2_WinSCPLogs_20230330.log (237.43 KB, Private file)
Description: WindSCP logs files for process result 1 to 22

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,453
Location:
Prague, Czechia

Re: EnumerateRemoteFiles and FileExists not working correcly for file with parenthesis in file name

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

wwoiseau@hotmail.com
Joined:
Posts:
2

I have included the log files

Hello Martin,
I thought I had included the WinSCP log files (in private mode) when I first created this Bug report but I have attached the log file document once again.

I have merged them all in one attachment. You will find them in the attach document: TestWindSCP2_WinSCPLogs_20230330.log
  • TestWindSCP2_WinSCPLogs_20230330.log (237.43 KB, Private file)

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,453
Location:
Prague, Czechia

Re: I have included the log files

Indeed you did. I didn't notice before. Thanks.

I have added the issue with Session.EnumerateRemoteFiles to the tracker:
Issue 2177 – Session.EnumerateRemoteFiles does not work correctly when the mask includes brackets and other symbols

I'm sending you an email with a development version of WinSCP to the address you have used to register on this forum.

As for the Session.FileExists:
It does not allow wildcards:
https://winscp.net/eng/docs/library_session_fileexists

Reply with quote

Advertisement

You can post new topics in this forum