Jump to content

Create a folder and file


Recommended Posts

I am trying to create a folder that contains a blank text file. I have a script that I want to run one time on a workstation and not run again when someone else logs on. I am doing this by checking for the file and folder. I wrote the following short script to create the folder and put the file in it. My problem is that the folder gets created, but not the file. What am I missing. Thanks.

#include <file.au3>

$dir = DirCreate("C:\change")

$flag=FileOpen ($dir & "\flag.txt", 2)

FileClose($flag)

Link to comment
Share on other sites

  • Moderators

I am trying to create a folder that contains a blank text file. I have a script that I want to run one time on a workstation and not run again when someone else logs on. I am doing this by checking for the file and folder. I wrote the following short script to create the folder and put the file in it. My problem is that the folder gets created, but not the file. What am I missing. Thanks.

#include <file.au3>

$dir = DirCreate("C:\change")

$flag=FileOpen ($dir & "\flag.txt", 2)

FileClose($flag)

Look at the "Return" value for DirCreate(), it's not the Directory you created or a Handle... it's a 1 or a 0 boolean style.

Something like:

$sFile = @HomeDrive & "\change"
If DirCreate($sFile) Then
    FileClose(FileOpen($sFile & "\flag.txt", 2))
Else
    MsgBox(16, "Error", "Failed to create the directory")
EndIf

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

#include <file.au3>
$folder = @HomeDrive & "\change"
    If FileExists ($folder & "\flag.txt") = 1 Then
        MsgBox (-1 , "info" , "file already exists")
    EndIf
    If DirGetSize ($folder) = -1 Then ; this way u know that the folder does not exists
        DirCreate ($folder)
        FileWrite ($folder & "\flag.txt","")
    EndIf
    If DirGetSize ($folder) >= 0 And FileExists ($folder & "\flag.txt") = 0 Then
        FileWrite ($folder & "\flag.txt","")
    EndIf

I'm not sure of what u want

Edited by star2

[quote]Baby you're all that I want, When you're lyin' here in my armsI'm findin' it hard to believe, We're in heavenAnd love is all that I need , And I found it there in your heartIt isn't too hard to see, We're in heaven .Bryan Adams[/quote].............................................................................[u]AUTOIT[/u]

Link to comment
Share on other sites

  • Moderators

#include <file.au3>
$folder = @HomeDrive & "\change"
    If FileExists ($folder & "\flag.txt") = 1 Then
        MsgBox (-1 , "info" , "file already exists")
    EndIf
    If DirGetSize ($folder) = -1 Then ; this way u know that the folder does not exists
        DirCreate ($folder)
        FileWrite ($folder & "\flag.txt","")
    EndIf
    If DirGetSize ($folder) >= 0 And FileExists ($folder & "\flag.txt") = 0 Then
        FileWrite ($folder & "\flag.txt","")
    EndIf

I'm not sure of what u want

Just FYI...

You don't need to get the Dir size to know if the directory exists or not... You can use If FileExists($folder) as well.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Also, the modes for "fileopen" allow for you to ADD the mode numbers together to get a combined mode, thus:

1 = Write mode (append to end of file)

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

then 9 = Create directory structure AND file if it doesn't exist, then open the file for editing in append mode.

So:

$File = FileOpen("C:\change\flag.txt", 9)

Alternatively if all you want do do is create the file, have a look at _FileCreate() in the helpfile, here is its example:

#include <File.au3>

If Not _FileCreate("error.log") Then

MsgBox(4096,"Error", " Error Creating/Resetting log. error:" & @error)

EndIf

Edited by tAKTelapis
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...