如何"对于"在cmd中批处理文件的工作? [英] How does "FOR" work in cmd batch file?

查看:193
本文介绍了如何"对于"在cmd中批处理文件的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在编程的几十个20年的语言,但我永远无法理解如何FOR在windows CMD工作外壳的批处理文件,不管如何努力我试过了。我读

I've been programming in dozens of languages for 20 years but I could never understand how "FOR" work in windows cmd shell batch file, no matter how hard I tried. I read

<一个href=\"http://rads.stackoverflow.com/amzn/click/0470046163\">http://www.amazon.com/Windows-Administration-Command-Line-Vista/dp/0470046163/ref=sr_1_1?ie=UTF8&s=books&qid=1241362727&sr=8-1

http://www.ss64.com/nt/for.html

和互联网上的其他一些文章,但仍然迷惑,不能完成任何事情。

and several other articles on the internet but still confuse and cannot accomplish anything.

任何人可以给我一个简洁的解释如何FOR一般的作品?

Can anybody give me a concise explaination on how "FOR" works in general ?

有关更具体一点问题,我怎样才能通过%PATH%变量每个路径遍历?
我试着

For a little more specific question, how can I iterate through each path in %PATH% variable ? I've tried with

rem showpathenv.bat
for /f "delims=;" %%g in ("%PATH%") do echo %%g

这会只显示第一个路径,而不是全部。为什么呢?我做了什么?

That'd show only the first path, not all of them. Why ? What I do wrong ?

推荐答案

问题的答案没有实际工作。我设法找到解决自己。
这是一个有点hackish,但它解决的问题对我来说:

None of the answers actually work. I've managed to find the solution myself. This is a bit hackish, but it solve the problem for me:

echo off
setlocal enableextensions
setlocal enabledelayedexpansion
set MAX_TRIES=100
set P=%PATH%
for /L %%a in (1, 1, %MAX_TRIES%) do (
  for /F "delims=;" %%g in ("!P!") do (
    echo %%g
    set P=!P:%%g;=!
    if "!P!" == "%%g" goto :eof
  )
)

哦!我最讨厌的批处理文件编程!!

Oh ! I hate batch file programming !!

更新

标记的解决方案是简单,但它不会与含有空白路径工作。这是马克的解决方案的一个小修改版

Mark's solution is simpler but it won't work with path containing whitespace. This is a little-modified version of Mark's solution

echo off
setlocal enabledelayedexpansion
set NonBlankPath=%PATH: =#%
set TabbedPath=%NonBlankPath:;= %
for %%g in (%TabbedPath%) do (
  set GG=%%g
  echo !GG:#= !
)

这篇关于如何&QUOT;对于&QUOT;在cmd中批处理文件的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆