BAT获取开始菜单路径、桌面路径、任务栏路径等
Windows BAT获取开始菜单路径和桌面路径的实现
有时候在bat批处理中需要向 开始菜单、 桌面、 任务栏添加文件夹/文件/快捷方式等,这就需要先获取到对应的路径。
这篇文章将总结如何在bat批处理中获取 开始菜单、 桌面、 任务栏的绝对路径。
一、解决方案
方案1:通过环境变量获取
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
@ 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:通过注册表获取
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
@ 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项下的路径都是扩展路径(含变量);
关键代码解读:
通用格式如下:
1
|
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)如果有需要去除提取的路径末尾的一个空格,可以参考如下方式:
1
|
set desk_path_ex=%desk_path_ex:~0,-1% |
2)将扩展路径转为直接路径,可参考如下方式:
1
|
for /F "tokens=*" %%d in ( 'echo %desk_path_ex%' ) do set dpx_path=%%d |
获取任务栏路径的方式,只能通过方案1,因为任务栏路径未注册到注册表。
到此这篇关于Windows BAT获取开始菜单路径和桌面路径的实现的文章就介绍到这了,更多相关BAT获取路径内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
关注我】。(●'◡'●)
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的【因为,我的写作热情也离不开您的肯定与支持,感谢您的阅读,我是【Jack_孟】!
本文来自博客园,作者:jack_Meng,转载请注明原文链接:https://www.cnblogs.com/mq0036/p/17817805.html
【免责声明】本文来自源于网络,如涉及版权或侵权问题,请及时联系我们,我们将第一时间删除或更改!