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

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

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

返回列表