Solved

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

Posted on 2007-11-29
9
6,534 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
  • 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

809 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