EnumerateRemoteFiles and FileExists not working correcly for file with parenthesis in file name
I am using the
PowerShell script:
PowerShell Result:
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()
============================================================================================== ++++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 ==============================================================================================