Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net SSIS-脚本任务:使用带有动态文件路径的VBscript替换文本文件中的文本_Vb.net_Vbscript_Ssis - Fatal编程技术网

Vb.net SSIS-脚本任务:使用带有动态文件路径的VBscript替换文本文件中的文本

Vb.net SSIS-脚本任务:使用带有动态文件路径的VBscript替换文本文件中的文本,vb.net,vbscript,ssis,Vb.net,Vbscript,Ssis,我正在使用SSIS脚本任务替换文本文件中的文本。在我的VB脚本中,在脚本中硬编码文件路径,但我想使用用户变量 Const ForReading = 1 Const ForWriting = 2 Dim objFSO, objFile, strText, strNewText objFSO = CreateObject("Scripting.FileSystemObject") objFile = objFSO.OpenTextFile("C:\Users\newtons\Desktop\Tex

我正在使用SSIS脚本任务替换文本文件中的文本。在我的VB脚本中,在脚本中硬编码文件路径,但我想使用用户变量

Const ForReading = 1
Const ForWriting = 2
Dim objFSO, objFile, strText, strNewText

objFSO = CreateObject("Scripting.FileSystemObject")
objFile = objFSO.OpenTextFile("C:\Users\newtons\Desktop\Text.txt", ForReading)

strText = objFile.ReadAll
objFile.Close()
strNewText = Replace(strText, "Jim", "James")

objFile = objFSO.OpenTextFile("C:\Users\newtons\Desktop\Text.txt", ForWriting)
objFile.WriteLine(strNewText)
objFile.Close()
objFSO = Nothing
objFile = Nothing

在全局定义变量后,您可以使用ReadOnlyVariables和/或ReadWriteVariables将客户变量传递到选项卡脚本中的[task script];看一看

更多详细信息:
要将变量添加到项目中,您可以选择包左上角的[variables]选项卡,然后插入requirend字段:

通过将变量选择为ReadOnlyVariables和/或ReadWriteVariables,可以将变量添加到[任务脚本]:

因此,您可以通过声明:
Dts.Variables(“用户::变量”).Value


正如@Matt所说,在线文档省略了“User:”

使用@Alex的答案添加和引用变量。但仅供参考,如果使用System.IO,您可以用两行代码完成整个脚本

因此,将其添加到导入部分

Import System.IO
然后在您的主Sub中使用这些行

Dim filePath As String = Dts.Variables("User::FullFilePath").Value.ToString()
File.WriteAllText(filePath, File.ReadAllText(filePath).Replace("Jim", "James"))

我只想指出,如果我不使用“USER::”标识符,例如Dts.Variables(“USER::VariableName”)。我知道的Value方法很奇怪,因为微软的文档说你不应该使用它@M.Mehta出错不会给Alex或社区任何可能出错的细节。你应该用你当前尝试和错误的细节更新你的帖子。Matt,我不明白你到底想说什么。我发现很少有VB脚本是用用户变量编写的,当然不是用来替换文件中的文本的。因此,如果我想使用变量而不是硬编码的路径,我正在试图弄清楚这个脚本将如何工作!!多亏了亚历克斯和马特。