[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

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

  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表达式无效

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

评分人数

    • Batcher: 感谢给帖子标题标注[已解决]字样PB + 2

逗号要转义

TOP

明白啦,谢谢

TOP

本帖最后由 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
复制代码
怎么转,转了十圈网上没找到答案。

TOP

本帖最后由 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
复制代码

TOP

回复 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+通配符的方法搜索。

TOP

回复 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

评分人数

TOP

本帖最后由 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"

TOP

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


这是终极挑战

TOP

返回列表