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

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

问题描述

该批处理文件从测试到LIVE释放构建。我想在这个文件中,确保有一个SPE cific文件夹中的accomanying发布文档添加一个约束检查。

 C:\\ Program Files文件\\ Windows资源工具包\\工具\\ robocopy.exe\\\\ TESTSERVER \\ testapp $
\\\\ liveserver \\ $ liveapp* * / E / XA:H / PURGE / XO / XD.svn文件/ NDL / NC / NS / NP
德尔\\\\ liveserver \\ liveapp $ \\ web.config中
仁\\\\ liveserver \\ liveapp $ \\ web.live.config的web.config

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


  1. 有在 \\\\ TESTSERVER \\ testapp $ 文件夹 version.txt 文件,该文件的唯一内容是版本号(例如,45 - 为构建45)
    我如何读的内容 version.txt 文件导入到批处理文件变量?


  2. 我如何检查文件, \\\\文件服务器\\ MyApp的\\ releasedocs \\ {}构建.DOC ,存在的地方使用变量从第1部分{中}建立?



解决方案

读取文件内容到一个变量:

  FOR / Fdelims =%%倍In(version.txt)并设置编译= %%点¯x

 设置/对构建= LT; version.txt

双方将采取同样只在文件中单行线,更多的线变种就会把的最后的行成变量,而设置/ p 将使用的第一。

使用变量 - 就像任何其他环境变量 - 这是其中之一,毕竟:

 %构建%

因此​​,要检查是否存在:

 是否存在\\\\文件服务器\\ MyApp的\\ releasedocs \\%建立.DOC%...

虽然这可能是没有UNC路径被允许存在。现在不能测试此权利,但请记住这一点。

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 spe cific folder.

"C:\Program Files\Windows Resource Kits\Tools\robocopy.exe" "\\testserver\testapp$"        
"\\liveserver\liveapp$" *.* /E /XA:H /PURGE /XO /XD ".svn" /NDL /NC /NS /NP
del "\\liveserver\liveapp$\web.config"
ren "\\liveserver\liveapp$\web.live.config" web.config

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

  1. There is a version.txt file in the \\testserver\testapp$ 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?

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

解决方案

Read file contents into a variable:

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

or

set /p Build=<version.txt

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%

So to check for existence:

if exist \\fileserver\myapp\releasedocs\%Build%.doc ...

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天全站免登陆