Board logo

标题: [文件操作] for中用SET设置变量后不生效,第2次执行才生效 [打印本页]

作者: keetian    时间: 2015-2-7 12:24     标题: 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"
请问怎么解决呢?
作者: pcl_test    时间: 2015-2-7 12:41

回复 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
复制代码

作者: DAIC    时间: 2015-2-7 14:23

  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
复制代码





欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2