Search code examples
batch-fileshellexecute

open folder by input in batch and C++ code


I searched how to open the folder in batch code as following

%SystemRoot%\explorer.exe "c:\Yaya\yoyo\"

However, what if I want to give the specific folder everytime i execute the batch program?

If you don't mind, could you also please tell me how to do it in C++ as well? It is hard to change the path by having scanf..

Currently I have

#include <windows.h>
#include <iostream>
int main ()
 {
     HINSTANCE result;
     result=ShellExecute(NULL,NULL,L"c:\my_folder_path_by_input",NULL,NULL,SW_SHOWDEFAULT);
     if ((int)result<=32)
     std::cout << "Error!\nReturn value: " << (int)result << "\n";
     return 0;
 }

Solution

  • Run the batch file with path of folder to open as parameter and use in batch file %SystemRoot%\explorer.exe "%~1" or perhaps even better %SystemRoot%\explorer.exe /e,"%~1".

    Example:

    Batch file OpenFolder.bat contains:

    @echo off
    if "%~1" == "" (
        %SystemRoot%\explorer.exe
    ) else (
        %SystemRoot%\explorer.exe /e,"%~1"
    )
    

    This batch file is started for example with one of the lines below:

    OpenFolder.bat
    OpenFolder.bat %windir%\Temp
    OpenFolder.bat "%TEMP%"
    OpenFolder.bat "%APPDATA%"
    OpenFolder.bat "%USERPROFILE%\Desktop"
    

    Enclosing the folder path in double quotes is always possible, but really required are the double quotes on running OpenFolder if the folder path contains anywhere a space character or one of these characters: &()[]{}^=;!'+,`~

    See also Windows Explorer Command-Line Options

    I'm not sure why a batch file is needed at all to open a specific folder in Windows Explorer. Pressing Windows+E opens in any Windows starting from Windows 95 a new Windows Explorer window. And entering in address bar of Explorer window one of the strings above results in displaying the appropriate folder. See also Microsoft page about Windows Keyboard Shortcuts.

    Here is one more batch version asking user for a folder path if not specified as parameter on starting the batch file.

    @echo off
    if not "%~1" == "" (
        %SystemRoot%\explorer.exe /e,"%~1"
        goto :EOF
    )
    
    rem There is no folder path specified as parameter.
    rem Prompt user for folder path and predefine the environment variable
    rem with a double quote as value to avoid batch processing exit because of
    rem a syntax error if the batch file user just hits key RETURN or ENTER.
    
    set "FolderPath=""
    set /P "FolderPath=Folder path: "
    
    rem Remove all double quotes from string entered by the user.
    
    set "FolderPath=%FolderPath:"=%"
    
    if "%FolderPath%" == "" (
        %SystemRoot%\explorer.exe
    ) else (
        %SystemRoot%\explorer.exe /e,"%FolderPath%"
        set "FolderPath="
    )
    

    For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

    • call /? ... explains %~1 (first parameter without double quotes).
    • echo /?
    • goto /?
    • if /?
    • rem /?
    • set /?