Vbscript 如何复制从列表中读取的文件

Vbscript 如何复制从列表中读取的文件,vbscript,copy,wsh,Vbscript,Copy,Wsh,大家好,我对上面的代码有一个或多个问题 我试图让“sExtension”在另一个文件夹中搜索,而不是我用来保存脚本的文件夹,因为这个脚本将在许多计算机上用作启动脚本 (仅当我在同一文件夹“sExtension”、“ExtAssign.txt”和scocomputername中运行脚本时,它才起作用,否则它找不到路径) 这就是它应该做的 读取一个名为“ExtAssign.txt”的文件(该文件中有一个完整的计算机名列表),如果在该文件中找到计算机名,则应将一个扩展名为的文件从文件服务器复制到“C:

大家好,我对上面的代码有一个或多个问题 我试图让“sExtension”在另一个文件夹中搜索,而不是我用来保存脚本的文件夹,因为这个脚本将在许多计算机上用作启动脚本

(仅当我在同一文件夹“sExtension”、“ExtAssign.txt”和scocomputername中运行脚本时,它才起作用,否则它找不到路径)

这就是它应该做的 读取一个名为“ExtAssign.txt”的文件(该文件中有一个完整的计算机名列表),如果在该文件中找到计算机名,则应将一个扩展名为的文件从文件服务器复制到“C:\”驱动器

在本例中,我尝试在本地执行此操作,如果可以,我将在文件服务器上尝试

Set objFSO    = CreateObject("Scripting.FileSystemObject")
set oFso       = CreateObject("Scripting.FileSystemObject")
Set objFS      = CreateObject("Scripting.FileSystemObject")
Set fso        = CreateObject("Scripting.FileSystemObject")
set oShell     = WScript.CreateObject("WScript.Shell")
set oShellEnv  = oShell.Environment("Process")
Set folder     = Fso.GetFolder("C:\Users\XXXXX\Desktop\Test\Extensions\")
Set wshshell   = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
Set ObjEnv     = WshShell.Environment("Process")
Set objFso     = WScript.CreateObject("Scripting.FileSystemObject")
Scomputername  = ObjEnv("COMPUTERNAME")
Set objFSO = CreateObject("Scripting.FileSystemObject")
set objWShell = wScript.createObject("WScript.Shell")

Dim strFile
'File to scan
strFile = "C:\Users\XXXXX\Desktop\Test\Extensions\Extassign\ExtAssign.txt"

Dim strPattern
'Look for computer name in file
strPattern = scomputername

Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfStream
 Dim strLine
    'Read each line and store it in strLine
    strLine = objFile.ReadLine
    'If the line matches the computer name, save the line to ExtArray
    If InStr(strLine,strPattern)>0 Then
        Dim ExtArray
        'Split the line and separate the extension
        ExtArray = Split(strLine,"|", -1, 1)
    Dim sExtension
        'Save the extension to sExtension
        sExtension=ExtArray(1)
    End If
Loop
'If the sExtension is empty, computer was not found, send message and terminate script.
If sExtension="" Then
WScript.Echo "ERROR: Computer "& scomputername &" not found in Extension Assignment List, so no extension has been set.  Avaya will not be launched.  Please contact your IT department for assistance."
Else
    'If the sExtension contains a number, Copy that file to C:\ and rename it to Config.xml
    fso.CopyFile "C:\Users\XXXXX\Desktop\Test\Extensions\ "& sExtension &"", "C:\Config.xml", True
End If
最后,如果找到文件sExtension,它会将其重命名为Config.xml,但它不会这样做,除非我在同一文件夹sExtension和sComputername中运行脚本

我得到文件找不到错误


提前谢谢你,新年快乐

罪魁祸首很可能是这句话:

fso.CopyFile "C:\Users\XXXXX\Desktop\Test\Extensions\ "& sExtension &"", "C:\Config.xml", True
路径中最后一个反斜杠后面有一个尾随空格,因此您正在创建一个路径

C:\Users\XXXXX\Desktop\Test\Extensions\ 12345
                                       ^
当你真正想要一条路径的时候

C:\Users\XXXXX\Desktop\Test\Extensions\12345

更一般地说:为什么要创建7个(!)
FileSystemObject
实例(除此之外还要替换其中一个实例三次)?和3(!)
WScript.Shell
实例?您甚至不使用它们中的大多数,更不用说您首先不需要
Shell
对象。您只使用它来确定计算机名,这可以通过使用
WScript.Network
对象(您根本不用)来完成

此外,请不要使用这样的评论:

'Read each line and store it in strLine
strLine = objFile.ReadLine
很明显,您读取每一行并将其分配给变量
strLine
。注释不应该重新表述您正在做的事情(代码已经这样做了,至少当您使用变量和函数名时是这样),而是应该重新表述您为什么要这样做,即特定代码部分的用途是什么

您的代码可以简化为以下内容:

Set fso = CreateObject("Scripting.FileSystemObject")
Set net = CreateObject("WScript.Network")

computername = net.ComputerName
foldername   = "C:\Users\XXXXX\Desktop\Test\Extensions"
filename     = fso.BuildPath(foldername, "Extassign\ExtAssign.txt")

Set f = fso.OpenTextFile(filename)
Do Until f.AtEndOfStream
  line = f.ReadLine
  If InStr(line, computername) > 0 Then
    arr = Split(line, "|", -1, 1)
    If UBound(arr) >= 1 Then extension = arr(1)
  End If
Loop
f.Close

If IsEmpty(extension) Then
  WScript.Echo "ERROR: Computer "& computername &" not found in ..."
Else
  fso.CopyFile fso.BuildPath(foldername, extension), "C:\Config.xml", True
End If

这是Visual Basic吗?您可能希望相应地标记您的问题。