File Path Help Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
WarlordAkamu67
Posts: 219
Joined: 21 Mar 2023, 06:52

File Path Help

Post by WarlordAkamu67 » 05 Jun 2023, 15:21

I am trying to create a script that has the capability to update itself. There are two (2) separate AHK scripts that work okay. I am trying to have more dynamic file paths. Additionally, I would like to clean it up a bit if you've suggestions!

The issues with file path: I want to check for a file path, but may be unsure of what drive it is under. My computer has it located as "Z:/Employee Files" ... The computer I would be running the script on is "F:\Company Files\Employee Files
Getting the file path to be more dynamic if possible would be lovely. I have attempted using just the known string and wildcards "*\01 EMPLOYEE FILES\00 Me\(ect.)".

This code works as far as I have been using it:
The Code for the 2nd "updater" script.

Code: Select all

; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ;

remove_Updater(*) {
  my_File_Delete(A_Temp "\warlords_Script_Updater.ini")
  my_File_Delete(A_Temp "\warlords_Script_Updater.exe")
  return
}

; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ;

my_File_Delete(path_to_Delete) {
  try {
    FileDelete(path_to_Delete)
}
}

; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ;
; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ;
; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ;

OnExit(remove_Updater)

if (FileExist(A_Temp "\warlords_Script_Updater.ini")) {
  script_Name := IniRead(A_Temp "\warlords_Script_Updater.ini", "script_to_Update", "the_Name")
  script_Location := IniRead(A_Temp "\warlords_Script_Updater.ini", "script_to_Update", "working_Space")

  if (FileExist("Z:\01 EMPLOYEE FILES\00 Me")) {
    found_Location := "Z:\01 EMPLOYEE FILES\00 Me\Executables\" script_Name
} else if (FileExist("F:\Company Files\01 EMPLOYEE FILES\00 Me")) {
    found_Location := "F:\Company Files\01 EMPLOYEE FILES\00 Me\Executables\" script_Name
} else {
    found_Location := 0
}

  if (FileExist(found_Location)) {
    FileCopy(found_Location, script_Location, 1)
    while (!FileExist(script_Location)) {
      Sleep(50)
}
    MsgBox(script_Name " has been updated.")
    Run(script_Location)
} else {
    MsgBox("No Updated File Available.")
    Run(script_Location)
}
} else {
  MsgBox("Update Failed.")
}

ExitApp()
The Code for the script running and to be updated.

Code: Select all

#SingleInstance Force

; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ;

if (A_IsCompiled && !IsSet(ver_Now)) {
	ver_Now := "1.0.0"
	the_Name := "AIO_Suite"
	check_for_update()
}

; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ;

check_for_update() {
	global
	if (FileExist("Z:\01 EMPLOYEE FILES\00 Me\Code\The Collector\CollectToggle\" the_Name "_version_Number.ini")) {
		found_Location := "Z:\01 EMPLOYEE FILES\00 Me\Code\The Collector\CollectToggle\" the_Name "_version_Number.ini"
} else if (FileExist("F:\Company Files\01 EMPLOYEE FILES\00 Me\Code\The Collector\CollectToggle\" the_Name "_version_Number.ini")) {
		found_Location := "F:\Company Files\01 EMPLOYEE FILES\00 Me\Code\The Collector\CollectToggle\" the_Name "_version_Number.ini"
} else {
		found_Location := 0
}
	if (FileExist(found_Location)) {
		current := IniRead(found_Location, "version", "current")
		if (ver_Now !== current) {
			while (A_TimeIdle < 500) {
			Sleep(1000)
}
		msgResult := MsgBox("Update available for " the_Name ", continue update?", "", 4)
		if (msgResult = "Yes") {
			try {
				FileDelete(A_Temp "\warlords_Script_Updater.ini")
}

		FileAppend
(
"[script_to_Update]
the_Name=" the_Name ".exe
working_Space=" A_ScriptFullPath
), A_Temp "\warlords_Script_Updater.ini"

		FileInstall("Z:\01 EMPLOYEE FILES\00 Me\Executables\warlords_Script_Updater.exe", A_Temp "\warlords_Script_Updater.exe", 1)
		Run(A_Temp "\warlords_Script_Updater.exe")
		ExitApp()
}
}
} else {

	FileAppend
(
"[version]
current=" ver_Now "
)", found_Location

}
	SetTimer(check_for_update,360000)
	return
}

; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ;

User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: File Path Help  Topic is solved

Post by mikeyww » 05 Jun 2023, 16:57

Code: Select all

#Requires AutoHotkey v2.0
basePath := '\Windows\System32\notepad.exe'
If f := filePath(basePath)
     MsgBox f, 'Success', 64
Else MsgBox 'Not found.`n' basePath, 'Failure', 48

filePath(basePath) {
 Loop Parse DriveGetList()
  If FileExist(f := A_LoopField ':' basePath)
   Return f
}

User avatar
WarlordAkamu67
Posts: 219
Joined: 21 Mar 2023, 06:52

Re: File Path Help

Post by WarlordAkamu67 » 06 Jun 2023, 06:50

Thank you very much @mikeyww. However, I am still stuck checking each one individually. I would need to add a check for "COMPANY FILES" as a possibility past the drive check.

After compiling the script into an EXE (after each change of course), I ran it on both computers.
The following code reports:

Computer 1:
Success with (base_Path := "\01 EMPLOYEE FILES\")
Failed with (base_Path := "COMPANY FILES\01 EMPLOYEE FILES\")
Failed with (base_Path := "*\01 EMPLOYEE FILES\")

Computer 2:
Failed with (base_Path := "\01 EMPLOYEE FILES\")
Success with (base_Path := "COMPANY FILES\01 EMPLOYEE FILES\")
Failed with (base_Path := "*\01 EMPLOYEE FILES\")

Code: Select all

#Requires AutoHotkey v2.0
base_Path := "*\01 EMPLOYEE FILES\"
if (path_to_File := filePath(base_Path)) {
  MsgBox path_to_File , "Success", 64
} else {
  MsgBox "Not found.`n" base_Path, "Failure", 48
}

filePath(base_Path) {
  Loop Parse DriveGetList()
{
    if FileExist(path_to_File := A_LoopField ":" base_Path) {
      Return path_to_File
}
}
}
Sorry, I changed it up to fit my current style.

Code: Select all

	if (FileExist("Z:\01 EMPLOYEE FILES\00 Me\Code\The Collector\CollectToggle\" the_Name "_version_Number.ini")) {
		found_Location := "Z:\01 EMPLOYEE FILES\00 Me\Code\The Collector\CollectToggle\" the_Name "_version_Number.ini"
} else if (FileExist("F:\COMPANY FILES\01 EMPLOYEE FILES\00 Me\Code\The Collector\CollectToggle\" the_Name "_version_Number.ini")) {
		found_Location := "F:\COMPANY FILES\01 EMPLOYEE FILES\00 Me\Code\The Collector\CollectToggle\" the_Name "_version_Number.ini"
} else {
		found_Location := 0
}
					     Original
↑↑↑			↑↑↑			↑↑↑			↑↑↑			↑↑↑
↓↓↓			↓↓↓			↓↓↓			↓↓↓			↓↓↓
					       New

	filePath(base_Path) {
		Loop Parse DriveGetList()
{
			if FileExist(found_Location := A_LoopField ":" base_Path) {
				Return found_Location
}
}
}

	if (found_Location := filePath("\01 EMPLOYEE FILES\00 Me\Code\The Collector\CollectToggle\" the_Name "_version_Number.ini")) {
} else if (found_Location := filePath("\COMPANY FILES\01 EMPLOYEE FILES\00 Me\Code\The Collector\CollectToggle\" the_Name "_version_Number.ini")) {
} else {
		return
}

User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: File Path Help

Post by mikeyww » 06 Jun 2023, 08:09

OK. I guess I'm not sure what the question is, or what the script should do. Others may have a better idea here.

User avatar
WarlordAkamu67
Posts: 219
Joined: 21 Mar 2023, 06:52

Re: File Path Help

Post by WarlordAkamu67 » 06 Jun 2023, 12:25

A compiled version would carry the installed compiled version of another script. This is designed for computers on the same network, with access to at least one shared folder.

1. Check each drive for a folder path, thus confirming our file structure and capturing a variable path.
2. Now that we know the File Structure is correct, check to see if an "INI" file for the appropriate script is there, if not create one. This is the current version.
3. If an "INI" file is there, then we will read it and check its "version number" against the one currently running, if they do not match exactly, an update is prompted.
4. Selecting "No" just continues on through the rest of the code.
5. Selecting "Yes" installs a second script and passes some information to it in the form of its own ".INI" and closes itself (ExitApp). The second script (which is was compiled into this one and FileInstalled) reads the ".INI" and copies, what should be a new compiled version, the corresponding ".EXE" into the old ones place, then it is run and the second script deletes itself.
6. Check again every hour.

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force

; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ;

if (A_IsCompiled && !IsSet(ver_Now)) {
	ver_Now := "2.2.9"
	the_Name := "AIO_Suite"
	locations_to_Check := ["\01 EMPLOYEE FILES\00 Me\Code\The Collector\CollectToggle\", "\COMPANY FILES\01 EMPLOYEE FILES\00 Me\Code\The Collector\CollectToggle\"]
	check_for_update()
}

; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ;

file_Path(base_Path) {
  Loop Parse DriveGetList()
{
    if FileExist(found_Location := A_LoopField ":" base_Path) {
      Return found_Location
}
}
}

; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ;

check_for_update() {
	global

	for check_Location in locations_to_Check {
		if (found_Location := file_Path(check_Location)) {
			break
} else {
			found_Location := 0
}
}

	if (!found_Location) {
		return
}
	found_File := found_Location the_Name "_version_Number.ini"

	if (FileExist(found_File)) {
		current := IniRead(found_File, "version", "current")
		if (ver_Now !== current) {
			while (A_TimeIdle < 500) {
			Sleep(1000)
}
		msgResult := MsgBox("Update available for " the_Name ", continue update?", "", 4)
		if (msgResult = "Yes") {
			try {
				FileDelete(A_Temp "\warlords_Script_Updater.ini")
}

		FileAppend
(
"[script_to_Update]
the_Name=" the_Name ".exe
working_Space=" A_ScriptFullPath
), A_Temp "\warlords_Script_Updater.ini"

		FileInstall("Z:\01 EMPLOYEE FILES\00 Me\Executables\warlords_Script_Updater.exe", A_Temp "\warlords_Script_Updater.exe", 1)
		Run(A_Temp "\warlords_Script_Updater.exe")
		ExitApp()
}
}
} else {

	FileAppend
(
"[version]
current=" ver_Now "
)", found_File

}
	SetTimer(check_for_update,360000)
	return
}

; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ;
Updater Script. Is normally compiled and installed from the first script.

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force

; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ;

remove_Updater(*) {
  my_File_Delete(A_Temp "\warlords_Script_Updater.ini")
  my_File_Delete(A_Temp "\warlords_Script_Updater.exe")
  return
}

; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ;

my_File_Delete(path_to_Delete) {
  try {
    FileDelete(path_to_Delete)
}
}

; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ;

file_Path(base_Path) {
  Loop Parse DriveGetList()
{
    if FileExist(found_Location := A_LoopField ":" base_Path) {
      Return found_Location
}
}
}

; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ;
; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ;
; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ;

locations_to_Check := ["\01 EMPLOYEE FILES\00 Me\Executables\", "\COMPANY FILES\01 EMPLOYEE FILES\00 Me\Executables\"]

OnExit(remove_Updater)

if (FileExist(A_Temp "\warlords_Script_Updater.ini")) {
  script_Name := IniRead(A_Temp "\warlords_Script_Updater.ini", "script_to_Update", "the_Name")
  script_Location := IniRead(A_Temp "\warlords_Script_Updater.ini", "script_to_Update", "working_Space")

for check_Location in locations_to_Check {
	if (found_Location := file_Path(check_Location) script_Name) {
	 break
} else {
    found_Location := 0
}
}

  if (FileExist(found_Location)) {
    FileCopy(found_Location, script_Location, 1)
    while (!FileExist(script_Location)) {
      Sleep(50)
}
    MsgBox(script_Name " has been updated.")
    Run(script_Location)
} else {
    MsgBox("No Updated File Available.")
    try {
      Run(script_Location)
}
}
} else {
  MsgBox("Update Failed.")
}

ExitApp()

User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: File Path Help

Post by mikeyww » 06 Jun 2023, 19:28

OK. Your original problem statement was
I want to check for a file path, but may be unsure of what drive it is under.
We solved that. I'm still not sure what the next question is, but others may have some better ideas based on what you wrote.

Your script is hard to follow. You might want to attend to the indentation, and consider shortening it during your testing, so that you can focus on the problematic areas.

Post Reply

Return to “Ask for Help (v2)”