如何使用批处理获取桌面路径

时间:2014-11-23 23:37:10

标签: windows batch-file registry desktop

我想使用批处理文件从Windows注册表获取桌面路径。我创建了以下代码,但它打印出来:

Desktop        REG_SZ  C:\Documents and Settings\Usuario\Escritorio

我是如何只在 DESKTOP_DIR 变量中设置路径的?

以下是代码:

@ECHO OFF

SET DESKTOP_REG_ENTRY="HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
SET DESKTOP_REG_KEY="Desktop"
SET DESKTOP_DIR=

FOR /F "tokens=*" %%a IN ('REG QUERY %DESKTOP_REG_ENTRY% /v %DESKTOP_REG_KEY% ^| FINDSTR "REG_SZ"') DO (
    ECHO %%a
)

ECHO Desktop dir: %DESKTOP_DIR%
PAUSE

1 个答案:

答案 0 :(得分:1)

像这样:

@echo off

SET DESKTOP_REG_ENTRY="HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
SET DESKTOP_REG_KEY="Desktop"
SET DESKTOP_DIR=

FOR /F "tokens=1,2*" %%a IN ('REG QUERY %DESKTOP_REG_ENTRY% /v %DESKTOP_REG_KEY% ^| FINDSTR "REG_SZ"') DO (
    set DESKTOP_DIR="%%c"
)

ECHO Desktop dir: %DESKTOP_DIR%
PAUSE