通过批量输入和C++代码打开文件夹

open folder by input in batch and C++ code

我搜索了如何在批处理代码中打开文件夹如下

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

但是,如果我想在每次执行批处理程序时都给出特定的文件夹怎么办?

如果你不介意的话,你能不能也告诉我如何用 C++ 来做? 很难通过 scanf..

来改变路径

目前我有

#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;
 }

运行 带有文件夹路径的批处理文件作为参数打开并在批处理文件中使用 %SystemRoot%\explorer.exe "%~1" 或者甚至更好 %SystemRoot%\explorer.exe /e,"%~1".

示例:

批处理文件 OpenFolder.bat 包含:

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

例如,此批处理文件使用以下行之一启动:

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

始终可以将文件夹路径括在双引号中,但如果文件夹路径包含 space 字符或以下字符之一,则真正需要的是 运行 OpenFolder 上的双引号这些字符:&()[]{}^=;!'+,`~

另见 Windows Explorer Command-Line Options

我不确定为什么需要批处理文件才能在 Windows 资源管理器中打开特定文件夹。按 Windows+E 打开从 Windows 95 开始的任何 Windows 一个新的 Windows 资源管理器 window。在资源管理器的地址栏中输入 window 上面的字符串之一会显示相应的文件夹。另请参阅有关 Windows Keyboard Shortcuts.

的 Microsoft 页面

这是另一个批处理版本,如果在启动批处理文件时未指定为参数,则要求用户提供文件夹路径。

@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="
)

要了解使用的命令及其工作原理,请打开命令提示符 window,在其中执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。

  • call /? ... 解释 %~1(第一个参数不带双引号)。
  • echo /?
  • goto /?
  • if /?
  • rem /?
  • set /?