Create DIR and then make it the default save folder?

Get help with using AutoHotkey and its commands and hotkeys
chef423
Posts: 213
Joined: 28 Aug 2016, 12:10
Google: Christopher Smith
GitHub: chef23

Create DIR and then make it the default save folder?

05 Feb 2018, 23:45

Is this possible?

I have code this code in a program. BUT, if someone uses the Terminal (computer) and saves something to a different folder, my code breaks.

Code: Select all

FileDelete, C:\Reports\*.* ; Will delete all files in C:\Reports\ -Folder 
	FileRemoveDir, C:\Reports ; Will remove the Reports Folder
	FileRecycleEmpty, C:\
	FileCreateDir, C:\Reports
How can AHK (in the code) select the default save folder before my code executes? I have researched and read its not possible? This seems like it would be super simple to implement with 1-2 lines...ala 'FileCreateDir'

Thank you for any comments.
Chris
BoBo
Posts: 2515
Joined: 13 May 2014, 17:15

Re: Create DIR and then make it the default save folder?

06 Feb 2018, 01:31

SetWorkingDir :?: I'd guess you can trigger the default download directory using its registry setting ...
garry
Posts: 1565
Joined: 22 Dec 2013, 12:50

Re: Create DIR and then make it the default save folder?

06 Feb 2018, 04:42

usually use at beginning : setworkingdir,%a_scriptdir%
( when start an ahk-script via link )
and maybe change later ...
chef423
Posts: 213
Joined: 28 Aug 2016, 12:10
Google: Christopher Smith
GitHub: chef23

Re: Create DIR and then make it the default save folder?

07 Feb 2018, 00:14

Thanks. Not sure how I missed that one. I was reading the old forums where many said it was not possible to set a save path...

Chef
chef423
Posts: 213
Joined: 28 Aug 2016, 12:10
Google: Christopher Smith
GitHub: chef23

Re: Create DIR and then make it the default save folder?

23 Feb 2018, 17:01

Can anyone else assist here?

Again, it seems like a no brainier to have a command in AHK that simple lets you set a save path (C:\Reports) as a AHK script is starting.

I have this code the auto runs reports, and it needs to email them. The dir the emailing program looks for these reports is the C:\Reports folder. But, the issue is, when other people use the computer, they may save a file to a different drive / directory / folder and once that is done, Windows automatically tried to use the last save path for any new file being saved so my path of C:\Reports is washed.

Maybe I am looking at this problem incorrectly.

Help :)
Chris
BoBo
Posts: 2515
Joined: 13 May 2014, 17:15

Re: Create DIR and then make it the default save folder?

24 Feb 2018, 08:47

a) What's the name of the email-program?
b) probably that might help: https://www.online-tech-tips.com/window ... indows-10/ (I'd go for the registry-option!)
burque505
Posts: 679
Joined: 22 Jan 2017, 19:37

Re: Create DIR and then make it the default save folder?

24 Feb 2018, 09:20

@chef423, Windows programs drive me nuts in this regard. The script PersistentSaveFolder might be something you can adapt for your own use. I use it dozens of times a day, although I haven't worked out a bug or two, especially with Acrobat. Someday ... :D

My thought (not knowing what email program you use) is that whenever your script calls the email program and requests a save, it sends the directory path to the appropriate control in the save dialog, which very well may be an "ahk_class #32770", probably ClassNN being Edit2 or maybe Edit3 (just use Window Spy).

(If you're creating the directory, I suppose you'd want to check to make sure it doesn't already exist before you use code as above.)

Remember that hitting Alt+d in a save dialog will highlight that folder path. Try having your script first do a 'send, !d', and then send the path (C:\Folders or whatever you use) to that control. I'm probably explaining this poorly - take a look at the code in that script, there's not much to it.

Regards,
burque505
User avatar
jeeswg
Posts: 5419
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Create DIR and then make it the default save folder?

26 Feb 2018, 04:17

Re. dialogs and save folders. You could try NirSoft RegFromApp, which monitors changes to the registry made by a program. Or you could do a before/after registry comparison by using Regshot.
BoBo
Posts: 2515
Joined: 13 May 2014, 17:15

Re: Create DIR and then make it the default save folder?

26 Feb 2018, 11:52

But, the issue is, when other people use the computer, they may save a file to a different drive / directory / folder and once that is done, Windows automatically tried to use the last save path for any new file being saved so my path of C:\Reports is washed.
... but that's only the case if you try to save your report also via the standard 'SaveAs'-dialog box, right??
That gives me the idea that you're triggering any frontend events to save that file? If that's the case, would you mind to provide (that section of) your code?
chef423
Posts: 213
Joined: 28 Aug 2016, 12:10
Google: Christopher Smith
GitHub: chef23

Re: Create DIR and then make it the default save folder?

26 Feb 2018, 12:03

Here is the code, this may help. AHK runs and opens the POS (Point of Sale, not Piece of..) and then saves 3 reports (hence me needed the default save path IF it changes somehow), then invokes the sendEmail program. So the folder save path is rendered in AHK, sendEmail really doesnt do anything but literally send the files, specified by name, to the intended user.

Code: Select all

/*

This script will query and save: Flash, Detailed Daily & General Hourly Reports in CRE and then email them to the Client.

sendEmail http://caspian.dotconf.net/menu/Software/SendEmail/ [Download the sendEMail-v156.zip]

SETUP-----------SETUP------------SETUP
-Install AutoHotKey located in the sendEmail.zip file
-Create 'C:\sendEmail' folder and extract the SendEmail files here, right click on the sendEmail.exe and run as Administrator to install
-Place the EmailReports_MASTERv4.ahk file in the C:\sendEmail

-Date_time code

	CurrentDate := A_Now
	CurrentDate += -1, D
	FormatTime, TimeString, %CurrentDate%, M/d/yyyy
	Send, %TimeString%

	CurrentDate := A_Now
	FormatTime, TimeString, %CurrentDate%, M/d/yyyy
	Send, %TimeString%

-From: 

-To:

*/


	Process, Close, CRE2004.exe ; Close the program
        
	Process, Exist, CRE2004.exe ; check to see if CRE2004.exe is running
	If (ErrorLevel = 0) ; If it is not running


     		{
	DetectHiddenWindows, Off ; don't close the hidden windows, which could be background programs/processes you want running
	SetTitleMatchMode, 2 ; 2: A window's title can contain WinTitle anywhere inside it to be a match. 
	WinGet WindowList , List
	Loop %WindowList%
		{
	WinGetClass, WindowClass, % "ahk_id" WindowList%A_Index%
	If WindowClass not in Progman,Button,Shell_TrayWnd
	WinClose,% "ahk_id " WindowList%A_Index%,,,autohotkey.exe Cash Register Express
		}
	; SetWorkingDir, D:\My Folder\Temp
	FileDelete, C:\Reports\*.* ; Will delete all files in C:\Reports\ -Folder 
	FileRemoveDir, C:\Reports ; Will remove the Reports Folder
	FileRecycleEmpty, C:\
	FileCreateDir, C:\Reports
	SetWorkingDir, C:\Reports
			
	Run, "C:\Program Files (x86)\CRE.NET\CRE2004" ; Executes CRE
       	Sleep, 10000 ; Places code on hold til the program starts

       	 WinActivate, Settlement Pending
	SetKeyDelay, 5000 ; delays keystroks by a 2 seconds
	Send, !o{Backspace}{Backspace}
	WinActivate, Cash Register Express
        SetKeyDelay, 700
	Send, !fvdW1{tab}01{enter}4k ;- this will get to the Friendly Printer Setup
	WinActivate, Printer Setup
	Send, {tab}c!x  ; Saves CutePDF as Default Printer
	Send, 5l ; Moves to Reporting Screen
	WinActivate, Reporting
	Send, ff ; To Flash Report
	Send, {tab}{tab}{tab}{tab} ; Tabs to Date field		
	CurrentDate := A_Now
	CurrentDate += -1, D
	FormatTime, TimeString, %CurrentDate%, M/d/yyyy
	Send, %TimeString%
	Send, {tab}{tab}
	CurrentDate := A_Now
	FormatTime, TimeString, %CurrentDate%, M/d/yyyy
	Send, %TimeString%
	Send, !p
	Sleep, 10000
	WinActivate, Save As
	Sleep, 7000
	Send, !n
	SetKeyDelay, 700
	Send, Flash_Report_TEST1_
	SetKeyDelay, 700
	CurrentDate := A_Now
	CurrentDate += -1, D
	FormatTime, TimeString, %CurrentDate%, M-d-yyyy
	Send, %TimeString%
	Send, !s
	SetKeyDelay, 700
	WinActivate, Reporting
	Send, {tab}{tab}{tab}{tab}
	Send, dd ; Moves to Detailed Daily Report
	Send, !d
	WinActivate, Detailed Daily Report
	Send, !p
	Sleep, 10000
	WinActivate, Save As
	; Send, {tab}{down}L{enter}{tab}{tab}R{enter}
	; FileSelectFolder, Folder, C:\Reports, 0,
	Send, !n
	SetKeyDelay, 700
	Send, Detailed_Daily_Report_TEST1_
	CurrentDate := A_Now
	CurrentDate += -1, D
	FormatTime, TimeString, %CurrentDate%, M-d-yyyy
	Send, %TimeString%
	Send, !s!x
	SetKeyDelay, 700
	WinActivate, Reporting
	Send, {tab}{tab}{tab}{tab}{tab}G
	Send, !p
	Sleep, 10000
	WinActivate, Save As
	Send, !n
	SetKeyDelay, 700
	Send, General_Hourly_Report_TEST1_
	CurrentDate := A_Now
	CurrentDate += -1, D
	FormatTime, TimeString, %CurrentDate%, M-d-yyyy
	Send, %TimeString%
	Send, !s
	WinActivate, Reporting
	Send, !x4k{tab}t!x!x

	; sendEmail code
	FileDelete, C:\sendEmail\EmailReport.bat

	now := A_Now
	FormatTime, today, %now%, M-d-yyyy
	now += -1, D
	FormatTime, yesterday, %now%, M-d-yyyy
	; MsgBox today:`n%today%`n`nyesterday:`n%yesterday%
 
	FileAppend,
	(
	sendEmail -o tls=yes -f (HIDDEN INFO) -u "TEST1 - Flash, Daily & Hourly Reports" -m "Flash Report, Detailed Daily & General Hourly Reports for TEST1 are attached" -a C:\Reports\Flash_Report_TEST1_%yesterday%.pdf -a 			
               C:\Reports\Detailed_Daily_Report_TEST1_%yesterday%.pdf -a C:\Reports\General_Hourly_Report_TEST1_%yesterday%.pdf -vv
	), C:\sendEmail\EmailReport.bat

	Sleep, 2000
	run, %comspec%
	Sleep, 3000
	WinActivate, Administrator: C:\Windows\system32\cmd.exe
	SetKeyDelay, 300
	Send, CD C:\sendEmail{enter}
	Send, EmailReport.bat{enter}
	Sleep, 7000
	WinActivate, Administrator: C:\Windows\system32\cmd.exe
	Send, exit{enter}
				
	}

	Else ; If it is running, ErrorLevel equals the process id for the target program (CRE2004).
       	 {
	WinActivate, Cash Register Express        
        	SetKeyDelay, 500
        	Send, !fx      
	Sleep, 7000
	Process, Exist, CRE2004.exe ; check to see if CRE2004.exe is running
	If (ErrorLevel = 0) ; If it is not running

	DetectHiddenWindows, Off ; don't close the hidden windows, which could be background programs/processes you want running
	SetTitleMatchMode, 2 ; 2: A window's title can contain WinTitle anywhere inside it to be a match. 
	WinGet WindowList , List
	Loop %WindowList%
		{
	WinGetClass, WindowClass, % "ahk_id" WindowList%A_Index%
	If WindowClass not in Progman,Button,Shell_TrayWnd
	WinClose,% "ahk_id " WindowList%A_Index%,,,autohotkey.exe Cash Register Express
		}

	FileDelete, C:\Reports\*.* ; Will delete all files in C:\Reports\ -Folder 
	FileRemoveDir, C:\Reports ; Will remove the Reports Folder
	FileRecycleEmpty, C:\
	FileCreateDir, C:\Reports
	SetWorkingDir, C:\Reports
			
	Run, "C:\Program Files (x86)\CRE.NET\CRE2004" ; Executes CRE
        Sleep, 10000 ; Places code on hold til the program starts

        WinActivate, Settlement Pending
	SetKeyDelay, 5000 ; delays keystroks by a 2 seconds
	Send, !o{Backspace}{Backspace}
	WinActivate, Cash Register Express
        	SetKeyDelay, 700
	Send, !fvdW1{tab}01{enter}4k ;- this will get to the Friendly Printer Setup
	WinActivate, Printer Setup
	Send, {tab}c!x  ; Saves CutePDF as Default Printer
	Send, 5l ; Moves to Reporting Screen
	WinActivate, Reporting
	Send, ff ; To Flash Report
	Send, {tab}{tab}{tab}{tab} ; Tabs to Date field		
	CurrentDate := A_Now
	CurrentDate += -1, D
	FormatTime, TimeString, %CurrentDate%, M/d/yyyy
	Send, %TimeString%
	Send, {tab}{tab}
	CurrentDate := A_Now
	FormatTime, TimeString, %CurrentDate%, M/d/yyyy
	Send, %TimeString%
	Send, !p
	Sleep, 10000
	WinActivate, Save As
	; Send, {tab}{down}L{enter}{tab}{tab}R{enter} 	
	; FileSelectFolder, Folder, C:\Reports, 0,
	Sleep, 4000
	Send, !n
	SetKeyDelay, 700
	Send, Flash_Report_TEST1_
	SetKeyDelay, 700
	CurrentDate := A_Now
	CurrentDate += -1, D
	FormatTime, TimeString, %CurrentDate%, M-d-yyyy
	Send, %TimeString%
	Send, !s
	SetKeyDelay, 700
	WinActivate, Reporting
	Send, {tab}{tab}{tab}{tab}
	Send, dd ; Moves to Detailed Daily Report
	Send, !d
	WinActivate, Detailed Daily Report
	Send, !p
	Sleep, 10000
	WinActivate, Save As
	Sleep, 7000
	Send, !n
	SetKeyDelay, 700
	Send, Detailed_Daily_Report_TEST1_
	CurrentDate := A_Now
	CurrentDate += -1, D
	FormatTime, TimeString, %CurrentDate%, M-d-yyyy
	Send, %TimeString%
	Send, !s!x
	SetKeyDelay, 700
	WinActivate, Reporting
	Send, {tab}{tab}{tab}{tab}{tab}G
	Send, !p
	Sleep, 10000
	WinActivate, Save As
	Sleep, 7000
	Send, !n
	SetKeyDelay, 700
	Send, General_Hourly_Report_TEST1_
	CurrentDate := A_Now
	CurrentDate += -1, D
	FormatTime, TimeString, %CurrentDate%, M-d-yyyy
	Send, %TimeString%
	Send, !s
	WinActivate, Reporting
	Send, !x4k{tab}t!x!x

	; sendEmail code
	FileDelete, C:\sendEmail\EmailReport.bat

	now := A_Now
	FormatTime, today, %now%, M-d-yyyy
	now += -1, D
	FormatTime, yesterday, %now%, M-d-yyyy
	; MsgBox today:`n%today%`n`nyesterday:`n%yesterday%
 
	FileAppend,
	(
	sendEmail -o tls=yes -f n (HIDDEN INFO) -u "TEST1 - Flash, Daily & Hourly Reports" -m "Flash Report, Detailed Daily & General Hourly Reports for TEST1 are attached" -a C:\Reports\Flash_Report_TEST1_%yesterday%.pdf -a 
               C:\Reports\Detailed_Daily_Report_TEST1_%yesterday%.pdf -a C:\Reports\General_Hourly_Report_TEST1_%yesterday%.pdf -vv
	), C:\sendEmail\EmailReport.bat
	
	Sleep, 2000
	run, %comspec%
	Sleep, 3000
	WinActivate, Administrator: C:\Windows\system32\cmd.exe
	SetKeyDelay, 300
	Send, CD C:\sendEmail{enter}
	Send, EmailReport.bat{enter}
	Sleep, 7000
	WinActivate, Administrator: C:\Windows\system32\cmd.exe
	Send, exit{enter}
	
	}
chef423
Posts: 213
Joined: 28 Aug 2016, 12:10
Google: Christopher Smith
GitHub: chef23

Re: Create DIR and then make it the default save folder?

26 Feb 2018, 12:04

BoBo wrote:
But, the issue is, when other people use the computer, they may save a file to a different drive / directory / folder and once that is done, Windows automatically tried to use the last save path for any new file being saved so my path of C:\Reports is washed.
... but that's only the case if you try to save your report also via the standard 'SaveAs'-dialog box, right??
That gives me the idea that you're triggering any frontend events to save that file? If that's the case, would you mind to provide (that section of) your code?
Code posted. :)
BoBo
Posts: 2515
Joined: 13 May 2014, 17:15

Re: Create DIR and then make it the default save folder?

26 Feb 2018, 13:49

OK, AFAI remember there's a quite simple option to ensure that you won't have to care for the given (probably wrong) path after a SaveAs-Dialog has opened ...
<fanfare>Simply drop your files full target path instead of the filename alone</fanfare>. I've tested it here and it worked fine. So let's get fingers crossed ;)

There are several parts of your script (eg. timestamp/embedding that batch file processing/timings/etc.) which can be improved. The main thing you should have a look at, replace all Send-events with ControlSend if possible.

PS. you can send the timestring concatenated to the filename in one go.

Code: Select all

CurrentDate := A_Now
	CurrentDate += -1, D
	FormatTime, TimeString, %CurrentDate%, M-d-yyyy
	Send, %  "Detailed_Daily_Report_TEST1_" . TimeString
chef423
Posts: 213
Joined: 28 Aug 2016, 12:10
Google: Christopher Smith
GitHub: chef23

Re: Create DIR and then make it the default save folder?

26 Feb 2018, 16:05

BoBo wrote:OK, AFAI remember there's a quite simple option to ensure that you won't have to care for the given (probably wrong) path after a SaveAs-Dialog has opened ...
<fanfare>Simply drop your files full target path instead of the filename alone</fanfare>. I've tested it here and it worked fine. So let's get fingers crossed ;)

There are several parts of your script (eg. timestamp/embedding that batch file processing/timings/etc.) which can be improved. The main thing you should have a look at, replace all Send-events with ControlSend if possible.

PS. you can send the timestring concatenated to the filename in one go.

Code: Select all

CurrentDate := A_Now
	CurrentDate += -1, D
	FormatTime, TimeString, %CurrentDate%, M-d-yyyy
	Send, %  "Detailed_Daily_Report_TEST1_" . TimeString
BoBo, thank you so much for taking the time from your busy day to respond. I would love to clean the code up to make it more reliable and robust. It does choke from time to time. I understand the sample code you passed me, but I didnt understand the first part of your response. 'fanfare' ??

Return to “Ask For Help”

Who is online

Users browsing this forum: afe, hanslhansl, Hellbent, makro88, Tigerlily and 85 guests