AnonymousX

Run/ShellExecute windows folder locations

6 posts in this topic

Hello,

What command would I use to open the following folder directories for:

Computer/This PC (windows 7/Windows 10)

Libraries\documents

Libraries\Pictures

I know:

 Run("Explorer.exe " & $filepath)
 ShellExecute($filepath)

can be used to open folder locations when you type in the full directory such as "C:\Users\%username%\Documents" but I'm trying to figure out how to use these windows short paths. 

Thanks,

Share this post


Link to post
Share on other sites



21 hours ago, Subz said:

Sorry pushed submit by accident, I meant to say

Do you mean macros like @DocumentsDir or did you want special folders like:

_WinAPI_ShellGetSpecialFolderPath 

https://msdn.microsoft.com/en-us/library/windows/desktop/bb762494(v=vs.85).aspx

Referring to things like @DocumentsDir (Which gives me an error of undefined macro. Guessing I'm missing an #include?)

 

See right now, before a user is allowed to save an address I use:

FileExists($inputaddress)

to check if the path exists, but in this case if $inputaddress was to be "Libraries\Pictures" this would return 0 (Path/file does not exist). However if I go into windows explore and type "Libraries\Pictures" (or just "Pictures" for that matter) it takes me to C:\Users\(username)\Pictures and will not display the full path it just shows "Libraries\Documents".

So I'm trying to find out if there is a way that if the user enters in one of these shorten versions of the file path such as "Libraries\Pictures" the code will be able to recognize this as a true path and will be able to open that file location.

With further testing I've discovered that both:

ShellExecute("Libraries\Pictures")
Run('Explorer.exe ' & "Libraries\Pictures")

Don't work either, so I'm starting to think this might be a complicated thing to do for me....

Share this post


Link to post
Share on other sites

Sorry, @DocumentsDir should have been - @MyDocumentsDir

Libraries are ultimately shortcut files with the extension .library-ms files and can be located in @AppDataDir & '\Microsoft\Windows\Libraries.

You can use something like the following and it will open to that location

ShellExecute('Explorer.exe', @AppDataDir & '\Microsoft\Windows\Libraries\Pictures.library-Ms')

The file itself is actually xml, if you use a parser you can grab the <simplelocation>><url>knownfolder:{Guid} you can view a list of Guids and there locations here:

https://msdn.microsoft.com/en-us/library/bb882665(v=vs.110).aspx

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

I created a function to do something similar a while ago. You may be able to adapt for your needs.

#include <WinAPIShellEx.au3>
#include <WinAPI.au3>

Local $s_FilePath = _Win7_GetKnownFolder('PicturesLibrary')

if @error Then
    MsgBox(0,'DOH!', $s_FilePath)
Else
    MsgBox(0,'Yay Found', 'Path: '& $s_FilePath & @crlf & 'File Exists: ' & FileExists($s_FilePath))
EndIf


; #FUNCTION# ====================================================================================================================
; Name ..........: _Win7_GetKnownFolder
; Description ...: Retrieves the full path of a known folder
; Syntax ........: _Win7_GetKnownFolder($s_Folder)
; Parameters ....: $s_Folder  - A string value denoting the known folder
; Return values .: Success - Returns the full folder path
;                  Failure - Sets @error to 1 and returns a string for logging
; Remarks .......: Only for Windows Vista and above
; Related .......: _XP_GetSpecialFolder (for XP and lower)
; Link ..........: link http://msdn.microsoft.com/en-us/library/dd378457.aspx
; Example .......: _Win7_GetKnownFolder('CommonPrograms')
; ===============================================================================================================================
Func _Win7_GetKnownFolder($s_Folder)
    ; trimmed list of KnownFolder constants
    Local $s_Constants = 'CommonPrograms|' & _ ;%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs
            'CommonStartMenu|' & _ ; %ALLUSERSPROFILE%\Microsoft\Windows\Start Menu
            'CommonStartup|' & _ ; %ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\StartUp
            'CommonTemplates|' & _ ; %ALLUSERSPROFILE%\Microsoft\Windows\Templates
            'Desktop|' & _ ; %USERPROFILE%\Desktop
            'Documents|' & _ ; %USERPROFILE%\Documents
            'DocumentsLibrary|' & _ ; %APPDATA%\Microsoft\Windows\Libraries\Documents.library-ms
            'Favorites|' & _ ; %USERPROFILE%\Favorites
            'Fonts|' & _ ; %windir%\Fonts
            'Libraries|' & _ ; %APPDATA%\Microsoft\Windows\Libraries
            'LocalAppData|' & _ ; %LOCALAPPDATA% (%USERPROFILE%\AppData\Local)
            'LocalAppDataLow|' & _ ; %USERPROFILE%\AppData\LocalLow
            'Music|' & _ ; %USERPROFILE%\Music
            'MusicLibrary|' & _ ; %APPDATA%\Microsoft\Windows\Libraries\Music.library-ms
            'PhotoAlbums|' & _ ; %USERPROFILE%\Pictures\Slide Shows
            'PicturesLibrary|' & _ ; %APPDATA%\Microsoft\Windows\Libraries\Pictures.library-ms
            'Pictures|' & _ ; %USERPROFILE%\Pictures
            'Playlists|' & _ ; %USERPROFILE%\Music\Playlists
            'Profile|' & _ ; %USERPROFILE% (%SystemDrive%\Users\%USERNAME%)
            'ProgramData|' & _ ; %ALLUSERSPROFILE% (%ProgramData%, %SystemDrive%\ProgramData)
            'ProgramFiles|' & _ ; %ProgramFiles% (%SystemDrive%\Program Files)
            'ProgramFilesX64|' & _ ; %ProgramFiles% (%SystemDrive%\Program Files)
            'ProgramFilesX86|' & _ ; %ProgramFiles% (%SystemDrive%\Program Files)
            'ProgramFilesCommon|' & _ ; %ProgramFiles%\Common Files
            'ProgramFilesCommonX64|' & _ ; %ProgramFiles%\Common Files
            'ProgramFilesCommonX86|' & _ ; %ProgramFiles%\Common Files
            'Programs|' & _ ; %APPDATA%\Microsoft\Windows\Start Menu\Programs
            'Public|' & _ ; %PUBLIC% (%SystemDrive%\Users\Public)
            'PublicDesktop|' & _ ; %PUBLIC%\Desktop
            'PublicDocuments|' & _ ; %PUBLIC%\Documents
            'PublicLibraries|' & _ ; %ALLUSERSPROFILE%\Microsoft\Windows\Libraries
            'PublicMusic|' & _ ; %PUBLIC%\Music
            'PublicPictures|' & _ ; %PUBLIC%\Pictures
            'PublicVideos|' & _ ; %PUBLIC%\Videos
            'QuickLaunch|' & _ ; %APPDATA%\Microsoft\Internet Explorer\Quick Launch
            'ResourceDir|' & _ ; %windir%\Resources
            'RoamingAppData|' & _ ; %APPDATA% (%USERPROFILE%\AppData\Roaming)
            'SendTo|' & _ ; %APPDATA%\Microsoft\Windows\SendTo
            'StartMenu|' & _ ; %APPDATA%\Microsoft\Windows\Start Menu
            'Startup|' & _ ; %APPDATA%\Microsoft\Windows\Start Menu\Programs\StartUp
            'System|' & _ ; %windir%\system32
            'SystemX86|' & _ ; %windir%\system32
            'Templates|' & _ ; %APPDATA%\Microsoft\Windows\Templates
            'UserPinned|' & _ ; %APPDATA%\Microsoft\Internet Explorer\Quick Launch\User Pinned
            'UserProfiles|' & _ ; %SystemDrive%\Users
            'UserProgramFiles|' & _ ; %LOCALAPPDATA%\Programs
            'UserProgramFilesCommon|' & _ ; %LOCALAPPDATA%\Programs\Common
            'Videos|' & _ ; %USERPROFILE%\Videos
            'VideosLibrary|' & _ ; %APPDATA%\Microsoft\Windows\Libraries\Videos.library-ms
            'Windows' ; %windir%

    ; check the passed folder name is listed
    If Not _String_SearchForWords($s_Constants, $s_Folder) Then Return SetError(1, 0, '"' & $s_Folder & '" ' & ' is not listed in the Known Folder list')

    ; return the value of the variable
    Local $s_GUID = Eval('FOLDERID_' & $s_Folder)

    ; get the known folder path
    Local $s_FolderPath = _WinAPI_ShellGetKnownFolderPath($s_GUID)
    If @error Then Return SetError(@error, @extended, _WinAPI_GetLastErrorMessage())
    Return $s_FolderPath
EndFunc   ;==>_Win7_GetKnownFolder

; #FUNCTION# ====================================================================================================================
; Name ..........: _String_SearchForWords
; Description ...: Search for the existance of multiple words in a given string
; Syntax ........: _String_SearchForWords($sCallingFunc, $sTest, $sSearch)
; Parameters ....: $sTest        - The string to run the check against
;                  $sSearch      - The string(s) to search for
; Return values .: Success - Returns 0 - No Match
;                                    1 - Match
;                  Failure - Sets @error to 1 and logs an error message
; Example .......: MsgBox(0,'', _String_SearchForWords('', 'batcave1,batcave2,xbmc,benneraswell', 'batcave'))
; ===============================================================================================================================
Func _String_SearchForWords($sTest, $sSearch)
    Local $sRunningFunc = '_String_SearchForWords'
    Local $sPattern = '(?i)^(?=.*\b' & StringReplace($sSearch, '|', '\b)(?=.*\b') & '\b)'
    Local $iRet = StringRegExp($sTest, $sPattern)

    If @error Then Return SetError(@error, @extended, '')
    Return $iRet
EndFunc   ;==>_String_SearchForWords

Or probably better for you

Local $s_FileName = FileSelectFolder('Please Select The Folder...', '', 3, "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}")

If Not @error Then
    ; get the folder name of any special folders
    If StringInStr($s_FileName, '::{') Then
        $s_DisplayName = SpecialFolders_GetSelfName($s_FileName)
        $s_FileName = SpecialFolder_ReturnPath($s_FileName)
    EndIf

    MsgBox(0,'', $s_FileName)
    ; check fileexists
EndIf

Func SpecialFolder_ReturnPath($s_Path)
    Local $s_Lnk = @TempDir & '\FileName.lnk'

    ; creste a shortcut for the path
    If Not FileCreateShortcut($s_Path, $s_Lnk) Then Return $s_Path

    ; read the shortcuts property to an array
    Local $a_Shortcut = FileGetShortcut($s_Lnk)
    If @error Then Return $s_Path
    FileDelete($s_Lnk)

    ; get the shortcut target path
    If IsArray($a_Shortcut) Then Return $a_Shortcut[0]
    Return $s_Path
EndFunc   ;==>SpecialFolder_ReturnPath

Func SpecialFolders_GetSelfName($s_Path)
    Local $objShell = ObjCreate("Shell.Application")
    If Not IsObj($objShell) Then Return ''

    Return $objShell.NameSpace($s_Path).Self.Name
EndFunc   ;==>SpecialFolders_GetSelfName

 

Edited by benners
1 person likes this

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Similar Content

    • nacerbaaziz
      By nacerbaaziz
      Hello
      How are you
      I have a question, please.
      I've created a window and i need
      when i run the script if it detect an other copy from the same script was runing
       the script close the first copy Automatically and run a new copy
      I want an example to understand it
    • Mag91
      By Mag91
      Hey Community,
      cause im too new in the Auto it world i will try it with the your help. hopefully.
      I woud like to know how i can handle my Problem.
      ----
      I have a Excel Data with 362 random numbers.
      For Example:
      1166642335374 1172899897343
      .....
      this numbers are a part of the filepath ...example
      D:\Projekte\1166_64233_5374
      as u can see its the first number of the Excel data. After the first 4 numbers it shoud make a "_" than another 5 "_"
      This is my first question. How can i handle this to make it Shell execute.
       
      --------
      Second question:
      If i am in the path.
      For Example:
      D:\Projekte\1166_64233_5374
      the code shoud search for specific PDF Files.
      They are named like: 0050569E364B1ED79B900F73E62660EC.pdf
      the first 15 letters are always the same
      0050569E364B1ED
      when he found this data he has to copy it on a Folder on the Desktop.
      (There can also be 2 or 3 pdfs in one Folder with this letters)
      ----
      Please give me some help :-)
       
       
       
       
       
       
    • kawliga751
      By kawliga751
      I am attempting to run an Autoit.exe with schtasks every weekday @ 9:05 am.
      I have the below
      C:\>SchTasks /Create /TN "P10Run" /TR "O:\AUTOIT\P40 Run.au3" /SC WEEKLY /D MON, TUE,WED,THU,FRI /ST 09:05:00 SUCCESS: The scheduled task "P10Run" has successfully been created. I also did a "test" run :
      C:\>Schtasks /Run /TN "P10Run SUCCESS: Attempted to run the scheduled task "P10Run". But when I run my query to verify success my "Last Result" seems to indicate that the script did not run:
      C:\>SCHTASKS /QUERY /FO LIST /V /TN "P10RUN" Folder: \ HostName: xxxxxxxxxxxxxxxx TaskName: \P10RUN Next Run Time: N/A Status: Could not start Logon Mode: Interactive only Last Run Time: 6/14/2017 9:08:56 AM Last Result: -2147024894 Author: xxxxxxx Task To Run: O:\AUTOIT\P40 Run.au3 Start In: N/A Comment: N/A Scheduled Task State: Enabled Idle Time: Disabled Power Management: Stop On Battery Mode, No Start On Batterie s Run As User: xxxxxxxxx\xxxxxxxx Delete Task If Not Rescheduled: Enabled Stop Task If Runs X Hours and X Mins: 72:00:00 Schedule: Scheduling data is not available in this f ormat. Schedule Type: Weekly Start Time: 9:05:00 AM Start Date: 6/14/2017 End Date: N/A Days: MON, TUE, WED, THU, FRI Months: Every 1 week(s) Repeat: Every: Disabled Repeat: Until: Time: Disabled Repeat: Until: Duration: Disabled Repeat: Stop If Still Running: Disabled C:\> Is there something missing in my command? Also is there a better way to make sure that the script does in fact sun successfully?
      Thanks In Advance! 
    • StMaSi
      By StMaSi
      I'm attempting to code a Run or RunWait command that is a little on the long side and contains multiple sets of embedded quotes (as it would be issued from a CMD prompt), but can't for the life of me figure out the containing quotes as pertains to AutoIt. Here's the one-line command as it would be issued via a CMD prompt...
      "\\AppServ\G$\Connectivity\Clients\Citrix\Receiver\14.5.0.10018\CitrixReceiver.exe" ADDLOCAL="ReceiverInside,ICA_Client,WebHelper,SSON,AM,SELFSERVICE,USB,DesktopViewer,Flash,Vd3d" ALLOWADDSTORE=A ALLOWSAVEPWD=A ENABLEPRELAUNCH=Y ENABLE_SSON=Yes STORE0="AppStore;https://receiver.domain.net/Citrix/Apps/discovery;on;AppStore" /EnableCEIP=false /includeSSON /noreboot /silent I've tried so many different combinations of surrounding quotes with Run and RunWait, I just don't understand why it's this difficult to get past this.
      Can anyone assist with this?
      Thank you.
    • WoodGrain
      By WoodGrain
      Hi guys,
      This is probably an obvious one, but I really don't use this command at all so am hoping you can spot my mistake.
      I'm running a powershell script on a schedule with the following script in a function, the function is being called in a loop, but the console process is not closing in the background and I end up with a bunch of console windows running in the background:
      Run(@comspec & ' /k PowerShell.exe -STA -NonInteractive -ExecutionPolicy ByPass -Command "& ''Z:\Powershell\365\GetNextDetails.ps1'' "', "", @SW_HIDE) Thanks!