当前位置: 代码网 > it编程>前端脚本>Dos/bat > Windows BAT获取开始菜单路径和桌面路径的实现

Windows BAT获取开始菜单路径和桌面路径的实现

2024年05月15日 Dos/bat 我要评论
有时候在bat批处理中需要向 开始菜单、 桌面、 任务栏添加文件夹/文件/快捷方式等,这就需要先获取到对应的路径。这篇文章将总结如何在bat批处理中获取 开始菜单、 桌面、 任务栏的绝对路径。一、解决

有时候在bat批处理中需要向 开始菜单、 桌面、 任务栏添加文件夹/文件/快捷方式等,这就需要先获取到对应的路径。
这篇文章将总结如何在bat批处理中获取 开始菜单、 桌面、 任务栏的绝对路径。

一、解决方案

方案1:通过环境变量获取

@echo off
:: 获取当前用户desktop路径
set desk_path=%userprofile%\desktop
echo desk_path=%desk_path%
:: 获取当前用户start menu路径
set sm_path=%userprofile%\appdata\roaming\microsoft\windows\start menu
::set sm_path=%appdata%\microsoft\windows\start menu
echo sm_path=%sm_path%
:: 获取当前用户taskbar路径
set tb_path=%userprofile%\appdata\roaming\microsoft\internet explorer\quick launch\user pinned\taskbar
::set tb_path=%appdata%\microsoft\internet explorer\quick launch\user pinned\taskbar
echo tb_path=%tb_path%
:: 获取公共用户desktop路径
set com_dp_path=%public%\desktop
::set com_dp_path=c:\users\public\desktop
echo com_dp_path=%com_dp_path%
:: 获取公共用户start menu路径
set com_sm_path=%allusersprofile%\microsoft\windows\start menu\programs
::set com_sm_path=%programdata%\microsoft\windows\start menu\programs
::set com_sm_path=c:\programdata\microsoft\windows\start menu\programs
echo com_sm_path=%com_sm_path%
:: 没有公共用户taskbar这个概念

输出结果:

desk_path=c:\users\cyinl\desktop
sm_path=c:\users\cyinl\appdata\roaming\microsoft\windows\start menu
tb_path=c:\users\cyinl\appdata\roaming\microsoft\internet explorer\quick launch\user pinned\taskbar
com_dp_path=c:\users\public\desktop
com_sm_path=c:\programdata\microsoft\windows\start menu\programs

方案2:通过注册表获取

@echo off
:: 获取当前用户desktop路径
::reg_sz
set desk_rq=reg query "hkcu\software\microsoft\windows\currentversion\explorer\shell folders" /v "desktop"
for /f "tokens=3,*" %%d in ('%desk_rq%') do set desk_path=%%d %%e
echo desk_path=%desk_path%
::reg_expand_sz
set desk_rq_ex=reg query "hkcu\software\microsoft\windows\currentversion\explorer\user shell folders" /v "desktop"
for /f "tokens=3,*" %%d in ('%desk_rq_ex%') do set desk_path_ex=%%d %%e
echo desk_path_ex=%desk_path_ex%
:: 获取当前用户start menu路径
::reg_sz
set sm_rq=reg query "hkcu\software\microsoft\windows\currentversion\explorer\shell folders" /v "start menu"
for /f "tokens=4,*" %%d in ('%sm_rq%') do set sm_path=%%d %%e
echo sm_path=%sm_path%
::reg_expand_sz
set sm_rq_ex=reg query "hkcu\software\microsoft\windows\currentversion\explorer\user shell folders" /v "start menu"
for /f "tokens=4,*" %%d in ('%sm_rq_ex%') do set sm_path_ex=%%d %%e
echo sm_path_ex=%sm_path_ex%
:: 用户taskbar路径,未注册到注册表
:: 获取公共用户desktop路径
::reg_sz
set com_desk_rq=reg query "hklm\software\microsoft\windows\currentversion\explorer\shell folders" /v "common desktop"
for /f "tokens=4,*" %%d in ('%com_desk_rq%') do set com_desk_path=%%d %%e
echo com_desk_path=%com_desk_path%
::reg_expand_sz
set com_desk_rq_ex=reg query "hklm\software\microsoft\windows\currentversion\explorer\user shell folders" /v "common desktop"
for /f "tokens=4,*" %%d in ('%com_desk_rq_ex%') do set com_desk_path_ex=%%d %%e
echo com_desk_path_ex=%com_desk_path_ex%
:: 获取公共用户start menu路径
::reg_sz
set com_sm_rq=reg query "hklm\software\microsoft\windows\currentversion\explorer\shell folders" /v "common start menu"
for /f "tokens=5,*" %%d in ('%com_sm_rq%') do set com_sm_path=%%d %%e
echo com_sm_path=%com_sm_path%
::reg_expand_sz
set com_sm_rq_ex=reg query "hklm\software\microsoft\windows\currentversion\explorer\user shell folders" /v "common start menu"
for /f "tokens=5,*" %%d in ('%com_sm_rq_ex%') do set com_sm_path_ex=%%d %%e
echo com_sm_path_ex=%com_sm_path_ex%
echo -----------------------------------------
::上述提取的路径,末尾都带有一个空格,(如果有必要)可以通过下述方法删除
set desk_path_ex=%desk_path_ex:~0,-1%
::将可扩展的路径,转为直接路径,以 desk_path_ex 为例
for /f "tokens=*" %%d in ('echo %desk_path_ex%') do set dpx_path=%%d
echo %desk_path_ex%=%dpx_path%

输出结果:

desk_path=c:\users\cyinl\desktop
desk_path_ex=%userprofile%\desktop
sm_path=c:\users\cyinl\appdata\roaming\microsoft\windows\start menu
sm_path_ex=%userprofile%\appdata\roaming\microsoft\windows\start menu
com_desk_path=c:\users\public\desktop
com_desk_path_ex=%public%\desktop
com_sm_path=c:\programdata\microsoft\windows\start menu
com_sm_path_ex=%programdata%\microsoft\windows\start menu
-----------------------------------------
%userprofile%\desktop=c:\users\cyinl\desktop

二、总结

一般情况下,方案一就可以满足需求了,但是有特殊情况,比如有极个别用户对桌面或开始菜单做了重定向,那么方案1可能就不可行了。这时,方案2就派上用场了。

注册表 rootkey,当前用户对应hkcu(hkey_current_user),公共用户对应hklm(hkey_local_machine);

注册表shell folders 项下的路径都是直接路径(不含变量);user shell folders项下的路径都是扩展路径(含变量);

关键代码解读:

通用格式如下:

for /f "tokens=3,*" %%d in ('reg_query_command_str') do set path_value=%%d %%e

1)"tokens=3,*"部分。如果待查询的值名称含有n个空格,则需3需替换为3+n,比如:查公共用户开始菜单路径时,值名称common start menu含有2个空格,则"tokens=5,*";

2)path_value=%%d %%e部分。需要把*提取到的部分(即%%e)拼接上,如果不拼接,遇到含有空格的路径,则第一个空格后的部分将丢失。比如:查询公共用户开始菜单路径时,路径为%userprofile%\appdata\roaming\microsoft\windows\start menu,如果不拼接上%%e,则获取到的路径为%userprofile%\appdata\roaming\microsoft\windows\start,这样就有问题了;

补充说明:

1)如果有需要去除提取的路径末尾的一个空格,可以参考如下方式:

set desk_path_ex=%desk_path_ex:~0,-1%

2)将扩展路径转为直接路径,可参考如下方式:

for /f "tokens=*" %%d in ('echo %desk_path_ex%') do set dpx_path=%%d

获取任务栏路径的方式,只能通过方案1,因为任务栏路径未注册到注册表。

到此这篇关于windows bat获取开始菜单路径和桌面路径的实现的文章就介绍到这了,更多相关bat获取路径内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com