批处理 - 阅读文件的内容在一个数组 [英] Batch - Read contents of a file in an array

查看:351
本文介绍了批处理 - 阅读文件的内容在一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和两行(说param.txt),这是如下所示的文本文件:

I've a text file with two rows (say param.txt) which is shown below:

Mar2012
dim1,dim2,dim3,dim4

我想读在批处理文件和第一行的内容存储在一个变量称为cube_name。当我在读第二行,我要拆分的逗号分隔的字符串DIM1,DIM2,为dim3,dim4和创建四个元素的数组。我计划使用变量,并在脚本的后半部分阵列。

I want to read this file in batch and store the contents of first line in a variable called cube_name. When I'm reading the second line, I want to split the comma delimited string dim1,dim2,dim3,dim4 and create an array of four elements. I am planning to use the variable and the array in later part of the script.

这是我创建的code如下所示。如预期的code不工作。

The code which I created is shown below. The code is not working as expected.

@echo off & setlocal enableextensions enabledelayedexpansion
set /a count_=0
for /f "tokens=*" %%a in ('type param.txt') do (
set /a count_+=1
set my_arr[!count_!]=%%a
)
set /a count=0
for %%i in (%my_arr%) do (
set /a count+=1
if !count! EQU 1 (
    set cube_name=%%i
)

if !count! GTR 1 (
    set dim_arr=%%i:@=,%
)
)
for %%i in (%dim_arr%) do (
echo %%i
)

echo !cube_name!

我能看到下面当我运行code:

I get to see the following when I run the code:

C:\Working folder>test2.bat
ECHO is off.

因此​​,这似乎并没有工作,我想不出什么我做错了。我是相当新的批处理脚本,以便帮助pciated AP $ P $

So this doesn't appear to work and I can't figure out what I'm doing wrong. I am fairly new to the batch scripting so help is appreciated

推荐答案

您第一个for循环正常。这不是我会怎么做,但它的作品。之后,一切是一个烂摊子。它看起来像您认为阵列是在批处理正式的概念,当他们没有。有可能在看起来让人联想到阵列的方式使用变量工作。但真正的阵列不批中存在。

Your first FOR loop is OK. It is not how I would do it, but it works. Everything after that is a mess. It looks like you think arrays are a formal concept in batch, when they are not. It is possible to work with variables in a way that looks reminiscent of arrays. But true arrays do not exist within batch.

您使用%my_arr%,如果它是一个数组,但 my_arr 甚至没有定义。您已经定义的变量 my_arr [1] AMD my_arr [2] - 括号和数量是变量名称的一部分

You use %my_arr% as if it is an array, but my_arr is not even defined. You have defined variables my_arr[1] amd my_arr[2] - the brackets and number are part of the variable name.

它也像你for循环的误解。我建议你​​仔细阅读有关文档(在命令行键入 HELP)。另外,也要看看就这个问题和其他网站的例子。 for命令是非常复杂的,因为它有类似于未经训练的眼睛,尚未有完全不同的行为,许多变化。一个优秀的资源来帮助你的理解是 http://judago.webs.com/batchforloops.htm

It also looks like you have a misunderstanding of FOR loops. I suggest you carefully read the FOR documentation (type HELP FOR from a command line). Also look at examples on this and other sites. The FOR command is very complicated because it has many variations that look similar to the untrained eye, yet have profoundly different behaviors. One excellent resource to help your understanding is http://judago.webs.com/batchforloops.htm

假设文件始终正好有2行,我会解决你的问题,像这样

Assuming the file always has exactly 2 lines, I would solve your problem like so

@echo off
setlocal enableDelayedExpansion
set dimCnt=0
<param.txt (
  set /p "cube_name=" >nul
  set /p "dimList=" >nul
  for %%D in (!dimList!) do (
    set /a dimCnt+=1
    set "dim[!dimCnt!]=%%D"
  )
)
echo cube_name=!cube_name!
  for /l %%I in (1 1 !dimCnt!) do echo dim[%%I]=!dim[%%I]!

以上解决方案的一个很好的特性是它允许不同数量在2号线尺寸的名单条款。如果有制表符,空格,分号,平等, * 维度名称将失败。有比较简单的方法来解决这个限制,如果需要的话。

One nice feature of the above solution is it allows for a varying number of terms in the list of dimensions in the 2nd line. It will fail if there are tabs, spaces, semicolon, equal, * or ? in the dimension names. There are relatively simple ways to get around this limitation if need be.

制表符,空格,分号和平等可以通过使用搜索和替换用引号括每学期处理。

Tabs, spaces, semicolon and equal can be handled by using search and replace to enclose each term in quotes.

  for %%D in ("!dimList:,=","!") do (
    set /a dimCnt+=1
    set "dim[!dimCnt!]=%%~D"
  )

我不会在这里发布完整的解决方案,因为它不太可能是必要的。但处理 * 和/或将需要一个新的行字符替换逗号,并切换到FOR / ˚F声明。

I won't post the full solution here since it is not likely to be needed. But handling * and/or ? would require replacing the commas with a new-line character and switching to a FOR /F statement.

这篇关于批处理 - 阅读文件的内容在一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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