Page 1 of 1

Adding a timestamp to a file's name

Posted: 09 Jan 2021, 20:18
by Fleco
Hi

I am trying to use the following code to save files with a timestamp in its name, but I get a "This variable has not been assigned a value" error due to the line: file :="D:\RUN_"+%A_Index%+"_"+showTime(A_now)

Thanks in advance!

Cheers

Code: Select all


proc := "C:\InterWinner\BIN\InterWinner.exe", wTitle := "InterWinner"
time=10000; Time of measurement

#v::
If WinExist(wTitle)
 WinActivate
Else Run, %proc%
WinWaitActive, %wTitle%,, 20

Loop
{
    if (A_Index < 4)
	Send !fa
	WinWaitActive, %wTitle%,, 5
	file :="D:\RUN_"+%A_Index%+"_"+showTime(A_now)
	Send %file%
	Send {Enter}
	WinWaitActive, %wTitle%,, 5
	Send {F4} ; Erase spectrum
	WinWaitActive, %wTitle%,, 5
	Send {F2} ; Start measurement
	Sleep, %time%  ; Exposure for %time% miliseconds.
	Send {F3} ; Stop measurement
	WinWaitActive, %wTitle%,, 5
    if (A_Index > 3)
    	break  ; Terminate the loop
}

showTime(ts) {
 FormatTime, formatted, %ts%, dddd MMMM d, yyyy hh:mm:ss tt
 Return formatted
}

Return


Re: Adding a timestamp to a file's name

Posted: 09 Jan 2021, 20:26
by gregster
Fleco wrote:
09 Jan 2021, 20:18
I am trying to use the following code to save files with a timestamp in its name, but I get a "This variable has not been assigned a value" error due to the line: file :="D:\RUN_"+%A_Index%+"_"+showTime(A_now)
Looks like a warning, not an error, although I don't see a #warn directive in your code. Generally, if it's a warning like this one (which is just a hint about a potential problem), you can either remove #Warn, modify the #Warn options or initialize the variables before using them.

Re: Adding a timestamp to a file's name

Posted: 09 Jan 2021, 20:28
by gregster
Well, the syntax of the line is actually wrong - you'd concatenate like this in AHK expressions (and usually you wouldn't want to double-deref the variable A_index):

Code: Select all

file := "D:\RUN_" A_Index "_" showTime(A_now)
Of course, you could also use . as a concat operator: https://www.autohotkey.com/docs/Variables.htm#concat
But not +.

Edit: Also, there should be a space in front of the ; of an inline comment:

Code: Select all

time := 10000 ;Time of measurement
Perhaps there are more problems... I only had a quick look, and didn't run it.

Re: Adding a timestamp to a file's name

Posted: 09 Jan 2021, 20:42
by JoeWinograd
In addition to the syntax problems that gregster mentioned, the value returned by the showTime function is not valid in a file name because of the colons. The following characters are not allowed in folder/file names (other than in the drive/path and as wildcards for a few of them):

< (less than)
> (greater than)
: (colon)
" (double quote)
/ (forward slash)
\ (backslash)
| (vertical bar or pipe)
? (question mark)
* (asterisk)

Thus, to use the value in a file name, change the colons to some other character that is valid in a file name, such as a period, hyphen, underscore, etc. For example:

Code: Select all

FormatTime, formatted, %ts%, dddd MMMM d, yyyy hh.mm.ss tt
Regards, Joe

Re: Adding a timestamp to a file's name  Topic is solved

Posted: 09 Jan 2021, 22:49
by Fleco
I modified my code taking into account your comments and, although it is still not very well written, it makes what I need.
Thanks a lot!

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

proc := "C:\InterWinner\BIN\InterWinner.exe", wTitle := "InterWinner"
time := 10000 ; exposure time in milisegundos
iterations := 3

#v::
If WinExist(wTitle)
 WinActivate
Else Run, %proc%
WinWaitActive, %wTitle%,, 20

Loop 
{
	if (A_Index < iterations+1)	
	Send {F4} ; Erase
	WinWaitActive, %wTitle%,, 5
	Send !fa
	WinWaitActive, %wTitle%,, 5
	
	file := "D:\M" showTime(A_now) "_RUN_" A_Index ".SPE"
	Send %file%
	WinWaitActive, %wTitle%,, 5
	Send {Enter}
	
	WinWaitActive, %wTitle%,, 5
	Send {F2} ; Start
	Sleep, %time%  ; Exposes for %time% miliseconds.
	Send {F3} ; Stop
	WinWaitActive, %wTitle%,, 5
    
    if (A_Index = iterations)
    	Send !fa
    	WinWaitActive, %wTitle%,, 5
  	
    	file2 := "D:\M" showTime(A_now) "_RUN_" A_Index ".SPE"
    	Send %file2%
    	Send {Enter}
	
    if (A_Index > iterations)
    	break ; Terminate the loop
}

showTime(ts) {
 FormatTime, formatted, %ts%, yyMMddhhmmss
 Return formatted
}

Return

Re: Adding a timestamp to a file's name

Posted: 10 Jan 2021, 10:12
by Fleco
Hi, one more question regarding this script

How can avoid losing focus on the windows I am interested in?

If during this sleep time

Code: Select all

Sleep, %time%  ; Exposes for %time% miliseconds.


someone open another app, let say notepad.exe, the next line

Code: Select all

Send {F3} ; Stop


will not work on the windows I want to.
I already have tried writing

Code: Select all

WinWaitActive, %wTitle%,, 5
Send {F3} ; Stop

but it doesn't get focus on my back windows.

Thanks in advance.

Cheers

Re: Adding a timestamp to a file's name

Posted: 10 Jan 2021, 12:53
by Fleco
Hi, I found the solution in this forum, the next line recovers the focus on my app's windows.

Code: Select all

WinActivate, ahk_exe iwmain.exe
Only one comment, iwmain.exe is the name of my app in the Windows Task Manager processes list.