Solved

How do I append DOS output to a file from an AutoIt script?

Posted on 2007-11-29
9
6,657 Views
Last Modified: 2008-02-01
I've created an AutoIt script that gives me quick access to commonly used programs I use to perform routine maintenance on the computers at the office where I work.

One function I use is the NET TIME command so I can verify that the computer is set to receive the time from our server. I am trying to record the output of this command to a log file for future reference but my script is not logging it.

I have these variables initialized at the beginning of my script:

$path="\\lrecdops-dc3\d$\home_folders\michael\maintenance\"
$file=$path & @ComputerName & ".txt"

And here is the code from the segment that runs the NET TIME command:

RunWait("net time >> " & $file)
FileOpen ($file, 1 )
FileWriteLine($file, _Now() & " - Ran NET TIME check" & @LF)
FileClose ($file)      

When I run the script, the DOS window flashes, but when I check the log all I see is the line saying it ran the NET TIME check.  

How do I get the script to append the NET TIME output to the log file?
0
Comment
Question by:mjbtec
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
9 Comments
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 20376263
If there's any whitespace in the filename, then you won't get the required output. What you want to do, is wrap quotation marks around the file name.

I'm not familiar with the AutoIT language, so you'll have to help me a bit here:
How do you include quotation marks inside a string?

In most other languages, you prepend a backslash, like so:

RunWait("net time >> \"" & $file & "\"")

But based solely on the code you've presented above, it would seem unlikely that this is the correct syntax in AutoIT..
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 20376277
Futhermore, it would seem that the net time application writes it's output to a different stream; try this instead:

RunWait("net time 2> " & $file)
0
 

Author Comment

by:mjbtec
ID: 20382382
Well, this is getting quite interesting.

When I run my script and invoke the NET TIME command first, it works fine.

RunWait(@ComSpec & " /k " & "net time >>" & $file)            

BUT...

When I have the script write anything to the log file and then try invoking the NET TIME command, I get this error:

The process cannot access the file because it is being used by another process.

That's what was in the DOS window when it flashed. Normally NET TIME takes a few seconds to run.

I think the script is somehow keeping the log file open so NET TIME can't write anything to it.

I'm taking this to the forums on AutoIt's web site and will come back here when I find something out.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Accepted Solution

by:
mjbtec earned 0 total points
ID: 20382844
Someone at the AutoIt forums suggested that I close the handle created by FileOpen() function. This fixed my problem.

Here's the modified code that works:



$FileOpen = FileOpen ($file, 1)
FileWriteLine($file,"Output of NET TIME check:" & @LF)
FileClose ($FileOpen)
RunWait(@ComSpec & " /k " & "net time >>" & $file)	

Open in new window

0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 20383678
Excellent, thanks for letting us know the solution.
I'll have this thread PAQ'd, and your points refunded.
0
 

Author Comment

by:mjbtec
ID: 20405679
To answer your question about inserting quotes in a string, I use the CHR function to load its corresponding ASCII code, which is 43.

So if I wanted to use quotes in this string:

net time >> \"" & $file & "\"

I would use

net time >> \" & chr(34) & " & $file & " & chr(34) & "\
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 20405901
Nice one, thank you.
0
 
LVL 1

Expert Comment

by:modus_operandi
ID: 20412434
Closed, 250 points refunded.
modus_operandi
EE Moderator
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this post we will learn different types of Android Layout and some basics of an Android App.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question