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

时间:2014-01-04 21:44:14

标签: vbscript copy wsh

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

(仅当我在同一文件夹“sExtension”,“ExtAssign.txt”和sComputername中运行脚本时,它才有效,否则它将找不到路径)

这是应该做的 读取一个名为“ExtAssign.txt”的文件(该文件中有一个完整的计算机名列表),如果它在该文件中找到计算机名称,那么它应该复制一个文件,其中包含分配给该计算机名称的扩展名号。文件服务器为“C:\”Drive

对于这个例子,我试图在本地做这个,如果我能做到,那么我会从我的文件服务器尝试它

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中运行脚本。

我找不到文件错误

提前谢谢你,新年快乐!

1 个答案:

答案 0 :(得分:1)

罪魁祸首很可能是这一行:

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