Jump to content

creating an empty file


Recommended Posts

i'm writing a small script which will create a new file in My Docs. the new file's name is specified in the command line:

createNewFile.exe newFile.doc

i want to be able to specifiy a new folder too:

createNewFile.exe newFolder\newFile.doc

but, this doesn't work with the code i have

$filename = @MyDocumentsDir & "\" & $CmdLine[1]

; 2 = erase the previous contents of the file
$filehandle = FileOpen ($filename, 2)
if $filehandle = -1 then
    msgbox (48, "Error", "Could not open file")
endif
FileClose ($filehandle)

is there an easy way to fix this?

Link to comment
Share on other sites

Might want to improve this but it works

;$CmdLine[1] is for the new folder

;$CmdLine[2] is for the file

$filename = $CmdLine[1] & "\" & $CmdLine[2]

if not FileExists($CmdLine[1]) then DirCreate($CmdLine[1])

; 2 = erase the previous contents of the file

$filehandle = FileOpen ($filename, 2)

if $filehandle = -1 then

msgbox (48, "Error", "Could not open file")

endif

FileClose ($filehandle)

Link to comment
Share on other sites

that's pretty much what i did. i didn't want whoever was using the script to get bogged down with details, so i made the input simple. here is what i did:

; usage: createMyDocsFile <file path>
;
; <file path> = path of file to create
;       e.g. "temp.doc" or "temp/temp/temp.txt"

#include <file.au3>

$temp = StringSplit ($CmdLine[1], "\")

$filename = $temp[$temp[0]]
$dir_path = @MyDocumentsDir 

if $temp[0] > 1 then
    for $j = 1 to $temp[0] - 1
        $dir_path = $dir_path & "\" & $temp[$j]
    next
    DirCreate ($dir_path)
endif

if not _FileCreate ($dir_path & "\" & $filename) then
    if @error = 1 then
        msgbox(0," ","cant open file")
    elseif @error = 2 then
        msgbox(0," ","cant write to file")
    endif
endif
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...