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

[文件操作] for中用SET设置变量后不生效,第2次执行才生效

@echo off

set "wmpfile=C:\Program Files\Windows Media Player\wmplayer.exe"
if not exist "%wmpfile%" (
echo 未安装Windows Media Player 11...
)
if exist "%wmpfile%" (
for /f "tokens=5" %%i in ('filever.exe  "%wmpfile%"') do SET wmpver=%%i
echo 已安装Windows Media Player "%wmpver%"
)

以上代码存为f.bat,在cmd窗口中,第一次执行f.bat,输出已安装Windows Media Player ""
第一次执行f.bat,输出已安装Windows Media Player "10.0.0.3802"
请问怎么解决呢?

回复 1# keetian
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set "wmpfile=C:\Program Files\Windows Media Player\wmplayer.exe"
  4. if not exist "%wmpfile%" (
  5. echo 未安装Windows Media Player ...
  6. ) else (
  7. for /f "tokens=5" %%i in ('filever.exe "%wmpfile%"') do SET wmpver=%%i
  8. echo 已安装Windows Media Player !wmpver!
  9. )
  10. pause
复制代码

TOP

  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set "wmpfile=C:\Program Files\Windows Media Player\wmplayer.exe"
  4. if not exist "%wmpfile%" (
  5.     echo 未安装Windows Media Player ...
  6. ) else (
  7.     for /f "tokens=5" %%i in ('filever.exe "%wmpfile%"') do (
  8.         set "wmpver=%%i"
  9.     )
  10.     echo 已安装Windows Media Player !wmpver!
  11. )
  12. pause
复制代码

TOP

返回列表