将文件的第一行设置为变量的最短 Windows 批处理文件代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5023545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:04:43  来源:igfitidea点击:

Shortest Windows batch file code to get first line of a file set to a variable?

windowsbatch-filecmd

提问by djangofan

I am looking for the shortest amount of code possible that will get the first line of a text file and set it to a variable in Windows.

我正在寻找尽可能短的代码,以获取文本文件的第一行并将其设置为 Windows 中的变量。

There are lots of examples of this kind of thing in other threads on this StackExchange site but I am notlooking for how to loop through and display all lines in a file. What I want is to just simplyget the first line.

在这个 StackExchange 站点上的其他线程中有很多这种事情的例子,但我不是在寻找如何循环并显示文件中的所有行。我想要的只是简单地获得第一行。

回答by Joe

set /p var= <Text.txt
echo %var%

Referenced from: http://forums.techguy.org/software-development/551867-batch-file-read-line-text.htmlIronically the poster is able to get the first line but wanted to loop through the whole file!

引用自:http: //forums.techguy.org/software-development/551867-batch-file-read-line-text.html具有讽刺意味的是,海报能够获得第一行,但想要遍历整个文件!

回答by nephi12

this is just a slightly shorter version:

这只是一个略短的版本:

set/pz=<file
echo %z%

回答by user3110750

I couldn't get this to work under Windows 7; the variable was simply not set. But here's a slightly longer solution:

我无法让它在 Windows 7 下工作;变量根本没有设置。但这里有一个稍长的解决方案:

for /F "delims=" %%i in (Text.txt) do (  
  set Z=%%i
  goto BREAK1
)
:BREAK1
echo %Z%

回答by phuclv

If delayed expansionhas been enabled before (Setlocal EnableDelayedExpansionor cmd started with /V:ONoption) then there's a shorter (one-liner) version

如果之前已启用延迟扩展Setlocal EnableDelayedExpansion或 cmd 以/V:ON选项开头),则有一个较短的(单行)版本

set /pz=<filename.txt&echo !z!

回答by Ganesh Kamath - 'Code Frenzy'

I had to change the line of file for a text file inside Program Files(x86)directory. After trying a lot of solutions, the following solution worked for me:

我不得不更改Program Files(x86)目录内文本文件的文件行。在尝试了很多解决方案后,以下解决方案对我有用:

Steps:

脚步:

  1. Use typecommand piped with findstrto get a file with the desired data
  2. Delete the original file
  3. Use typecommand to move the content back to the file with the original file name
  4. Delete the new file
  1. 使用type管道命令findstr获取包含所需数据的文件
  2. 删除原始文件
  3. 使用type命令将内容移回原文件名的文件
  4. 删除新文件

Example:

例子:

@echo off
set originalFile="C:\Program Files (x86)\<Target File Path>\<Target File Name>"
set newFile="C:\Program Files (x86)\<Target File Path>\<Target newFile Name>"
if exist %originalFile% (
    type %originalFile% | findstr /v T4VSHost >> %newFile%
    del %originalFile% /F /Q
    type %newFile% >> %originalFile%
    del %newFile% /F /Q
)
@echo on

Note:

笔记:

When I tried writing the change back to the original file in step 1, the result was a an empty file.

当我在步骤 1 中尝试将更改写回原始文件时,结果是一个空文件。

Note the string I am matching is T4VSHost, which occurs in the file at path C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes\EF6.Utility.CS.ttinclude

请注意我匹配的字符串是T4VSHost,它出现在路径的文件中C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes\EF6.Utility.CS.ttinclude

回答by S Techy Tse

just FYI...

仅供参考...

I tried it and didn't work. But I realized finally that I had to reproduce the text file (originally the text file is generated by a powershell script) so when I did this and it works.

我试过了,没有用。但我终于意识到我必须重现文本文件(最初文本文件是由 powershell 脚本生成的),所以当我这样做时,它就可以工作了。

:: output to different file
Type Text.txt > text2.txt

Then apply the same code (change text.txtto text2.txt)

然后应用相同的代码(更改text.txttext2.txt

set /p var= <Text2.txt
echo %var%

If I use the original one, I would either get garbage in %var%, not sure if the text file has not <CR>or what. Anyway, by reproducing the text file, it seems working fine.

如果我使用原始文件,我要么会得到垃圾%var%,不确定文本文件是否没有<CR>或什么。无论如何,通过重现文本文件,它似乎工作正常。