gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 02:13:16 25 4
gpt4 key购买 nike

如何在变量中获取 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

它不起作用。

最佳答案

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

此批处理代码仅在带有 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 /?
  • 关于batch-file - 在批处理文件中获取 Windows 下载文件夹的路径(下载 shell 文件夹),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27460275/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com