找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 32617|回复: 8

[系统相关] [已解决]批处理for语句中使用wmic命令的奇怪问题

[复制链接]
发表于 2015-6-16 17:28:49 | 显示全部楼层 |阅读模式
  1. WMIC LOGICALDISK get DESCRIPTION,DEVICEID,FILESYSTEM,SIZE,FREESPACE
复制代码
这条命令直接在cmd中执行结果如下:
Description   DeviceID  FileSystem  FreeSpace     Size
本地固定磁盘  C:        NTFS        261877760     39419113472
本地固定磁盘  D:        NTFS        180770955264  229895041024
本地固定磁盘  E:        NTFS        137467572224  230686715904

但是,放到批处理的for语句里:
  1. @echo off
  2. for /f "delims= " %%a in ('WMIC LOGICALDISK get DESCRIPTION,DEVICEID,FILESYSTEM,SIZE,FREESPACE') do echo %%a
  3. pause
复制代码
执行后,就会报错:GET表达式无效

请教大家,是什么原因,怎么解决?谢谢!

评分

参与人数 1PB +2 收起 理由
Batcher + 2 感谢给帖子标题标注[已解决]字样

查看全部评分

发表于 2015-6-16 18:43:57 | 显示全部楼层
逗号要转义
 楼主| 发表于 2015-6-16 19:24:31 | 显示全部楼层
明白啦,谢谢
发表于 2023-9-2 00:06:25 | 显示全部楼层
本帖最后由 tiandyoin 于 2023-9-2 00:07 编辑

回复 2# CrLf
  1.         rem Windows 规定文件名违法字符:      【*\/|:"<>?        】
  2.         rem ;1,%~2%,%~,!%~3!,!%%~4!,!!%%5!!^^^,)(.)(,& ;set ==!@#$%^&_+{}`-=[];',..txt
  3.         rem ;1,%~2%,%~,!%~3!,!%%~4!,!!%%5!!^^^,)(.)(,& ;set ==!@#$%^&_+{}`-=[];',..txt.lnk

  4.         call set "filepath=%%%~1%%"
  5.         echo "lnkpath2=%filepath%"
  6.         set  "filepath=%filepath:\=\\%"
  7.         echo "lnkpath3=%filepath%"
  8.         REM set  "filepath=%filepath:,=,%"
  9.         REM echo "lnkpath4=%filepath%"
  10.         REM set  "filepath=%filepath:`=``%"
  11.         REM echo "lnkpath5=%filepath%"
  12.         REM set  "filepath=%filepath:;=^;%"
  13.         REM echo "lnkpath6=%filepath%"
  14.         REM set  "filepath=%filepath:'=\'%"
  15.         REM echo "lnkpath7=%filepath%"

  16.         wmic path win32_shortcutfile where "name='%filepath%'" get target
复制代码
怎么转,转了十圈网上没找到答案。
发表于 2023-9-2 02:47:32 | 显示全部楼层
本帖最后由 Five66 于 2023-9-2 03:56 编辑

回复 4# tiandyoin
额,好像是双引号里面的才需要多层转义,已编辑

  1. set "aaa=%~dp0%~1%"
  2. set filepath=%aaa:\=\\%
  3. wmic path win32_shortcutfile where name='%filepath%' get target
  4. pause
复制代码
发表于 2023-9-2 18:18:25 | 显示全部楼层
回复 2# CrLf
  1. @echo OFF
  2. call :main_20230214_010127 %*
  3. REM echo.&pause
  4. @goto :EOF

  5. @rem https://learn.microsoft.com/zh-cn/windows/win32/wmisdk/like-operator

  6. :main_20230214_010127
  7.         @echo off
  8.         set "filepath=%~f1"
  9.         if not exist "%filepath%" set/p "filepath=filepath="
  10.         REM 脱去环境变量 filepath 左右两侧的双引号。
  11.         set filepath| findstr /i /r "^filepath=".*"$" > nul && set "filepath=%filepath:~1,-1%"
  12.         if not exist "%filepath%" exit /b 1
  13.         echo "lnkpath1=%filepath%"
  14.         call :get_lnk_target3 filepath returnVal
  15.         echo.
  16.         echo.dir "%returnVal%"
  17.         dir "%returnVal%"
  18. @goto :eof

  19. @rem Usage:
  20. rem                获取快捷方式的目标路径。
  21. :get_lnk_target3 {<@in &file=*.lnk> ,[@out target]}
  22.         @echo off||code by tiandyoin&title ^Call :get_lnk_target4 {^<@in ^&file=*.lnk^> ,[@out target]}
  23.         rem Windows 规定文件名违法字符:      【*\/|:"<>?        】
  24.         rem ;1,%~2%,%~,!%~3!,!%%~4!,!!%%5!!^^^,)(.()(,& ;set ==!@#$%^&_+{}`-=[];',..txt
  25.         rem ;1,%~2%,%~,!%~3!,!%%~4!,!!%%5!!^^^,)(.()(,& ;set ==!@#$%^&_+{}`-=[];',..txt.lnk

  26.         call set "filepath=%%%~1%%"
  27.         echo "lnkpath2=%filepath%"
  28.         set  "filepath=%filepath:\=\\%"
  29.         echo "lnkpath3=%filepath%"
  30.         set  "filepath=%filepath:'=\'%"
  31.         echo "lnkpath4=%filepath%"
  32.         set  "filepath=%filepath:[=[[]%"
  33.         echo "lnkpath5=%filepath%"
  34.         set  "filepath=%filepath:_=[_]%"
  35.         echo "lnkpath6=%filepath%"
  36.         rem 似乎没法转义逗号??只能使用 Like 通配符 _(下划线)。
  37.         setlocal enabledelayedexpansion
  38.                 set "filepath=!filepath:,=_!"
  39.                 echo "lnkpath7=!filepath!"
  40.                 set "filepath=!filepath:%%=[%%]!"
  41.         endlocal & set "filepath=%filepath%"
  42.         echo "lnkpath8=%filepath%"

  43.         set target=
  44.         for /f "skip=1 delims=" %%a in (
  45.                 'wmic path win32_shortcutfile where "name like '%filepath%'" get target'
  46.         ) do (
  47.                 set "target=%%~a"
  48.                 goto :BREAK
  49.         )
  50.         :BREAK
  51.         set "target=%target:~0,-2%"
  52.         if "%~2" neq "" (set "%~2=%target%") else echo "target=%target%"
  53. @goto :eof
复制代码
找遍全网也没有找到转义逗号的方法,只能使用 like+通配符的方法搜索。
发表于 2023-9-2 18:50:20 | 显示全部楼层
回复 6# tiandyoin


    首先要知道为啥要转义逗号,因为它被当作谓词分隔符了,你这种情况可以用括号括起来:
  1. for /f "delims=" %%i in ('"wmic path win32_shortcutfile where (name='C:\\ProgramData\\应用,商店.lnk') get target /value|findstr ."') do echo,%%i
复制代码

评分

参与人数 1技术 +1 收起 理由
Batcher + 1 乐于助人

查看全部评分

发表于 2023-9-19 22:41:45 | 显示全部楼层
本帖最后由 tiandyoin 于 2023-9-19 22:51 编辑

回复 7# buyiyang


    那么如何转义百分号呢?比如我的路径是: "c:\Users\test\Desktop\;1,2,3A'%temp%.lnk"

不想让TEMP求值,
知道了,前面加 ^

  1.         setlocal enabledelayedexpansion
  2.                 set "filepath=!filepath:%%=^%%!"
  3.         endlocal & set "filepath=%filepath%"
  4. @        echo "lnkpath6=%filepath%"
复制代码
但又如何转义 ( 和 ) 两个括号呢?
比如我的路径是: "c:\Users\test\Desktop\;1,2,3A'%temp(%)().lnk"
发表于 2023-9-19 22:58:42 | 显示全部楼层
rem 手动创建 .lnk 系统可能会转义,要重新检查,改名为以下内容。
        rem Windows 规定文件名违法字符:      【*\/|:"<>?        】
        :: filename=;%1,%~2%,%~,if !%~3!==!%%~4!,!!%temp%!!%my%,^^^,)(.()(,& ;set a=!@#$%^&_+{}`-`=[];'',..txt
        :: filename=;%1,%~2%,%~,if !%~3!==!%%~4!,!!%temp%!!%my%,^^^,)(.()(,& ;set a=!@#$%^&_+{}`-`=[];'',..txt.lnk


这是终极挑战
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-18 13:10 , Processed in 0.010938 second(s), 9 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表