开发者

Shouldnt This not Throw an "Already Open Error" when going through a text file

I am running a few commands on the domain computers. I would like to test it before I actually use it in the "bigger picture". When I run this it returns data for the computers that it can connect to (no admin machine right now).

But for example if I list My personal Computer twice in the txt file it reads from, it says File is already being used and skips me.

Is this an error because its the same computer? I would like it to be able to just go ahead and write the duplicate if it comes across one.

开发者_JS百科
Sub HandleInfo()
    'Declare Command
        Dim sCommand = "pushd \\*\C$ && whoami.exe >> C:\JavaInfo.txt && java.exe -version 2>> C:\JavaInfo.txt"
'declare File where Users are Located
        Dim strFile As String = "C:\TestDUsers.txt"
    'declare Lines in User.Txt File
        Dim strLines() As String = IO.File.ReadAllLines(strFile)
         For Each strUserName As String In strLines
        Dim ReplaceCommand As String = sCommand.Replace("*", strUserName)
        Shell("cmd.exe /c" & ReplaceCommand)
    Next


Change it to :

Shell("cmd.exe /c" & ReplaceCommand, AppWinStyle.Hide, True, -1)

This will make it wait until the command has finished executing. The -1 means it will not timeout, it'll wait forever, so you can change that as you wish to suit.

More info:

http://msdn.microsoft.com/en-us/library/xe736fyk(v=vs.71).aspx?ppud=4

Also, I'd change:

 Dim sCommand = "pushd \\*\C$ && whoami.exe >> C:\JavaInfo.txt && java.exe -version 2>> C:\JavaInfo.txt"

To this, using double quotes, as cmd strings can be funny with spaces.

Dim sCommand = """pushd \\*\C$ && whoami.exe >> C:\JavaInfo.txt && java.exe -version 2>> C:\JavaInfo.txt"""


The error occurs because javainfo.txt ia already in use.

To confirm the same just add * to file name. e.g. javainfo*.txt

If its confirmed remove * from file name and set wait= true in shell function and your code will work

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜