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

时间:2014-12-13 15:09:24

标签: batch-file cmd

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

根据this。我试过了:

@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

它不起作用。

1 个答案:

答案 0 :(得分:3)

这是一个批量代码,可以获得多个下载目录,我认为这些目录是自我解释的。

此批处理代码仅在具有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

<强>更新

但是对于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

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

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