Solved

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

Posted on 2007-11-29
9
6,480 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSumClump challenge 9 103
word0 challenge 4 66
C# Error - Add Failed 12 78
How to obtain Administrator permission when you are the Administrator 6 66
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now