简体   繁体   English

如何在批处理或Powershell中找到文件第一行的长度

[英]How can I find the length of the first line of a file in batch or powershell

I have to read each file in a folder and determine the length of the first line of each, then do something depending on whether or not that length is what is should be in a table. 我必须读取文件夹中的每个文件并确定每个文件第一行的长度,然后根据该长度是否应等于表中的长度来进行操作。 I can loop through each file in batch and have %%f as the file, but how do I get that length and assign it to a variable? 我可以批量遍历每个文件并将%% f作为文件,但是如何获取该长度并将其分配给变量?

If there is a way to do this in Powershell using a batch file, that would help, but I would need to know how to call the Powershell from the batch file also. 如果可以使用批处理文件在Powershell中执行此操作,那将有所帮助,但我还需要知道如何从批处理文件中调用Powershell。

The simple PowerShell code would look something like this: 简单的PowerShell代码如下所示:

param($path)
Get-ChildItem $path -File | 
    Select FullName,@{Label="1stLineLength";Expression={(Get-Content $_.FullName -First 1).Length}}

So the first argument will be taken as the path of the script. 因此,第一个参数将用作脚本的路径。 Then to call it from batch I borrow the answer to this SO question . 然后从批处理中调用它,我借用了这个SO问题的答案。

Powershell.exe -executionpolicy remotesigned -File m:\Scripts\firstlinelength.ps1 "C:\temp"

That will get output like this on console. 这样会在控制台上获得类似的输出。

FullName                              1stLineLength
--------                              -------------
C:\Users\mcameron\CleansedBigFile.txt             4

This code assumes that you have at least PowerShell v3.0 for the -First and -File parameter and switch. 该代码假定您至少具有-First-File参数和开关的PowerShell v3.0。 I would like to think that most batch code can be converted easily to a PowerShell equivalent so if your environment allows you consider converting to the powerful PowerShell. 我想可以将大多数批处理代码轻松转换为等效的PowerShell,因此,如果您的环境允许您考虑转换为功能强大的PowerShell。

Some of your question is pretty vague (what table?). 您的某些问题非常模糊(在哪张桌子上?)。 But in general, you don't need a batch file at all. 但是通常,您根本不需要批处理文件。 PowerShell example: PowerShell示例:

Get-ChildItem -File | ForEach-Object {
  $firstLineLength = (Get-Content $_ | Select-Object -First 1).Length
  if ( $firstLineLength -gt $whateverFromTable ) {
    ...
  }
}

Note that the -File parameter of Get-ChildItem doesn't exist before PowerShell v3. 请注意,PowerShell v3之前不存在Get-ChildItem-File参数。 For PowerShell v2 and below, you would replace Get-ChildItem -File with Get-ChildItem | Where-Object { -not $_.PSIsContainer } 对于PowerShell v2及以下版本, Get-ChildItem -File替换为Get-ChildItem | Where-Object { -not $_.PSIsContainer } Get-ChildItem | Where-Object { -not $_.PSIsContainer } . Get-ChildItem | Where-Object { -not $_.PSIsContainer }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从批处理文件启动Powershell命令行并强制其保持打开状态? - How I can I start a powershell command line from a batch file and enforce it to stay open? 如何将多行批处理文件语句转换为 Powershell? - How can I convert a multi-line batch file statement into Powershell? 如何使用Powershell查找文件大小 - How can i find file sizes with powershell 批处理/ Powershell为文件中的每一行替换字符串中的前2个字符 - Batch / Powershell replace first 2 characters from string for each line in file 如何在批处理文件中运行此 powershell 脚本 - How can I run this powershell script in a batch file 如何以管理员身份和批处理文件中的参数运行PowerShell? - How can I run PowerShell as an administrator AND with arguments from a batch file? 如何使用 powershell 命令用新行替换字符串? 从批处理文件中? - 双引号问题 - How can I make use a powershell command to replace a string witha new line? From within a batch file? -double quotes issue 如何从批处理文件中调用powershell脚本并将多个值返回到批处理文件? - How can I call a powershell script from a batch file and return multiple values to the batch file? 如何搜索文本文件中的第一行和最后一行? - How can I search the first line and the last line in a text file? 如何使用Powershell导出csv文件的每一行 - How can I export each line of a csv file with powershell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM