尝试这个 :-
将Foreach Loop
枚举源文件夹,路径将存储在变量中。在script task
编写代码以使用正则表达式获取文件夹名称。脚本任务值将存储在另一个变量中,该变量将用于File System Task
包装设计将是

在上面的DestLoc
变量表达式中,根据您的位置进行更改
- ForEach 循环配置


根据需要更改源文件夹位置
脚本任务 - 添加 2 变量如下

您需要从变量中提取文件夹名称LoopFiles
例子
LoopFiles
变量将D:\ForLoop\SampleFolder1.txt
在运行时
因此,为了从上述变量中提取文件夹名称,请使用正则表达式
打开Edit Script
并编写以下代码
List<string> filePatterns = null;
public void Main()
{
filePatterns = new List<string>();
filePatterns.Add("Folder1");
filePatterns.Add("Folder2");
string fileName = Path.GetFileNameWithoutExtension(Dts.Variables["User::LoopFiles"].Value.ToString());
Match match = Regex.Match(fileName, string.Join("|", filePatterns.ToArray()));
Dts.Variables["User::FolderName"].Value = match.Value;
Dts.TaskResult = (int)ScriptResults.Success;
}
在上面的代码中,您正在提取文件夹名称并将其存储在变量中FolderName
。如果有multiple folders
,则只需将 添加folder names
到filePatterns
集合变量中。
