newbie needs wildcard in directory...

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Tim_H
Posts: 16
Joined: 05 Aug 2021, 19:43

newbie needs wildcard in directory...

06 Mar 2022, 21:13

Hi!

I'm using AHK on two different laptops to launch some destination folders on my hard disk real quickly...
But unfortunately the Admin folder on both systems isn't the same: on one it is called "Admin", on the other it is "SquirlyPearly2000"...

For now I am using two different AHK scripts, one for each laptop.
But that is not a very elegant solution... And it's a pain in the ass once I start adding or editing my script(s).

So is there a way that I can change two separate scripts like this...

laptop 1:

Code: Select all

:o:dr :: 
Run "C:\Users\Admin\Dropbox\"
return
laptop 2:

Code: Select all

:o:dr :: 
Run "C:\Users\SquirlyPearly2000\Dropbox\"
return
...for something like this?

Code: Select all

:o:dr :: 
Run "C:\Users\???\Dropbox\"
return

I found some stuff about wildcards here, but it is way beyond my level of comprehension, haha...

Thanks a bunch for helping out!
User avatar
boiler
Posts: 17387
Joined: 21 Dec 2014, 02:44

Re: newbie needs wildcard in directory...

06 Mar 2022, 22:03

I guess you're saying you're not logged in under that username. If you are, you can just use A_UserName. If not, this should do it:

Code: Select all

:o:dr :: 
loop, Files, C:\Users\*, D
	if InStr(FileExist(A_LoopFileFullPath "\Dropbox"), "D")
		AdminDropboxDir := A_LoopFileFullPath "\Dropbox"
Run, % AdminDropboxDir
return
If there is more than one directory two levels below C:\Users that contains a Dropbox directory, the above script will find the last one. It can be changed to find the first one (or any in between, I suppose). But you probably only have one anyway.
Tim_H
Posts: 16
Joined: 05 Aug 2021, 19:43

Re: newbie needs wildcard in directory...

07 Mar 2022, 21:00

Hello!
Thanks for helping out.

What do you mean with:
I guess you're saying you're not logged in under that username.

I am Admin on both machines... It just happens to be that the name of the Admin user is different.
(When installing Windows you can choose for a custom Admin name; it doesn't have to be "Admin".)

I tried the first option you suggested with A_Username like this, but it doesn't work:

Code: Select all

:o:dr :: ; 
Run "C:\Users\A_UserName\Dropbox\"
Is this what you meant?

Would be convenient if something simple like this worked, because I can still understand it, haha. :)
I didn't try the second option yet.
Hardcoder
Posts: 278
Joined: 19 Feb 2022, 13:13
Location: Germany

Re: newbie needs wildcard in directory...

08 Mar 2022, 04:25

Tim_H wrote:
07 Mar 2022, 21:00
I tried the first option you suggested with A_Username like this, but it doesn't work:

Code: Select all

:o:dr :: ; 
Run "C:\Users\A_UserName\Dropbox\"
Is this what you meant?

Would be convenient if something simple like this worked, because I can still understand it, haha. :)
I didn't try the second option yet.
He thought you want to run this for different users on your system, but if it's always yourself...

Run is a command, as such it takes literal arguments, your variable A_UserName won't be expanded, you have to do that yourself:

Code: Select all

Run, C:\Users\%A_UserName%\Dropbox\
User avatar
boiler
Posts: 17387
Joined: 21 Dec 2014, 02:44

Re: newbie needs wildcard in directory...

08 Mar 2022, 07:33

Yes, if you’re actually logged in as that user name on each machine then using A_UserName will work if you correct the syntax as Hardcoder showed.
Tim_H
Posts: 16
Joined: 05 Aug 2021, 19:43

Re: newbie needs wildcard in directory...

08 Mar 2022, 22:38

He thought you want to run this for different users on your system
No, it's different laptops, only 1 user. :)
Tim_H
Posts: 16
Joined: 05 Aug 2021, 19:43

Re: newbie needs wildcard in directory...

08 Mar 2022, 22:43

Run is a command, as such it takes literal arguments, your variable A_UserName won't be expanded, you have to do that yourself:

Code: Select all

Run, C:\Users\%A_UserName%\Dropbox\
FABULOUS! This is exactly what I needed!!!! :) :)

Thanks a bunch, @Hardcoder. You just learned a newbie a new trick. ;)
Tim_H
Posts: 16
Joined: 05 Aug 2021, 19:43

Re: newbie needs wildcard in directory...

14 Jul 2022, 20:17

Hello @Hardcoder , can I ask your help again?

I had to reset my script and now for some reason it's not working anymore. :(

This is what I tried:

Code: Select all

:o:dr :: ; Launch My Documents
Run, C:\Users\%A_UserName%\Documents\
return

And I get this error message.
afbeelding.png
afbeelding.png (23.48 KiB) Viewed 1622 times

The part in Dutch says: "system can't find the file".
While I really want it to open a FOLDER.

Can you help me out once more? :o
User avatar
boiler
Posts: 17387
Joined: 21 Dec 2014, 02:44

Re: newbie needs wildcard in directory...

14 Jul 2022, 21:27

It's better to use A_MyDocuments than to assume the path:

Code: Select all

Run, %A_MyDocuments%
(it's also better to ask the forum in general rather than one member specifically)
Tim_H
Posts: 16
Joined: 05 Aug 2021, 19:43

Re: newbie needs wildcard in directory...

15 Jul 2022, 21:02

Hello @boiler, thanks for your suggestions, I will keep them in mind!

The "My Documents" folder was just a simplified example. Actually I want to open my Mega Sync folder, that resides under "Users". So path is: C:\Users\Username\MEGA.
User avatar
boiler
Posts: 17387
Joined: 21 Dec 2014, 02:44

Re: newbie needs wildcard in directory...

15 Jul 2022, 22:32

Are you sure there’s not a file named MEGA at the same level where you say the folder named MEGA is supposed to be?
Tim_H
Posts: 16
Joined: 05 Aug 2021, 19:43

Re: newbie needs wildcard in directory...

17 Jul 2022, 16:46

Hello, yes I'm sure!
That's why I did a test with both C:\Users\%A_Username%\MEGA and C:\Users\%A_Username%\Documents - to rule out any mysterious exceptions...

If I don't use %A_Username% but just the actual username on this machine, it works fine though.
User avatar
boiler
Posts: 17387
Joined: 21 Dec 2014, 02:44

Re: newbie needs wildcard in directory...

17 Jul 2022, 17:53

What does it show when you run the following script? Run it exactly as shown. Don’t edit in any username or anything else.

Code: Select all

MsgBox, % FileExist("C:\Users\" A_Username "\MEGA") . "`n" . A_UserName
Tim_H
Posts: 16
Joined: 05 Aug 2021, 19:43

Re: newbie needs wildcard in directory...

20 Jul 2022, 21:27

It shows a name I previously used as a user name..
afbeelding.png
afbeelding.png (4.47 KiB) Viewed 1053 times
User avatar
boiler
Posts: 17387
Joined: 21 Dec 2014, 02:44

Re: newbie needs wildcard in directory...

20 Jul 2022, 21:51

It also would show something above that if the folder was found, so it wasn’t able to find it using that path. To AHK, that is not a valid file path. It seems that somehow Windows is using an old version of your username for that environment variable. See how to change it here.
Tim_H
Posts: 16
Joined: 05 Aug 2021, 19:43

Re: newbie needs wildcard in directory...

27 Jul 2022, 20:07

But... isn't the point of using %A_Username% that the username doesn't matter? :o
User avatar
boiler
Posts: 17387
Joined: 21 Dec 2014, 02:44

Re: newbie needs wildcard in directory...

28 Jul 2022, 06:37

No, it doesn’t mean that at all. It doesn’t mean “skip over this part where the username would be.” It means “put the username of the loged-in user here.”

In case you’re asking why that’s useful because you can just type your username, imagine code that is meant to be used by more than one person, not just yourself.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 286 guests