Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

missing 'FileCreate' command


  • Please log in to reply
31 replies to this topic
JSLover
  • Members
  • 920 posts
  • Last active: Nov 02 2012 09:54 PM
  • Joined: 20 Dec 2004

I still want to add better file commands to a future version, such as the ability to 1) overwrite files without having to use FileDelete

...I have to say I wanted this to...because, normally, when you delete a file & re-create it, the creation date is changed, but when you truncate it & then write to it (if AHK had a command to do that) it would leave the date alone. But I've noticed, at least on XP, when you delete & re-create a file soon enough (even outside of AHK), it pulls the old creation date out of it's *ss or something. When I was thinking about this before, I figured I could use...

file=%A_ScriptName%.txt

FileMove, nul, %file%, 1
FileAppend, %A_Now%, %file%
...to truncate the file (copy the nul device over it) & then "append" to the newly truncated file...but I just tested it before posting & it didn't work, I tried FileMove, FileCopy, FileMoveDir & FileCopyDir & none of em would copy the nul device over it (errorlevel was silent during the failure too), it just kept appending the new date to the end. This however did work...but is clunky...

file=%A_ScriptName%.txt

;FileMove, nul, %file%, 1
RunWait, cmd /c copy nul "%file%", , hide
FileAppend, %A_Now%, %file%
...(no, I didn't use %comspec%...lol). Can you make AHK's commands see the nul device (& any other devices it might be supposed to see). This would be good...

FileAppend, Text, Filename [, Trunc]
...then we could add a ", 1" to the end of a FileAppend to say "truncate it 1st"...but then "FileAppend" loses it's meaning & should be named "FileWrite"...because a param determines weather it appends or not.

2) The followed was added per the suggestion above:
To create an empty file, write an empty or non-existent variable to it as in this example:
FileAppend, %EmptyVar%, C:\New File.txt

...I would've never tried an empty or non-existent var, I just now tried what I thought would work (above) & it didn't, then I tried...

FileAppend, , %file%
...& it complained that it wanted param 1 to be non-blank...can you make a blank 1st param be the same as a non-existent var...instead of an error?

Speaking of A_Now, in version 1.0.18 (when there wasn't a FormatTime command, which I still haven't played with yet), I created a subroutine UpdateTime, that would take all the A_?? date vars & convert them to D_?? vars...well, a script is worth a thousand words, so...

UpdateTime:
D_MM=%A_MM%
D_MM-=0
D_DD=%A_DD%
StringTrimLeft, D_YY, A_YYYY, 2
D_H=%A_Hour%
D_H-=0
if D_H>12
	D_H-=12
D_M=%A_Min%
D_S=%A_Sec%
if A_Hour<12
	D_TT=am
else
	D_TT=pm

D_Now=%D_MM%/%D_DD%/%D_YY% %D_H%:%D_M%:%D_S%%D_TT%
return
...now, this worked for me, but I had to Gosub, UpdateTime every time right before I used D_Now, so I wanted to know if you can add "hot vars" that when accessed, will launch a subroutine to determine it's value...like A_Now always gets the current date...I'd like to create vars that would let me know when they're accessed. Say perhaps define one like...

HotVar, Var [, Subroutine]
HotVar, D_Now, UpdateTime
...(default subroutine to var name)...you could either enhance "return" to support a return value or just allow the subroutine to assign the "hot var" a value & that would be used (without making the hot var not hot by overwriting it). Can I suggest...A_Date, A_Time & A_LongDate to use the current users format? I (assume I) could use (but haven't tried yet)...

FormatTime, A_Date, , ShortDate
FormatTime, A_Time, , Time
FormatTime, A_LongDate, , LongDate
...to create those vars, but they aren't "hot" like A_Now, so I'd have to Gosub every time before I use em. The YYYYMMDDHH24MISS format might be programmatically useful, but it ain't pretty for display. Is there any easy way to support a number of milliseconds (since 1970 {unix?} or 1980 {dos/windows?}) in the places the YYY... format is accepted? If that format is always 16 bytes, then anything less (or more, omg!) could be assumed to be milliseconds? If it's possible to support both in the same param, could you add a define so I can choose which I want A_Now to return...like...

#A_Now ms
;-or-
#TimeFormat ms
...coming from JavaScript (& some unix) I expect the "programmatic" time to be milliseconds & not the YYY... thing. Hrm, I just noticed...even with FormatTime, there's no way to get the current ms (of the current second, not since 1970, but you can't get that either)?
Useful forum links: New content since: Last visitPast weekPast 2 weeks (links will show YOUR posts, not mine)

OMFG, the AutoHotkey forum is IP.board now (yuck!)...I may not be able to continue coming here (& I love AutoHotkey)...I liked phpBB, but not this...ugh...

Note...
I may not reply to any topics (specifically ones I was previously involved in), mostly cuz I can't find the ones I replied to, to continue helping, but also just cuz I can't stand the new forum...phpBB was soo perfect. This is 100% the opposite of "perfect".

I also semi-plan to start my own, phpBB-based AutoHotkey forum (or take over the old one, if he'll let me)
PM me if you're interested in a new phpBB-based forum (I need to know if anyone would use it)
How (or why) did they create the Neil Armstrong memorial site (neilarmstronginfo.com) BEFORE he died?

  • Guests
  • Last active:
  • Joined: --
I struggled to start with there not being a fileread command, but I see that there is one now, which seems more intuitive that reading each line. Maybe we could have a similar to create a file in one go

data = lineone`nlinetwo`nline3
filecreate, %data%, filename.txt


Rajat
  • Members
  • 1904 posts
  • Last active: Jul 17 2015 07:45 AM
  • Joined: 28 Mar 2004
data = lineone`nlinetwo`nline3 
fileappend, %data%, filename.txt
Posted Image


i don't know why this is even being discussed!!
a lot more worthwhile would be 'FileDeleteLine' !

MIA

CleanNews.in : Bite sized latest news headlines from India with zero bloat


jonny
  • Members
  • 2951 posts
  • Last active: Feb 24 2008 04:22 AM
  • Joined: 13 Nov 2004
Rajat, I feel for you, but this time I think FileCreate is actually a good idea. Of course, as for some of the other "suggestions" made here, I think some people are better off not talking. :?

I'd like to kindly ask that people read the help file, browse the forum, work through example scripts, write some of their own, and finally, when they have enough experience and know how AHK works under the hood, then make sensible and detailed suggestions as to how AHK could be improved.

In other, less eloquent words: RTFM

ranomore
  • Members
  • 171 posts
  • Last active: Mar 01 2013 01:41 PM
  • Joined: 06 Nov 2004
amen.

EDIT:

a lot more worthwhile would be 'FileDeleteLine'

I was asking about something similar to this in the support forum a while ago (and I can't find it now). But what I understood from Chris was that AHK would basically have to re-write the entire file through the use of a temporary copy. He said performance-wise it probably wouldn't be much better than reading the file into a variable, operating on it, and then re-writing the file with your own AHK script. You would have more control over it the second way. (I think I heard Jonny say that once about another one of these deals :?: )

I'm starting to see this kind of thing with the script I wrote for work. What really is the performance advantage to having a hotkey that opens/activates a commonly used app if you can just Alt+Tab or Win+R it? 6 of one, half dozen of the other, I think.

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004

Can you make AHK's commands see the nul device (& any other devices it might be supposed to see).

I think the code would have to be changed to recognize "nul" as a special file name, which seems little too obscure to be worth it. If anyone can think of more uses for such a thing, please post them.

"truncate it 1st"...but then "FileAppend" loses it's meaning & should be named "FileWrite"...because a param determines weather it appends or not.

You're right, and in any case new parameters can't be easily added to commands whose last parameter can contain non-escaped commas.

FileAppend, %EmptyVar%, C:\New File.txt
I just now tried what I thought would work (above) & it didn't

Strange, it works on XP here.

can you make a blank 1st param be the same as a non-existent var...instead of an error?

Seems like a good idea. Thanks.

add "hot vars" that when accessed, will launch a subroutine to determine it's value

The ability to define and call functions is planned, which seems basically the same thing. Example:
Timestamp := GetFormattedTime()

Can I suggest...A_Date, A_Time & A_LongDate to use the current users format?

That will probably happen someday. In the meantime, at least there's a way to do it with FormatTime.

Is there any easy way to support a number of milliseconds (since 1970 {unix?} or 1980 {dos/windows?}) in the places the YYY... format is accepted? If that format is always 16 bytes, then anything less (or more, omg!) could be assumed to be milliseconds? If it's possible to support both in the same param, could you add a define so I can choose which I want A_Now to return...like...

Can you explain what you might use this for (see next item)? That helps determine whether and at what priority this task should be done.

there's no way to get the current ms (of the current second, not since 1970, but you can't get that either)?

That's right, resolution only goes down to seconds for the "current time" variables. However, you can use A_TickCount to measure the time between two events down to the millisecond (though it's typically in multiples of 10 milliseconds).

JSLover
  • Members
  • 920 posts
  • Last active: Nov 02 2012 09:54 PM
  • Joined: 20 Dec 2004
_________________
Useful forum links: New content since: Last visitPast weekPast 2 weeks (links will show YOUR posts, not mine)

OMFG, the AutoHotkey forum is IP.board now (yuck!)...I may not be able to continue coming here (& I love AutoHotkey)...I liked phpBB, but not this...ugh...

Note...
I may not reply to any topics (specifically ones I was previously involved in), mostly cuz I can't find the ones I replied to, to continue helping, but also just cuz I can't stand the new forum...phpBB was soo perfect. This is 100% the opposite of "perfect".

I also semi-plan to start my own, phpBB-based AutoHotkey forum (or take over the old one, if he'll let me)
PM me if you're interested in a new phpBB-based forum (I need to know if anyone would use it)
How (or why) did they create the Neil Armstrong memorial site (neilarmstronginfo.com) BEFORE he died?

jonny
  • Members
  • 2951 posts
  • Last active: Feb 24 2008 04:22 AM
  • Joined: 13 Nov 2004
I'll take on the position of an art critic: If I stare at it long enough, the point is bound to occur to me! :lol:

JSLover
  • Members
  • 920 posts
  • Last active: Nov 02 2012 09:54 PM
  • Joined: 20 Dec 2004

I think the code would have to be changed to recognize "nul" as a special file name, which seems little too obscure to be worth it. If anyone can think of more uses for such a thing, please post them.

...the nul device is a system device, it seems like just passing the string "nul" to the API should work, without you needing to detect "nul" & take special action. The use for it is to be able to truncate files...of course for the long run, FileTrunc or FileAppend,,, 1 would be better. The real use for "nul" tho is like in batch files...

Echo Wow
Pause>Nul
...to get rid of unwanted text...like unix's /dev/null. But it also (normally) works as a file to read from to get a 0 byte file. Wouldn't "FileAppend, Wow, nul" write to the nul device (it can't create a file with the name "nul")? Can't you make FileMove (or Copy) see it the same way?...pass it to the API, if that don't work, fine, oh well.

You're right, and in any case new parameters can't be easily added to commands whose last parameter can contain non-escaped commas.

...how many people really have comma's in their filenames? If you're adding one param, a number param, can't you only detect the stuff after the last comma & see if it's a number, if not, take it as part of the comma'd filename?

FileAppend, Text, My, File, Name, Has, Commas <--- don't trunc, last thing is word the "commas", not a number
FileAppend, Text, My, File, Name, Has, Commas, 1 <--- trunc, last thing is number
...I'm almost content with a FileTrunc command, cept it would take 2 lines instead of 1, to always trunc a file before appending to it. What about weening people off FileAppend to a new FileWrite command, that could include enough params for future enhancement?

Strange, it works on XP here.

...I said "what I thought would work (above)"...referring to the "FileMove, nul, %file%, 1"...after I saw that you said to use an empty or non-existent var, I tried that too...it worked.

Seems like a good idea. Thanks.

FileAppend's text parameter may be omitted to create an empty file or update the timestamp of an existing file. [thanks JSLover]

...yay! My 1st accepted idea! With a credit! Cool. :cool:

The ability to define and call functions is planned, which seems basically the same thing. Example:
Timestamp := GetFormattedTime()

...quasi-same, but then I'd have to use...something like...

msgbox, D_Now() <--- having to use () instead of it being like A_Now..."hot" but no ()'s
msgbox, %D_Now()% <--- or would I need %'s?
FileAppend, D_Now(), %file%
FileAppend, %D_Now()%, %file%
...with the ()'s on the var name...D_Now() vs just D_Now...perhaps you could do functions with () & also support "hot vars" without the ()?...some extra flag when defining the "function" whether or not its use should require the ()'s. For a function, I do want the () but since I created my own pre-formatted A_Now (& to create, period, A_Time, A_Date & A_LongDate & not have to call them A_Time()) I'd like to use it like I'd use the built-in AHK hot vars, with no ().

That will probably happen someday. In the meantime, at least there's a way to do it with FormatTime.

...but without "hot vars" I have to Gosub (to set them to the current time again) before each use, which makes a single-line command 2 lines.

Can you explain what you might use this for (see next item)? That helps determine whether and at what priority this task should be done.

...well, I don't know, cuz it looks better than the YYY... format, milliseconds can be operated on with math to get the rest of the parts of the date figured out. & it's just a generally good default. I guess it don't need high priority, but if it's easy to do, then good. But it is the default in JavaScript & unix & stuff.

That's right, resolution only goes down to seconds for the "current time" variables. However, you can use A_TickCount to measure the time between two events down to the millisecond (though it's typically in multiples of 10 milliseconds).

...not possible to get the current milliseconds in the WinAPI?
Useful forum links: New content since: Last visitPast weekPast 2 weeks (links will show YOUR posts, not mine)

OMFG, the AutoHotkey forum is IP.board now (yuck!)...I may not be able to continue coming here (& I love AutoHotkey)...I liked phpBB, but not this...ugh...

Note...
I may not reply to any topics (specifically ones I was previously involved in), mostly cuz I can't find the ones I replied to, to continue helping, but also just cuz I can't stand the new forum...phpBB was soo perfect. This is 100% the opposite of "perfect".

I also semi-plan to start my own, phpBB-based AutoHotkey forum (or take over the old one, if he'll let me)
PM me if you're interested in a new phpBB-based forum (I need to know if anyone would use it)
How (or why) did they create the Neil Armstrong memorial site (neilarmstronginfo.com) BEFORE he died?

ranomore
  • Members
  • 171 posts
  • Last active: Mar 01 2013 01:41 PM
  • Joined: 06 Nov 2004
@JS Lover -why don't you just learn C++ and make your own changes? You seem pretty smart (picky?) already! :!:

Most Europeans speak at least three different languages, what's one more for you? :wink:

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004

...the nul device is a system device, it seems like just passing the string "nul" to the API should work

That's exactly what it does, but apparently it's not universally supported throughout the API, at least not in the way you'd want it to be.

...how many people really have comma's in their filenames? If you're adding one param, a number param, can't you only detect the stuff after the last comma & see if it's a number, if not, take it as part of the comma'd filename?

Good idea and it does eliminate nearly all of the ambiguity. Even so, I'd probably prefer something like a question mark prefix in front of the file name to indicate "overwrite".

However, it might be best not to "overload" FileAppend in this manner, for the reasons you described (clarity). So your FileWrite idea below is probably better.

What about weening people off FileAppend to a new FileWrite command, that could include enough params for future enhancement?

Thanks for the idea. Does anyone else have any thoughts about this?

msgbox, %D_Now()% <--- or would I need %'s?
FileAppend, %D_Now()%, %file%

Great idea. In fact, this same method might allow expressions to be used everywhere in all commands (e.g. % 5 + MyVar %), but I'll have to think about it more to be sure there would be no major side-effects or breaking of existing scripts.

perhaps you could do functions with () & also support "hot vars" without the ()?...some extra flag when defining the "function" whether or not its use should require the ()'s.
... I'd like to use it like I'd use the built-in AHK hot vars, with no ().

Another good idea. But it might be somewhat hard to implement since something that looks like a variable by the pre-parser would have to be looked up in the function list if it's not found in the variable list. The problem is that at that stage, the function/subroutine list is not yet available (since the script isn't done loading), so it can't be looked up. As I continue to work on built-in functions, I'll see if there's some way to do it.

...not possible to get the current milliseconds in the WinAPI?

timeGetTime() gets this, but I get the impression from MSDN docs that it's actually 5 or 10 ms resolution (like A_TickCount). It is possible to get extremely high resolution times from QueryPerformanceCounter() if the hardware supports it (99.9% of machines in the last 10 years probably do, but I'm guessing). Do you (or anyone else reading this) have a need for times more precise than 10ms?

Thanks for the great ideas.

jonny
  • Members
  • 2951 posts
  • Last active: Feb 24 2008 04:22 AM
  • Joined: 13 Nov 2004

Does anyone else have any thoughts about this?


I think it's a good way to make AHK even more versatile. At times I have been somewhat put off by the limited nature of AHK's File commands.

JSLover
  • Members
  • 920 posts
  • Last active: Nov 02 2012 09:54 PM
  • Joined: 20 Dec 2004

@JS Lover -why don't you just learn C++ and make your own changes? You seem pretty smart (picky?) already! :!:

...(BTW, it's "JSLover", no space)...I have coded in C & would/could learn C++, but I haven't found a Free compiler for it...& no I don't wanna buy MS Visual C++ (I want it, I don't wanna buy it). Then, if I did find a Free (C++) compiler, I'd have to find out how to reformat all of the AHK code to 1st compile under the new compiler, then make changes. Chris, there was a topic somewhere else about compilers, any chance you'd wanna translate AHK to C (not ++) & use the lcc-win32 compiler system? hint hint

Most Europeans speak at least three different languages, what's one more for you? :wink:

...who said anything about Europeans?

Good idea and it does eliminate nearly all of the ambiguity. Even so, I'd probably prefer something like a question mark prefix in front of the file name to indicate "overwrite".

...a question mark has the wrong feel, more like "Overwrite?" than "Overwrite!". Would this be better?...

FileAppend, 1, Wow, File, Still, Has, Commas <--- if 1st param is a number (& is 1), overwrite
FileAppend, Wow, >File, Still, Has, Commas <--- or more like your '?' prefix, a > prefix would be more like "Overwrite!" (as in batch files...'>' is overwrite, '>>' is append).

Great idea. In fact, this same method might allow expressions to be used everywhere in all commands (e.g. % 5 + MyVar %), but I'll have to think about it more to be sure there would be no major side-effects or breaking of existing scripts.

...technically, I was asking if your current idea for function calling was to use %'s or not, my 1st example was without, but then I thought "wait, it might require %'s", so then I put it that way asking "or would I need %'s?". But if this led to function calling in more places than originally planned...great! Either with or without %'s, but they should be able to be called in-line..."MsgBox, msg()".

Do you (or anyone else reading this) have a need for times more precise than 10ms?

...I don't need "times more precise than 10ms" my only use would be for displaying a clock with the ms, some really quickly changing number, don't need to be precise, could probably fake it with a loop, but the real ms of the current second would be better for display. For timing anything, A_TickCount is fine, precise enough for me, for that, but A_TickCount don't tell me how many ms happened since THIS second, just since windows boot.
Useful forum links: New content since: Last visitPast weekPast 2 weeks (links will show YOUR posts, not mine)

OMFG, the AutoHotkey forum is IP.board now (yuck!)...I may not be able to continue coming here (& I love AutoHotkey)...I liked phpBB, but not this...ugh...

Note...
I may not reply to any topics (specifically ones I was previously involved in), mostly cuz I can't find the ones I replied to, to continue helping, but also just cuz I can't stand the new forum...phpBB was soo perfect. This is 100% the opposite of "perfect".

I also semi-plan to start my own, phpBB-based AutoHotkey forum (or take over the old one, if he'll let me)
PM me if you're interested in a new phpBB-based forum (I need to know if anyone would use it)
How (or why) did they create the Neil Armstrong memorial site (neilarmstronginfo.com) BEFORE he died?

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004

any chance you'd wanna translate AHK to C (not ++) & use the lcc-win32 compiler system

That would be about as much work as porting it to Unicode, and I suspect Unicode would be much more worth the effort. In that topic you linked to above, that free beta MS C++ compiler seem very close to being able to compiled AutoHotkey. If someone doesn't get to it first, someday I'll download it and try to adjust the source code.

...a question mark has the wrong feel, more like "Overwrite?" than "Overwrite!". Would this be better?...
FileAppend, Wow, >File, Still, Has, Commas <--- or more like your '?' prefix

I think it's best, as you originally said, not to overload FileAppend. It's much more correct and readable to have a new FileWrite command or something similar.

but they should be able to be called in-line..."MsgBox, msg()".

Something like MsgBox, which accepts string parameters (not numbers), would require precent signs to avoid breaking existing scripts: MsgBox %msg()%

my only use would be for displaying a clock with the ms, some really quickly changing number, don't need to be precise, could probably fake it with a loop

Rajat knows that sometimes I'm stubborn, so thanks for being persistent. I can see now that this definitely would have uses (stopwatches, etc.). It will be in the next update.

Rajat
  • Members
  • 1904 posts
  • Last active: Jul 17 2015 07:45 AM
  • Joined: 28 Mar 2004

Rajat knows that sometimes I'm stubborn

Posted Image

MIA

CleanNews.in : Bite sized latest news headlines from India with zero bloat