mattslav Posted August 24, 2004 Share Posted August 24, 2004 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 More sharing options...
this-is-me Posted August 24, 2004 Share Posted August 24, 2004 #include <File.au3> _FileCreate($filepath) Who else would I be? Link to comment Share on other sites More sharing options...
mattslav Posted August 24, 2004 Author Share Posted August 24, 2004 it still won't work. do i have to create the folder path first? Link to comment Share on other sites More sharing options...
bshoenhair Posted August 24, 2004 Share Posted August 24, 2004 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 More sharing options...
mattslav Posted August 24, 2004 Author Share Posted August 24, 2004 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 More sharing options...
this-is-me Posted August 24, 2004 Share Posted August 24, 2004 I think you will have to create the target folder first. Who else would I be? Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now