Jump to content

Open file, Save and Close - different for non standard programs?


Recommended Posts

I am trying to automate the process of opening a file in a program, saving it (this process updates parameters in the file for me) and closing the program.

First I tried FileOpen but it does not seem to open the file and does not close the program either:

run("FullProgramPath\MyProgram.exe")

WinWaitActive("MyProgram - Untitled")

Local $file = FileOpen("test.abc")

If $file = -1 Then
MsgBox(0, "Error", "Unable to open file.")
Exit
EndIf

MsgBox(0, "Got here", "Made it here")

FileClose($file)

I tried giving the full path of "test.abc", putting it in the same directory as the autoit script and using the various optional mode. The only way I seem to be able to open the file in my program is using ShellExecute("FullPath\test.abc") but then I run into the problem of not knowing how to "save" and "close".

I am aware that I didn't address the "saving" issue in my autoit code but I couldn't get that far since I wasn't able to open anything...

Any suggestions would be greatly appreciated. Thanks!

Link to comment
Share on other sites

Hi,

Welcome to the autoit forum :)

The FileOpen function contains two parameters. The first one, as you guessed is the file path, the second one is the open mode and its default value is "open for reading" and not for writing as you want it.

My advice: Highlight the function and click on F1, the help file will bring everything you need to know about it ;)

#include <FileConstants.au3>

Local Const $hFile = FileOpen("test.abc", $FO_OVERWRITE)
FileClose($hFile)

Br, FireFox.

Edited by FireFox
Link to comment
Share on other sites

Thanks for your quick response FireFox. I tried using your $FO_OVERWRITE approach but the file still does not open. The program opens, nothing shows up and it doesn't close.

Edit: Also it wipes out my file which I assume is because I chose the write mode...

Edited by katniss
Link to comment
Share on other sites

You have to use FileRead after the FileOpen if you want to actually read the file into the script. FileOpen only opens the file for read/write and returns a file handle, it doesn't actually "open" the file.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

It reads the contents of the file into a variable. See the help file for more information.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

katniss,

I am trying to automate the process of opening a file in a program, saving it (this process updates parameters in the file for me) and closing the program.

What program? Are you trying to open "test.abc" and feed the data to "myprogram.exe"? I can't figure out what you are trying to do from your code or descriptions. Can you describe your task in more detail?

Regarding file ops, if you look at the help file (as advised earlier) you will see examples of opening, reading, writing and closing files. "Saving" a file opened and altered by a script is just a matter of closing the file.

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

kylomas,

It's a proprietary program which displays things on a map, which is why I am not sure how non-standard programs work with FileOpen, FileRead, etc..

All of the examples I've been able to find so far use txt files and in the FileRead help file, the descriptions says "Read in a number of characters from a previously opened text file." which is not quite what I am trying to do. I want to run "myprogram.exe", open "test.abc" within the program, save and close. I need to do this for 100+ files every month which is why an autoit script would be amazing if I could get it to work...

The only way I've been able to open the file using my program so far is using:

ShellExecute("test.abc")

However, rather than returning a file handle like in FileOpen, it only returns success or failure. Is there a way to get a handle of the program at this point and simulate "File --> Save, Click "Save", File --> Exit"? I am also looking at the "AutoIt v3 Window Info" trying to see what sorts of parameters I can use.

Thanks for your response!

Link to comment
Share on other sites

katniss,

F.Y.I. - shellexecute opens your file because Windows invokes whatever pgm is associated with your file extention and carries out a default action (normally "open"). Script file actions have nothing to do with a program that you start with the "run" command.

Some possibilities -

- If the program you are running accepts a file as a parameter on startup then you can automate instances of this program feeding it file names

- There may be an api / scripting interface to the program, ask the vendor / author

- There may be a batch language for the program, again, ask the vendor / author

Given none of the above you are stuck with FireFox's suggestion. The forum is rife with threads for this kind of automation.

Good Luck,

kylomas

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

- If the program you are running accepts a file as a parameter on startup then you can automate instances of this program feeding it file names

Which function would you use to try this? "Run" only takes the path of the program and doesn't provide an option to pass a file name.

Link to comment
Share on other sites

If the file type is associated with the program (the program is the default program which opens on a doubleclick of the file), use shellexecute() on the file and the program will open up.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...