Append txt file using VBS
Author |
Message |
Chue #1 / 3
|
 Append txt file using VBS
Hey, I have 2 questions about appending a text file using Visual Basic Script. Hopefully someone out there can help me out (if i'm in the right group that is). Question 1: I have a script which changes the Local Administrator password on all computers listed in a text file. Change = "%comspec% /c cusrmgr.exe -u Administrator -m \\" & txtcomputername & " -P " & txtpassword & ">c:\Password\results.txt" The result is written to results.txt, the problem is after each change it enters the result on top of the previous in the file, and after changing say 20 computers, the result file only ever contains the last computer attempted. How do I set it so it will start at the end of the file each time? Question 2: I have another script which pings all computers listed in a text file. Using the results it generates a CSV simply giving the following information: computer1,0 computer2,1 computer3,1 (0 = no reply, 1 = reply) I want to be able to run this script every hour, and to add to the CSV these results, so it ends up looking something like this: computer1,0,0,0,0,1,1,1,1,0,0,0,0 computer2,1,1,1,1,1,1,1,1,1,1,1,1 computer3,1,1,1,1,0,0,0,0,0,0,1,1 This way we can easily see when a computer is turned on, how long for, and when switched off. The problem is, how do I set it to Append to the apropriate line? There should be some way for it to: Search for the computer name If its not there, add it to bottom Move to end of line Input result (1 or 0) I just dunno how to do it. If anyone can point me in right direction or knows of a good VBS help site I'd very much appreciate it. Thanx in advance for any help Chuey
|
Mon, 13 Sep 2004 20:03:07 GMT |
|
 |
Chris Hornberg #2 / 3
|
 Append txt file using VBS
Re: Problem #1: Dim fso Set fso = CreateObject("SCRIPTING.FileSystemObject") Dim fil Dim strm Set strm = fso.OpenTextFile("c:\file.txt", ForAppending, True) strm.WriteLine "This is a test... this is only a test." strm.Close Set fso = Nothing Problem #2: Read the file in as a complete text blob, split it on vbCrLf into a text array. Locate the entry in question and append the new reponse to it, then rejoin the array using vbCrLf. quick-code (compiled VB): dim s as string dim nFile as long dim sFilename as string dim lines() as string dim v as variant sfilename = "c:\thefile.txt" nfile = freefile open sfilename for input as nfile s = input$(lof(nFile), nFile) close #nfile lines = split(s,vbcrlf) for each v in lines if left$(v,len(currentComputerName)) = currentComputerName then v = v & "," & cstr(newReturnCode) end if next s = join(lines,vbcrlf) '// open and output s to the filename here. Hope that helps. ------------------------------------- Quote:
> Hey, > I have 2 questions about appending a text file using Visual Basic > Script. Hopefully someone out there can help me out (if i'm in the > right group that is). > Question 1: > I have a script which changes the Local Administrator password on all > computers listed in a text file. > Change = "%comspec% /c cusrmgr.exe -u Administrator -m \\" & > txtcomputername & " -P " & txtpassword & ">c:\Password\results.txt" > The result is written to results.txt, the problem is after each change > it enters the result on top of the previous in the file, and after > changing say 20 computers, the result file only ever contains the last > computer attempted. How do I set it so it will start at the end of the > file each time? > Question 2: > I have another script which pings all computers listed in a text file. > Using the results it generates a CSV simply giving the following > information: > computer1,0 > computer2,1 > computer3,1 > (0 = no reply, 1 = reply) > I want to be able to run this script every hour, and to add to the CSV > these results, so it ends up looking something like this: > computer1,0,0,0,0,1,1,1,1,0,0,0,0 > computer2,1,1,1,1,1,1,1,1,1,1,1,1 > computer3,1,1,1,1,0,0,0,0,0,0,1,1 > This way we can easily see when a computer is turned on, how long for, > and when switched off. The problem is, how do I set it to Append to > the apropriate line? There should be some way for it to: > Search for the computer name > If its not there, add it to bottom > Move to end of line > Input result (1 or 0) > I just dunno how to do it. If anyone can point me in right direction > or knows of a good VBS help site I'd very much appreciate it. > Thanx in advance for any help > Chuey
|
Tue, 14 Sep 2004 04:28:37 GMT |
|
 |
Alex K. Angelopoulo #3 / 3
|
 Append txt file using VBS
Other than what Chris said - + If you *are* doing things from the shell, the append operator is ">>"; Using ">" means overwrite. So your command would be: Change = "%comspec% /c cusrmgr.exe -u Administrator -m \\" & txtcomputername & " -P " & txtpassword & ">>c:\Password\results.txt" + There are VBScript and WSH specific groups also: microsoft.public.scripting.vbscript microsoft.public.scripting.wsh If your news server doesn't have them, you can get directly access them from Microsoft through the server news.microsoft.com Quote:
> Re: Problem #1: > Dim fso > Set fso = CreateObject("SCRIPTING.FileSystemObject") > Dim fil > Dim strm > Set strm = fso.OpenTextFile("c:\file.txt", ForAppending, True) > strm.WriteLine "This is a test... this is only a test." > strm.Close > Set fso = Nothing > Problem #2: > Read the file in as a complete text blob, split it on vbCrLf into a > text array. Locate the entry in question and append the new reponse to > it, then rejoin the array using vbCrLf. > quick-code (compiled VB): > dim s as string > dim nFile as long > dim sFilename as string > dim lines() as string > dim v as variant > sfilename = "c:\thefile.txt" > nfile = freefile > open sfilename for input as nfile > s = input$(lof(nFile), nFile) > close #nfile > lines = split(s,vbcrlf) > for each v in lines > if left$(v,len(currentComputerName)) = currentComputerName then > v = v & "," & cstr(newReturnCode) > end if > next > s = join(lines,vbcrlf) > '// open and output s to the filename here. > Hope that helps. > -------------------------------------
> > Hey, > > I have 2 questions about appending a text file using Visual Basic > > Script. Hopefully someone out there can help me out (if i'm in the > > right group that is). > > Question 1: > > I have a script which changes the Local Administrator password on all > > computers listed in a text file. > > Change = "%comspec% /c cusrmgr.exe -u Administrator -m \\" & > > txtcomputername & " -P " & txtpassword & ">c:\Password\results.txt" > > The result is written to results.txt, the problem is after each change > > it enters the result on top of the previous in the file, and after > > changing say 20 computers, the result file only ever contains the last > > computer attempted. How do I set it so it will start at the end of the > > file each time? > > Question 2: > > I have another script which pings all computers listed in a text file. > > Using the results it generates a CSV simply giving the following > > information: > > computer1,0 > > computer2,1 > > computer3,1 > > (0 = no reply, 1 = reply) > > I want to be able to run this script every hour, and to add to the CSV > > these results, so it ends up looking something like this: > > computer1,0,0,0,0,1,1,1,1,0,0,0,0 > > computer2,1,1,1,1,1,1,1,1,1,1,1,1 > > computer3,1,1,1,1,0,0,0,0,0,0,1,1 > > This way we can easily see when a computer is turned on, how long for, > > and when switched off. The problem is, how do I set it to Append to > > the apropriate line? There should be some way for it to: > > Search for the computer name > > If its not there, add it to bottom > > Move to end of line > > Input result (1 or 0) > > I just dunno how to do it. If anyone can point me in right direction > > or knows of a good VBS help site I'd very much appreciate it. > > Thanx in advance for any help > > Chuey
|
Wed, 15 Sep 2004 19:34:16 GMT |
|
|
|