在批处理文件中获取 Windows 下载文件夹的路径(下载 shell 文件夹)

Pur*_*Fox 6 cmd batch-file

如何在变量中获取 Windows 下载 Shell 文件夹?

根据。我试过:

@echo off
SETLOCAL

FOR /F "usebackq" %%f IN (`PowerShell -NoProfile -Command "Write-Host([Environment]::GetFolderPath('{374DE290-123F-4565-9164-39C4925E467B}'))"`) DO ( SET "DOWNLOAD_FOLDER=%%f" )

@ECHO %DOWNLOAD_FOLDER%
pause
Run Code Online (Sandbox Code Playgroud)

它不起作用。

The*_*Jan 6

我不明白这些废话有什么意义。

只需使用%userprofile%\Downloads.

  • ❌ 如果用户更改了属性中的文件夹位置,```%userprofile%\Downloads``` 确实不起作用 - 按照上面的注释(并在测试后明确确认),我已将位置更改为 [ D:\Downloads] 并在尝试 %userprofile%\Downloads 时出现“无法找到指定路径”错误...所以就“*不明白**废话**是关于什么的*”而言;OP 和像我这样的人想要的是一种**安全**的方式来始终引用用户的 CSLID 下载文件夹,无论它位于何处。 (3认同)
  • 非英语操作系统可能使用不同的路径,例如“%userprofile%\téléchargements”。另外,用户可以将其更改为他们想要的任何位置,例如“E:\Stuff” (2认同)

Mof*_*ofi 4

这是一个获取多个下载目录的批处理代码,我认为这是不言自明的。

此批处理代码仅在装有 Internet Explorer 8 的 Windows XP x86 上进行了测试。

@echo off
setlocal EnableExtensions DisableDelayedExpansion

set "Reg32=%SystemRoot%\System32\reg.exe"
if not "%ProgramFiles(x86)%" == "" set "Reg32=%SystemRoot%\SysWOW64\reg.exe"

set "DownloadDirectory="
for /F "skip=4 tokens=3*" %%U in ('%Reg32% query "HKCU\Software\Microsoft\Internet Explorer" /v "Download Directory" 2^>nul') do (
    set "DownloadDirectory=%%V"
    goto GetSaveDir
)

:GetSaveDir
set "SaveDirectory="
for /F "skip=4 tokens=3*" %%U in ('%Reg32% query "HKCU\Software\Microsoft\Internet Explorer\Main" /v "Save Directory" 2^>nul') do (
    set "SaveDirectory=%%V"
    goto OutputResults
)

:OutputResults
cls
echo/

echo Download directory of user account is:
echo/
echo    %USERPROFILE%\Downloads
echo/
echo/

if not defined DownloadDirectory goto OutputSaveDir
if "%DownloadDirectory:~-1%" == "\" set "DownloadDirectory=%DownloadDirectory:~0,-1%"
echo Download directory of Internet Explorer is:
echo/
echo    %DownloadDirectory%
echo/
echo/

:OutputSaveDir
if not defined SaveDirectory goto EndBatch
if "%SaveDirectory:~-1%" == "\" set "SaveDirectory=%SaveDirectory:~0,-1%"
echo Save directory of Internet Explorer is:
echo/
echo    %SaveDirectory%

:EndBatch
endlocal
Run Code Online (Sandbox Code Playgroud)

更新

但对于 Windows Vista/7/8/8.1/10,需要增强的批处理文件,因为在使用 Internet Explorer 8/9/10/11 的更高版本的 Windows 上,下载目录的定义有所不同。

下面的批处理代码适用于从 Windows 2000 开始的所有 Windows 操作系统。

它输出在硬盘(第一个)或 Windows 注册表(其余三个)中找到的目录。

@echo off
setlocal EnableExtensions DisableDelayedExpansion

set "Reg32=%SystemRoot%\System32\reg.exe"
if not "%ProgramFiles(x86)%" == "" set "Reg32=%SystemRoot%\SysWOW64\reg.exe"

set "DownloadShellFolder="
for /F "skip=1 tokens=1,2*" %%T in ('%Reg32% query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "{374DE290-123F-4565-9164-39C4925E467B}" 2^>nul') do (
    if /I "%%T" == "{374DE290-123F-4565-9164-39C4925E467B}" (
        set "DownloadShellFolder=%%V"
        goto GetDownloadDirectory
    )
)

:GetDownloadDirectory
set "DownloadDirectory="
for /F "skip=1 tokens=1,2,3*" %%S in ('%Reg32% query "HKCU\Software\Microsoft\Internet Explorer" /v "Download Directory" 2^>nul') do (
    if /I "%%S" == "Download" (
        if /I "%%T" == "Directory" (
            set "DownloadDirectory=%%V"
            goto GetSaveDirectory
        )
    )
)

:GetSaveDirectory
set "SaveDirectory="
for /F "skip=1 tokens=1,2,3*" %%S in ('%Reg32% query "HKCU\Software\Microsoft\Internet Explorer\Main" /v "Save Directory" 2^>nul') do (
    if /I "%%S" == "Save" (
        if /I "%%T" == "Directory" (
            set "SaveDirectory=%%V"
            goto OutputResults
        )
    )
)

:OutputResults
cls
echo/

if not exist "%USERPROFILE%\Downloads" goto OutputShellFolder
echo Download directory of user account is:
echo/
echo   %USERPROFILE%\Downloads
echo/
echo/

:OutputShellFolder
if not defined DownloadShellFolder goto OutputDownloadDir
if "%DownloadShellFolder:~-1%" == "\" set "DownloadShellFolder=%DownloadShellFolder:~0,-1%"
echo Download shell folder of user account is:
echo/
echo   %DownloadShellFolder%
echo/
echo/

:OutputDownloadDir
if not defined DownloadDirectory goto OutputSaveDir
if "%DownloadDirectory:~-1%" == "\" set "DownloadDirectory=%DownloadDirectory:~0,-1%"
echo Download directory of Internet Explorer is:
echo/
echo   %DownloadDirectory%
echo/
echo/

:OutputSaveDir
if not defined SaveDirectory goto EndBatch
if "%SaveDirectory:~-1%" == "\" set "SaveDirectory=%SaveDirectory:~0,-1%"
echo Save directory of Internet Explorer is:
echo/
echo   %SaveDirectory%

:EndBatch
endlocal
Run Code Online (Sandbox Code Playgroud)

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

  • cls /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • reg /?
  • reg query /?
  • set /?
  • setlocal /?