Windowes批处理文件:查找包含指定单词的行

时间:2022-08-17 02:09:30

I have to run script that find find partition with more than 1 GB free space. I wrote the following:

我必须运行脚本,找到具有超过1 GB可用空间的查找分区。我写了以下内容:

@echo on
set isComplete = 0
set last = 0;

DISKPART /s .\script.txt > .\log.txt 

for /f "tokens=3" %%f in ('.\log.txt') do (
FIND /C /I "GB" .\log.txt > %isComplete%
IF  %isComplete% > 0
%last% = %%f
)

Dispart call to another script that prints to log.txt all the availble partitions. Log.txt equals to:

Disall调用另一个脚本,打印到log.txt所有可用的分区。 Log.txt等于:

Partition ### Type Size Offset

分区###类型大小偏移


Partition 1 Primary 74 GB 1024 KB

分区1主要74 GB 1024 KB

Partition 2 Primary 300 MB 74 GB

分区2主要300 MB 74 GB

now, i want to check which parttion has more than 1 GB free space, so I look foe "GB" word.

现在,我想检查哪个parttion有超过1 GB的可用空间,所以我看看敌人的“GB”字样。

but I always get: > "was unexpected at this time." error. can anyine help me to figure out what the problem? or suggest me a better way to do that?

但我总是得到:>“此时此刻意外。”错误。可以帮忙找出问题吗?还是建议我一个更好的方法呢?

thanks!!!

1 个解决方案

#1


0  

Ouch.

You've got a number of problems.

你有很多问题。

The output of LIST PARTITION gives the size and offset of each partition. I'm not very familiar with DISKPART, but I don't believe the size gives the amount of free space within the partition. It indicates how large the partition is, but gives no indication of how much space has already been allocated to existing files. The offset indicates the location of the partition. So it seems you are off on the wrong foot right from the get-go.

LIST PARTITION的输出给出了每个分区的大小和偏移量。我对DISKPART不太熟悉,但我不相信大小会给分区内的可用空间量。它表示分区有多大,但没有指示已经为现有文件分配了多少空间。偏移量表示分区的位置。所以看起来你从一开始就走错了路。

That being said, you have some major syntax problems in your script.

话虽这么说,你的脚本中有一些主要的语法问题。

It looks like you are trying to read a file in your FOR statement, but in reality your syntax is attempting to execute a command because you enclosed the file name in single quotes. You need to type HELP FOR from the command line and read carefully the description of the FOR command.

看起来您正在尝试在FOR语句中读取文件,但实际上您的语法正在尝试执行命令,因为您将文件名括在单引号中。您需要从命令行键入HELP FOR并仔细阅读FOR命令的说明。

You want either

你也想要

for /f "tokens=3" %%f in (log.txt) do (

or, if the filename contains spaces (which yours doesn't), then

或者,如果文件名包含空格(您的文件不包含空格),那么

for /f "usebackq tokens=3" %%f in ("file name with spaces.txt") do (

Your line FIND /C /I "GB" .\log.txt > %isComplete% is attempting to write the output of the FIND command to a file named "0". I doubt that is your intention. The FIND command will print out the complete text of any line that contains your search string of "GB". It will also set the ERRORLEVEL to 0 if it finds at least one matching line, or 1 if it fails to find a matching line. I'm not sure what you are attempting to do.

您的行FIND / C / I“GB”。\ log.txt>%isComplete%正在尝试将FIND命令的输出写入名为“0”的文件。我怀疑这是你的意图。 FIND命令将打印出包含搜索字符串“GB”的任何行的完整文本。如果找到至少一个匹配的行,它也会将ERRORLEVEL设置为0,如果找不到匹配的行,则将其设置为1。我不确定你要做什么。

Your IF statement is a mess. Here again you need to type HELP IF from the command line and read carefully how to use it. Your existing attempt has many errors

你的IF声明是一团糟。在这里,您需要从命令行键入HELP IF并仔细阅读如何使用它。您现有的尝试有很多错误

  • You can't extend an IF statement accross multiple lines unless you either use line continuation ^, or parentheses.

    除非使用行继续^或括号,否则不能扩展多行的IF语句。

  • You can't use > as a comparison operator, you need to use GTR

    您不能使用>作为比较运算符,您需要使用GTR

  • I can't figure out exactly what comparison you are trying to make.

    我无法弄清楚你想要做什么比较。

I don't know how to point you in the right direction because I don't understand what your overall mission is. Perhaps if you describe in more detail what you are trying to do, someone can provide a script that will accomplish the task, and you can learn from that example.

我不知道如何指出你正确的方向,因为我不明白你的整体使命是什么。也许如果你更详细地描述你想要做什么,有人可以提供一个完成任务的脚本,你可以从这个例子中学习。

DISKPART is a potentially dangerous tool. I don't see how you can do harm with the LIST command, but other commands could create problems if misused. While you are learning the rudiments of batch scripting, I suggest you avoid DISKPART. I would hate for you to accidently corrupt your hard drive because of a batch programming mistake.

DISKPART是一种潜在的危险工具。我没有看到你如何使用LIST命令造成伤害,但是如果误用其他命令可能会产生问题。在您学习批处理脚本的基础知识时,我建议您避免使用DISKPART。因为批处理编程错误,我讨厌你意外地破坏你的硬盘。

#1


0  

Ouch.

You've got a number of problems.

你有很多问题。

The output of LIST PARTITION gives the size and offset of each partition. I'm not very familiar with DISKPART, but I don't believe the size gives the amount of free space within the partition. It indicates how large the partition is, but gives no indication of how much space has already been allocated to existing files. The offset indicates the location of the partition. So it seems you are off on the wrong foot right from the get-go.

LIST PARTITION的输出给出了每个分区的大小和偏移量。我对DISKPART不太熟悉,但我不相信大小会给分区内的可用空间量。它表示分区有多大,但没有指示已经为现有文件分配了多少空间。偏移量表示分区的位置。所以看起来你从一开始就走错了路。

That being said, you have some major syntax problems in your script.

话虽这么说,你的脚本中有一些主要的语法问题。

It looks like you are trying to read a file in your FOR statement, but in reality your syntax is attempting to execute a command because you enclosed the file name in single quotes. You need to type HELP FOR from the command line and read carefully the description of the FOR command.

看起来您正在尝试在FOR语句中读取文件,但实际上您的语法正在尝试执行命令,因为您将文件名括在单引号中。您需要从命令行键入HELP FOR并仔细阅读FOR命令的说明。

You want either

你也想要

for /f "tokens=3" %%f in (log.txt) do (

or, if the filename contains spaces (which yours doesn't), then

或者,如果文件名包含空格(您的文件不包含空格),那么

for /f "usebackq tokens=3" %%f in ("file name with spaces.txt") do (

Your line FIND /C /I "GB" .\log.txt > %isComplete% is attempting to write the output of the FIND command to a file named "0". I doubt that is your intention. The FIND command will print out the complete text of any line that contains your search string of "GB". It will also set the ERRORLEVEL to 0 if it finds at least one matching line, or 1 if it fails to find a matching line. I'm not sure what you are attempting to do.

您的行FIND / C / I“GB”。\ log.txt>%isComplete%正在尝试将FIND命令的输出写入名为“0”的文件。我怀疑这是你的意图。 FIND命令将打印出包含搜索字符串“GB”的任何行的完整文本。如果找到至少一个匹配的行,它也会将ERRORLEVEL设置为0,如果找不到匹配的行,则将其设置为1。我不确定你要做什么。

Your IF statement is a mess. Here again you need to type HELP IF from the command line and read carefully how to use it. Your existing attempt has many errors

你的IF声明是一团糟。在这里,您需要从命令行键入HELP IF并仔细阅读如何使用它。您现有的尝试有很多错误

  • You can't extend an IF statement accross multiple lines unless you either use line continuation ^, or parentheses.

    除非使用行继续^或括号,否则不能扩展多行的IF语句。

  • You can't use > as a comparison operator, you need to use GTR

    您不能使用>作为比较运算符,您需要使用GTR

  • I can't figure out exactly what comparison you are trying to make.

    我无法弄清楚你想要做什么比较。

I don't know how to point you in the right direction because I don't understand what your overall mission is. Perhaps if you describe in more detail what you are trying to do, someone can provide a script that will accomplish the task, and you can learn from that example.

我不知道如何指出你正确的方向,因为我不明白你的整体使命是什么。也许如果你更详细地描述你想要做什么,有人可以提供一个完成任务的脚本,你可以从这个例子中学习。

DISKPART is a potentially dangerous tool. I don't see how you can do harm with the LIST command, but other commands could create problems if misused. While you are learning the rudiments of batch scripting, I suggest you avoid DISKPART. I would hate for you to accidently corrupt your hard drive because of a batch programming mistake.

DISKPART是一种潜在的危险工具。我没有看到你如何使用LIST命令造成伤害,但是如果误用其他命令可能会产生问题。在您学习批处理脚本的基础知识时,我建议您避免使用DISKPART。因为批处理编程错误,我讨厌你意外地破坏你的硬盘。