FAQ FAQ  Forum Search   Register Register  Login Login

Redirect output to text file

 Post Reply Post Reply
Author
  Topic Search Topic Search  Topic Options Topic Options
praseetha View Drop Down
I'm new here
I'm new here


Joined: 15 May 2007
Online Status: Offline
Posts: 16
  Quote praseetha Quote  Post ReplyReply bullet Topic: Redirect output to text file
    Posted: 10 Jul 2007 at 02:30
Hi,
 
Given below is the code that i use to send the output of ipconfig to a text file.
strRun = "ipconfig >C:\Documents and Settings\inpka\Desktop\Test\New Text Document.txt"
objShell.Run strRun, 1, True which does not output to text file.
 
 
But When i run the same command in command prompt i give as
ipconfig >"C:\Documents and Settings\inpka\Desktop\Test\New Text Document.txt" i can view the output in the text file howeverin cmd prompt, It works only when i give the path within quotes
 
Could someone pld guide onthis..
 
regards,
Praseetha
 


Edited by praseetha - 10 Jul 2007 at 02:59
Back to Top
dm_4ever View Drop Down
Regular Member
Regular Member


Joined: 06 Jul 2007
Online Status: Offline
Posts: 68
  Quote dm_4ever Quote  Post ReplyReply bullet Posted: 10 Jul 2007 at 06:40
When you run it from the command prompt you add the extra quotes...so you need to do the same in your code.


strRun = "ipconfig > " & AddQuotes("C:\Documents and Settings\inpka\Desktop\Test\New Text Document.txt")
objShell.Run strRun, 1, True

Function AddQuotes(strInput)
    AddQuotes = Chr(34) & strInput & Chr(34)
End Function

Back to Top
praseetha View Drop Down
I'm new here
I'm new here


Joined: 15 May 2007
Online Status: Offline
Posts: 16
  Quote praseetha Quote  Post ReplyReply bullet Posted: 11 Jul 2007 at 07:27
Hi,
 
Given below is the code I used:
 
Dim objShell,objFile,objFso,strTmp,strCommand,strInput

Set objShell = WScript.CreateObject("WScript.Shell")
Set objFso = CreateObject("Scripting.FileSystemObject")
strTmp = "C:\Documents and Settings\inpka\Desktop\Test\textfile.txt"

strCommand = "ipconfig > " & Quote(strTmp)
objShell.Run strCommand,7,True 
 
Function Quote(s)   
 Quote = Chr(34) & CStr(s) & Chr(34)
End Function 
 
This did not forward output to text file.
This is smething very critical for us. Is there a way I can make this happen.
 
Thanks,
 
Praseetha
Back to Top
AbqBill View Drop Down
Frequent Contributor
Frequent Contributor


Joined: 31 Aug 2006
Online Status: Offline
Posts: 246
  Quote AbqBill Quote  Post ReplyReply bullet Posted: 11 Jul 2007 at 09:30
Hi Praseetha,

Output redirection is a feature of the command shell. The fix is to call cmd.exe to execute your command. Here's an example in JScript that's pretty easily adapted to VBScript:

var fso, wshshell, tempfile, cmdline, ts, data;

fso = new ActiveXObject("Scripting.FileSystemObject");
wshshell = new ActiveXObject("WScript.Shell");

// Generate temporary filename in %Temp%
do {
  tempfile = fso.BuildPath(fso.GetSpecialFolder(2), fso.GetTempName());
} while (fso.FileExists(tempfile));

// Create the command line
cmdline = wshshell.ExpandEnvironmentStrings("%SystemRoot%\\system32\\cmd.exe"
  + " /c %SystemRoot%\\system32\\ipconfig.exe > " + tempfile);

// Run the command; outputs to the temporary file
wshshell.Run(cmdline, 0, true);

// Open the temporary file as a TextStream object
ts = fso.OpenTextFile(tempfile, 1);
// Read the file into a variable
data = ts.ReadAll();
// Close the TextStream object
ts.Close();
// Delete the temporary file
fso.DeleteFile(tempfile);

HTH,

Bill
Back to Top
joseomjr View Drop Down
Regular Member
Regular Member


Joined: 31 Aug 2006
Online Status: Offline
Posts: 68
  Quote joseomjr Quote  Post ReplyReply bullet Posted: 11 Jul 2007 at 10:34
This should be the same as what Bill showed you in VBScript

strRun = "%comspec% /c ipconfig.exe > " & AddQuotes("C:\Documents and Settings\inpka\Desktop\Test\New Text Document.txt")
objShell.Run strRun, 1, True

Function AddQuotes(strInput)
    AddQuotes = Chr(34) & strInput & Chr(34)
End Function

Back to Top
AbqBill View Drop Down
Frequent Contributor
Frequent Contributor


Joined: 31 Aug 2006
Online Status: Offline
Posts: 246
  Quote AbqBill Quote  Post ReplyReply bullet Posted: 11 Jul 2007 at 15:00
Hi joseomjr,

Don't forget to call the ExpandEnvironmentStrings method to expand the %ComSpec% environment variable.

HTH,

Bill
Back to Top
dm_4ever View Drop Down
Regular Member
Regular Member


Joined: 06 Jul 2007
Online Status: Offline
Posts: 68
  Quote dm_4ever Quote  Post ReplyReply bullet Posted: 11 Jul 2007 at 16:24
You don't need to in this case. .Run will know what to do with it.
Back to Top
AbqBill View Drop Down
Frequent Contributor
Frequent Contributor


Joined: 31 Aug 2006
Online Status: Offline
Posts: 246
  Quote AbqBill Quote  Post ReplyReply bullet Posted: 12 Jul 2007 at 09:11
Hi dm_4ever,

Good tip. It's even documented in the docs for the Run method. Thanks.

Bill
Back to Top
praseetha View Drop Down
I'm new here
I'm new here


Joined: 15 May 2007
Online Status: Offline
Posts: 16
  Quote praseetha Quote  Post ReplyReply bullet Posted: 13 Jul 2007 at 03:34
Hi All,
 
strRun = "%comspec% /c ipconfig > " & AddQuotes("C:\Documents and Settings\inpka\Desktop\Test\New Text Document.txt")
objShell.Run strRun, 1, True

Function AddQuotes(strInput)
    AddQuotes = Chr(34) & strInput & Chr(34)
End Function
 This outputs teh result to a text file.
 
Now, I have written a script which will go and check some files. Based on the file name the script gets(based on condition set),
I need to run a command say "Eseutil /mk xyz.chk" in I:\logs directory in command prompt.
Can someone pls suggest as to how i can put the above command in "%comspec% /c ipconfig > " ( instead of ipconfig, i need to execute this command. 
The problem is that the command line should execute this comand frmo I:\logs. How can i have this done?
 
Thanks,
Praseetha 
Back to Top
AbqBill View Drop Down
Frequent Contributor
Frequent Contributor


Joined: 31 Aug 2006
Online Status: Offline
Posts: 246
  Quote AbqBill Quote  Post ReplyReply bullet Posted: 16 Jul 2007 at 08:34
Hi Praseetha,

I believe you can use the CurrentDirectory property of the WshShell object to set the current working directory for a script. For example:

Set WshShell = CreateObject("WScript.Shell")
WshShell.CurrentDirectory = "I:\Logs"

HTH,

Bill
Back to Top
praseetha View Drop Down
I'm new here
I'm new here


Joined: 15 May 2007
Online Status: Offline
Posts: 16
  Quote praseetha Quote  Post ReplyReply bullet Posted: 18 Jul 2007 at 00:20
Hi All..
 
Thanks a lot for your assistance:)
 
Regards,
Praseetha
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down