Path to File?

Hi Again,

I’ve written a script that outputs results to a text file in a specific format. I can read and write the file just fine. However, I can’t seem to tell Eggplant where to put the file. What I’ve been doing is:

open file "/volumes/Server/MyFolder/ResultFolder/foo.txt" for appending

With the path included, Eggplant will not write the file. If I lop the path off, it sticks it at the same level as the suite.

I’m sure it’s something simple as always…

Allen

Allen,

It appears you’re doing everything exactly right. I tried an equivalent command here, including accessing a remote filesystem mounted in the /Volumes folder and it worked perfectly, creating the file when it didn’t exist.

The most likely reason I can think of for this to fail would be insufficient privileges for accessing or creating the file. You might try some different variations on the command, including creating the file in a folder on the local disk.

Also, be aware that while the open file command will create the file if it doesn’t already exist, the file’s parent folder must already exist. The create file command will create the full path to a file as needed, but in the current version open file won’t. This may change in the future so that all file creation commands will create the full path.

If you need to find out whether the file already exists, you can use the “there is a file” operator, as shown here:

put "/full/path/to/file" into fullPathName
if there is not a file fullPathName then create file fullPathName
open file fullPathName for appending

I hope that helps. Oh, yes, as you noticed, if you don’t specify the full path to a file, it is assumed to be in the current folder (specified by the folder global property).

doug–

Thanks for the response.

This is what I was running into. This might be good to mention in the documentation in both the create file and open file sections. I (incorrectly) assumed that since open created the file, it’d create the path too.

I hope that helps. Oh, yes, as you noticed, if you don’t specify the full path to a file, it is assumed to be in the current folder (specified by the folder global property).

Thanks for the tip, and for setting me straight on this one.

Allen

While we are on the subject, I should probably mention a couple of other things. First, SenseTalk will also create the full path if you simply treat the file as a container and store something into it:

put newContents into file "/full/path/to/nonexistent/file"

Treating a file as a container (like a variable, but one that happens to be stored permanently on disk) is by far the easiest way to work with a file. The open file and related commands are usually only needed in relatively rare situations where there will be a lot of ongoing input and/or output over the course of a script run (you might, for example, open a log file at the beginning of a script and then write to it periodically throughout the run).

For most situations, you will be better served by simply putting a file into a variable (to read it in), or putting a variable into a file (to write it).

[quote=“SenseTalkDoug”]While we are on the subject, I should probably mention a couple of other things. First, SenseTalk will also create the full path if you simply treat the file as a container and store something into it:

put newContents into file "/full/path/to/nonexistent/file"

Treating a file as a container (like a variable, but one that happens to be stored permanently on disk) is by far the easiest way to work with a file.
[/quote]

Thanks for the tip. I’ve got some file manipulation to do and this will certainly help. I’m getting my head around the concept that I’m not working with just variables, I’m working with something more sophistcated.

The open file and related commands are usually only needed in relatively rare situations where there will be a lot of ongoing input and/or output over the course of a script run (you might, for example, open a log file at the beginning of a script and then write to it periodically throughout the run).

This is actually what I’m doing. I’m opening a file and writing to it several times over the course of a script. (actually several scripts, but I digress)

Thanks, Doug!

Allen