是什么在一个cmd文件%和%%之间的区别? [英] What is the difference between % and %% in a cmd file?

查看:259
本文介绍了是什么在一个cmd文件%和%%之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近包括在.cmd文件类似这样的一行:

I recently included a line similar to this in a .cmd file:

for /f %%f in ('dir /b .\directory\*.sql') DO sqlcmd -b -o ".\directory\output\%%f.txt" -i ".\directory\%%f"

我原本只是用来%F,并在命令行上运行,而不是在通过文件来看,它做工精细。当我切换到%%楼它的工作的文件中。只是想知道有什么区别。

Originally I had only used %f, and it would work fine when run on the command line, but not when run through the file. When I switched to %%f, it worked in the file. Just wondering what the difference is.

推荐答案

(更详细的说明,可在的此Microsoft知识库。)

三件事知道的:


  1. 百分号在批处理文件用于重新present命令行参数:%1 %2 ...

  2. 在他们之间的任何字符两个百分号PTED都作为变量间$ P $:

  1. The percent sign is used in batch files to represent command line parameters: %1, %2, ...
  2. Two percent signs with any characters in between them are interpreted as a variable:

回声%MYVAR%

为什么?

例如,如果我们执行你(简体)命令行

For example, if we execute your (simplified) command line

FOR /f %f in ('dir /b .') DO somecommand %f

在一个批处理文件,的规则2 的会尽力间preT

in a batch file, rule 2 would try to interpret

%f in ('dir /b .') DO somecommand %

作为一个变量。为了prevent的是,你必须申请的规则3 的并与第二个逃避

as a variable. In order to prevent that, you have to apply rule 3 and escape the % with an second %:

FOR /f %%f in ('dir /b .') DO somecommand %%f

这篇关于是什么在一个cmd文件%和%%之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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