Jump to content

FileOpen with full path


leuce
 Share

Recommended Posts

G'day everyone

I have a bunch of arrays like this:

$one[1] = C:\Documents and Settings\UserX\Desktop\Folder1\Folder2\file.txt

$one[2] = Some text here...

and I'd like to have the script create the file $one[1] and write $one[2] to it as its content. However, FileOpen doesn't work on full paths. Do you know of a method to create files in various paths and open them so that one can write content to them? Or alternatively is there a way to create files (in various paths) with specific content, without having to use FileOpen or a similar function before writing the content to the file?

Thanks

Samuel

Link to comment
Share on other sites

G'day everyone

I have a bunch of arrays like this:

$one[1] = C:\Documents and Settings\UserX\Desktop\Folder1\Folder2\file.txt

$one[2] = Some text here...

and I'd like to have the script create the file $one[1] and write $one[2] to it as its content. However, FileOpen doesn't work on full paths. Do you know of a method to create files in various paths and open them so that one can write content to them? Or alternatively is there a way to create files (in various paths) with specific content, without having to use FileOpen or a similar function before writing the content to the file?

Thanks

Samuel

Just start writing the file using filewrite()

FileWrite($one[1],$one[2])

[u]You can download my projects at:[/u] Pulsar Software
Link to comment
Share on other sites

Just start writing the file using filewrite()

FileWrite($one[1],$one[2])

And... amazingly, it works. Thanks. The Autoit helpfile says that FileWrite should be used on a previously opened file, so that's why I didn't try it.

I did find that using FileWrite like this only works if the directory already exists, so it looks like I would have to combine it with DirCreate.

Thanks

Samuel

==

Edited: Oh, no, it was too good to be true -- if the file isn't opened using FileOpen, then using FileWrite directly writes the file as ANSI. I need to the files to be written as UTF-8. Any other ideas, anyone?

Edited by leuce
Link to comment
Share on other sites

I have a bunch of arrays like this:

$one[1] = C:\Documents and Settings\UserX\Desktop\Folder1\Folder2\file.txt

$one[2] = Some text here...

and I'd like to have the script create the file $one[1] and write $one[2] to it as its content.

Okay, this is how I did it:

For $i = 1 to number_of_files

$one[1] = full_path
$one[2] = content of file

; If FileExists ($one[1]) Then
; MsgBox (0, "Whoops", "File or files already exist...", 0)
; Exit
; Else

; Get the path without the file name, and get the file name

$dirsplit = StringSplit ($one[1], "\", 1)
$filename = $dirsplit[$dirsplit[0]]
$getdir = StringSplit ($one[1], $filename, 1)
$diris = $getdir[1]

; Create the path without the file name, change working directory to it, and write the file

DirCreate($diris)
FileChangeDir ($diris)

$thisfile = FileOpen ($filename, 129)
FileWrite ($thisfile, $one[2])
FileClose ($thisfile)
; EndIf

Next

It's not simple, and if anyone can simplify it, I'd be happy to see how it can be done. I tend to come up with complex solutions instead of simple ones.

Link to comment
Share on other sites

Okay, this is how I did it:

It's not simple, and if anyone can simplify it, I'd be happy to see how it can be done. I tend to come up with complex solutions instead of simple ones.

There's not a thing wrong with that code.

This is one other way to go about it that is only one less line of source code, but, with no array creation, it likely is more efficient behind the scenes:

$one = @DesktopDir & "\test.txt"

$split = StringInStr($one, "\", 0, -1); find last "\"
$diris = StringLeft($one, $split - 1)
$filename = StringTrimLeft($one, $split)

MsgBox(0,"",$one & @CRLF & $filename & @CRLF & $diris)

Edit: PS - Considering the structure of your input array, you might find a "Step 2" clause useful in your for/next loop.

Edited by Spiff59
Link to comment
Share on other sites

FileOpen works will full paths just fine. Where in the world did you get the idea it didn't?

I tested it. But... now that I know that FileWrite doesn't work when the directory doesn't exist, I think that perhaps the reason my testing of FileOpen didn't work was because the directory that I tested it with didn't exist either. One can create a file by using FileOpen, so I assumed that one can create a file in a non-existing directory with FileOpen too, i.e. that AutoIt would simply create all the missing subdirectories automatically.

Also the help file creates the impression that one can't use a path in FileOpen.

Link to comment
Share on other sites

From the FileOpen Helpfile

8 = Create directory structure if it doesn't exist (See Remarks).

so if you use 137 you should also create the structure

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

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...