0

这是我想要做的。

该脚本一次读取一个计算机名称的文本文件。

检查是否存在 program.lnk。如果该链接存在,它会将新链接复制到该计算机/用户配置文件。然后它在一个新的文本文件中记录计算机名称和程序存在。然后它会删除原始的 program.lnk 文件。

如果 program.lnk 不存在,它会将计算机名称和该程序不存在记录到同一文本文件中。

scipt 然后移动到列表中的下一个计算机名称。

如果 ComputerListModified.txt 文件不存在,我无法创建它,但如果文件存在则附加该文件。

这是我的代码....

InputFile = "C:\scripts\computers.txt"
Const DeleteReadOnly = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(InputFile)
Do While Not (objFile.AtEndOfStream)
strComputer = objFile.ReadLine
For Each objsubfolder In objFSO.GetFolder("\\" & strComputer & "\c$\Documents and    Settings\").subfolders

If objFSO.FileExists(objsubfolder.Path & "\desktop\Wellcore Operations.LNK") Then
objFSO.CopyFile "\\pc01130\c$\scripts\Wellcore OperationsReadOnly.lnk", (objsubfolder.Path & "\desktop\")
outFile="c:\scripts\ComputerListModified.txt"
Set objFile = objFSO.CreateTextFile(outFile,True)
objFile.Write strComputer: objFile.Write  " This Computer had Wellcore Operations" & vbCrLf
objFile.Close

Else

outFile="c:\scripts\ComputerListModified.txt"
Set objFile = objFSO.CreateTextFile(outFile,True)
objFile.Write strComputer: objFile.Write " This Computer did not have Wellcore Operations" & vbCrLf
objFile.Close

End If

If objFSO.FileExists(objsubfolder.Path & "\desktop\Wellcore Operations.LNK") Then
objFSO.DeleteFile (objsubfolder.Path & "\desktop\Wellcore Operations.LNK")

End If
Next
Loop

MsgBox "Done"
4

1 回答 1

1

如果您想在(深度嵌套的)重复操作中记录相关事实,则必须在最里面的循环中写入文件。但是准备(和关闭)文件是一项顶级任务,不应该在循环中完成。

盖茨先生确实正确设计了 .OpenTextFile 方法。它可以在没有任何 .FileExists 检查的情况下使用。

演示代码:

Option Explicit

Const ForAppending = 8

Dim goFS  : Set goFS  = CreateObject("Scripting.FileSystemObject")
Dim gsLog : gsLog     = ".\logdemo.log"
WScript.Echo gsLog, "exists:", CStr(goFS.FileExists(gsLog))
'                           .OpenTextFile(filename[, iomode[, create[, format]]])
Dim goLog : Set goLog = goFS.OpenTextFile(gsLog, ForAppending, True)
goLog.WriteLine Now & " start"

Dim sComputer
For Each sComputer In Split("alpha beta gamma")
    Dim sFolder
    For Each sFolder in Split("A B C")
        goLog.WriteLine Join(Array(Now, sComputer, sFolder))
    Next
Next

goLog.WriteLine Now & " end"
goLog.WriteLine "-------------------------"
goLog.Close

输出:

cscript logdemo.vbs
.\logdemo.log exists: False

type logdemo.log
6/12/2013 9:25:26 PM start
6/12/2013 9:25:26 PM alpha A
6/12/2013 9:25:26 PM alpha B
6/12/2013 9:25:26 PM alpha C
6/12/2013 9:25:26 PM beta A
6/12/2013 9:25:26 PM beta B
6/12/2013 9:25:26 PM beta C
6/12/2013 9:25:26 PM gamma A
6/12/2013 9:25:26 PM gamma B
6/12/2013 9:25:26 PM gamma C
6/12/2013 9:25:26 PM end
-------------------------

cscript logdemo.vbs
.\logdemo.log exists: True

type logdemo.log
6/12/2013 9:25:26 PM start
6/12/2013 9:25:26 PM alpha A
6/12/2013 9:25:26 PM alpha B
6/12/2013 9:25:26 PM alpha C
6/12/2013 9:25:26 PM beta A
6/12/2013 9:25:26 PM beta B
6/12/2013 9:25:26 PM beta C
6/12/2013 9:25:26 PM gamma A
6/12/2013 9:25:26 PM gamma B
6/12/2013 9:25:26 PM gamma C
6/12/2013 9:25:26 PM end
-------------------------
6/12/2013 9:26:00 PM start
6/12/2013 9:26:00 PM alpha A
6/12/2013 9:26:00 PM alpha B
6/12/2013 9:26:00 PM alpha C
6/12/2013 9:26:00 PM beta A
6/12/2013 9:26:00 PM beta B
6/12/2013 9:26:00 PM beta C
6/12/2013 9:26:00 PM gamma A
6/12/2013 9:26:00 PM gamma B
6/12/2013 9:26:00 PM gamma C
6/12/2013 9:26:00 PM end
-------------------------
于 2013-06-12T19:36:07.143 回答