bat文件:使用的如果,和可变一起 [英] bat file: Use of if, for and a variable all together

查看:152
本文介绍了bat文件:使用的如果,和可变一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里所说:

我想,如果它存在于文本添加到文件中。该文本是去对文件的每一行的最后。要添加的文字是注意:一定条件下适用

I want to add text to a file if it exists. The text is to go on each line of the file at the very end. The text to be added is "Note: certain conditions apply"

我发现语法检查文件是否存在,以及语法for循环在该行的末尾添加文字,但他们似乎并不在该bat文件同时工作。

I found syntax to check if the file exists, and syntax for a for loop to add text at the end of the line, but they do not seem to work at the same time in the bat file.

此外,咨询有关变量名称各不相同,使用%,用引号本身,
所以在这里我有什么(假设一切都发生在同一个目录下)......

Also, advice varies about variable names, using "%" and using the quote marks themselves, so here is what I have (assume everything takes place in the same directory)....

@echo off
setLocal EnableDelayedExpansion

PAUSE 

REM File to be modified -- 1
SET FileToModify1=abcd.txt
SET SaveFile1=abcd1.txt

PAUSE 

IF EXIST "%FileToModify1%"  (
echo Yes
)

IF EXIST "%FileToModify1%"  (
for /f "tokens=* delims= " %%a in "%FileToModify1%" do (
echo %%a   Note:  certain conditions apply  >> "%SaveFile1%"
)
)

PAUSE 

有没有人对这里做什么建议?
此外,它更好有大约abcd.txt报价与否?
为什么所有的神秘的%左右变量?

Does anyone have a suggestion on what to do here? Also, is it better to have quotes around abcd.txt or not? Why all the mysterious "%" around variables?

请还通过文件中看到循环保存到变量一个更加有效的解决方案相同的问题....

Please also see loop through file saving to variable for a much more efficient solution to the same problem....

推荐答案

您需要这个(工作):

for /f "tokens=* delims= " %%a in (%FileToModify1%) do (

如果您有(不工作,错误的语法):

Where you have (doesn't work, wrong syntax):

for /f "tokens=* delims= " %%a in "%FileToModify1%" do (

从联机帮助(通过为/?

对于包含空格的文件名,你需要引用的文件名与
  双引号。为了也用双引号以这种方式,则
  需要使用usebackq选项,否则双引号将
  除preTED为定义要分析的字符串。

For file names that contain spaces, you need to quote the filenames with double quotes. In order to use double quotes in this manner, you also need to use the usebackq option, otherwise the double quotes will be interpreted as defining a literal string to parse.

此外,岂不是更好有大约abcd.txt报价与否?

Also, is it better to have quotes around abcd.txt or not?

如果您的文件名中有空格它是必需的,否则就optional.Your文件名没有空间,所以我使用了更简单的方法,没有引号。

It's required if your file name has spaces, otherwise it's optional.Your file name doesn't have spaces so I used the simpler approach, no quotes.

为什么所有的神秘的%左右变量?

Why all the mysterious "%" around variables?

那么它并不一定是神秘。这里有一个例子,在命令行:

Well it doesn't have to be "mysterious". Here's an example, from the command line:

> set fname=MyFile.txt
> echo %fname%

MyFile.txt

在%的变量名称周围被shell看到并触发变量扩展。要查看联机帮助,键入设置/?,你会得到更多的细节比你能忍受。

The %'s around the variable name are seen by the shell and trigger the variable expansion. To see the online help, type set /?, and you'll get more details than you can stand.

这篇关于bat文件:使用的如果,和可变一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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