如何将文件内容读入批处理文件中的变量? [英] How to read file contents into a variable in a batch file?

查看:37
本文介绍了如何将文件内容读入批处理文件中的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此批处理文件将构建从 TEST 发布到 LIVE.我想在此文件中添加一个检查约束,以确保特定文件夹中有随附的发布文档.

This batch file releases a build from TEST to LIVE. I want to add a check constraint in this file that ensures there is an accomanying release document in a specific folder.

"C:Program FilesWindows Resource KitsTools
obocopy.exe" "\testserver	estapp$"        
"\liveserverliveapp$" *.* /E /XA:H /PURGE /XO /XD ".svn" /NDL /NC /NS /NP
del "\liveserverliveapp$web.config"
ren "\liveserverliveapp$web.live.config" web.config

所以我有几个关于如何实现这一目标的问题......

So I have a couple of questions about how to achieve this...

  1. \testserver estapp$ 文件夹中有一个 version.txt 文件,该文件的唯一内容是内部版本号(对于例如,45 - 用于构建 45)如何将 version.txt 文件的内容读入批处理文件中的变量?

  1. There is a version.txt file in the \testserver estapp$ folder, and the only contents of this file is the build number (for example, 45 - for build 45) How do I read the contents of the version.txt file into a variable in the batch file?

如何使用第 1 部分中的变量代替 {build} 检查文件 \fileservermyapp eleasedocs {build}.doc 是否存在?

How do I check if a file ,\fileservermyapp eleasedocs {build}.doc, exists using the variable from part 1 in place of {build}?

推荐答案

将文件内容读入变量:

for /f "delims=" %%x in (version.txt) do set Build=%%x

set /p Build=<version.txt

两者都将在文件中只有一行时执行相同的操作,对于更多行,for 变体会将 最后 行放入变量中,而 set/p 将使用 first.

Both will act the same with only a single line in the file, for more lines the for variant will put the last line into the variable, while set /p will use the first.

使用变量——就像任何其他环境变量一样——毕竟它是一个:

Using the variable – just like any other environment variable – it is one, after all:

%Build%

所以要检查是否存在:

if exist \fileservermyapp
eleasedocs\%Build%.doc ...

尽管那里很可能不允许使用 UNC 路径.目前无法对此进行测试,但请记住这一点.

Although it may well be that no UNC paths are allowed there. Can't test this right now but keep this in mind.

这篇关于如何将文件内容读入批处理文件中的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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