SSIS Script Task中VB循环异常:FTP文件筛选失效致全量复制
Hey there! Let's get your SSIS Script Task to stop copying all FTP files and only grab the ones you want. Since you mentioned the fileName.Contains statement isn't working as expected, let's break down the most common fixes for this VB script issue.
Common Reasons Contains Fails & Fixes
1. You're filtering on a full path, not just the filename
Most FTP servers return the full remote file path (like /remote/folder/MyTargetFile.csv) instead of just the filename. If you're checking Contains against this full string, it might not match what you expect, or you might accidentally match paths that include your keyword.
Fix this by extracting the pure filename first using Path.GetFileName:
' Get just the filename without the remote path Dim pureFileName As String = System.IO.Path.GetFileName(fileName) ' Now check against the pure filename If pureFileName.Contains("YourTargetKeyword") Then ' Run your file copy logic here End If
2. Case sensitivity is tripping you up
By default, VB's String.Contains is case-sensitive. So if your target file is SalesReport.csv but the FTP server has salesreport.csv, the check will fail.
Use String.IndexOf with a case-insensitive comparison instead:
If pureFileName.IndexOf("YourTargetKeyword", StringComparison.OrdinalIgnoreCase) >= 0 Then ' Run your file copy logic here End If
3. Your filter is in the wrong place in the code
Double-check that your filtering condition is placed after you get each file name, but before you execute the copy. A typical correct loop structure looks like this:
' Assume you've already fetched the list of FTP files into a collection called ftpFiles For Each fileEntry In ftpFiles Dim pureFileName As String = System.IO.Path.GetFileName(fileEntry) ' Apply your filter here If pureFileName.IndexOf("DailySales", StringComparison.OrdinalIgnoreCase) >= 0 Then ' Example: Download the file to your local directory ftpClient.DownloadFile("C:\LocalFolder\" & pureFileName, fileEntry) End If Next
4. Debug to see what filenames you're actually processing
If you're still stuck, add a debug message to see exactly what filenames your script is reading. This will help you spot if the issue is with the filename format or your filter keyword:
' Add this inside your file loop to log the current file Dts.Events.FireInformation(0, "FTP Debug", "Processing file: " & fileEntry, String.Empty, 0, False)
You can view these messages in the SSIS execution logs to verify what's being passed to your filter.
内容的提问来源于stack exchange,提问作者lumiukko

