Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 如何使用findstr批处理删除.txt文件中的一行_Batch File - Fatal编程技术网

Batch file 如何使用findstr批处理删除.txt文件中的一行

Batch file 如何使用findstr批处理删除.txt文件中的一行,batch-file,Batch File,因此,我试图让我的批处理脚本在资产标记成功完成其工作并在这段代码中出现错误代码0后删除一个列表中的资产标记 FOR /F %%G IN (PingResults2.txt) DO ( @psexec -s -w C:\ \\%%G -c -f "USMTCAPTURE.cmd" IF !errorlevel! equ 0 ( echo %%G >> usmtdone.txt findstr /V %%G ToPingList2.txt

因此,我试图让我的批处理脚本在资产标记成功完成其工作并在这段代码中出现错误代码0后删除一个列表中的资产标记

FOR /F %%G IN (PingResults2.txt) DO (
    @psexec -s -w C:\ \\%%G -c -f "USMTCAPTURE.cmd"
    IF !errorlevel! equ 0 (
        echo %%G >> usmtdone.txt
        findstr /V %%G ToPingList2.txt > ToPingList2.txt
    )
)
据我所知,我的部分代码工作正常,但当它到达我循环中的这一行时,它只是清除了整个文件

findstr/V%%G ToPingList2.txt>ToPingList2.txt


我做了测试,在我看来它应该能工作
findstr/V%%G ToPingList2.txt
通过它自己调用文件中所有其他行的文本,当我测试
findstr/V%%G ToPingList2.txt>>ToPingList2.txt
时,它再次在文件中添加了所有其他行。那么为什么
findstr/V%%G ToPingList2.txt>ToPingList2.txt
将整个文件擦除干净?

当findstr仍将其作为输入处理时,您正在覆盖目标文件。因为您将条目存储在usmtdone.txt中,所以在for循环之后的一个步骤中很容易删除它们

FOR /F %%G IN (PingResults2.txt) DO (
  @psexec -s -w C:\ \\%%G -c -f "USMTCAPTURE.cmd"
  IF !errorlevel! equ 0 (
    echo %%G >> usmtdone.txt
  )
)
findstr /V /G:usmtdone.txt ToPingList2.txt >ToPingList2.tmp
Move /Y ToPingList2.tmp ToPingList2.txt

您正在覆盖目标文件,而findstr仍将其作为输入进行处理。因为您将条目存储在usmtdone.txt中,所以在for循环之后的一个步骤中很容易删除它们

FOR /F %%G IN (PingResults2.txt) DO (
  @psexec -s -w C:\ \\%%G -c -f "USMTCAPTURE.cmd"
  IF !errorlevel! equ 0 (
    echo %%G >> usmtdone.txt
  )
)
findstr /V /G:usmtdone.txt ToPingList2.txt >ToPingList2.tmp
Move /Y ToPingList2.tmp ToPingList2.txt

感谢LotPings的快速响应。我最终做的代码和你的建议略有不同,它做了我需要的事情,所以我想我会发布我自己的答案。似乎我缺少的是使用.tmp文件临时存储值,然后将其移动到原始位置

FOR /F %%G IN (PingResults.txt) DO (
@psexec -s -w C:\ \\%%G -c -f "USMTCAPTURE.cmd"
IF !errorlevel! equ 0 (
echo %%G >> usmtdone.txt
findstr /V %%G ToPingList2.txt > ToPingList2.tmp
Move /Y ToPingList2.tmp ToPingList2.txt
)
) 

谢谢你的帮助和建议!代码现在运行得很好

感谢LotPings的快速响应。我最终做的代码和你的建议略有不同,它做了我需要的事情,所以我想我会发布我自己的答案。似乎我缺少的是使用.tmp文件临时存储值,然后将其移动到原始位置

FOR /F %%G IN (PingResults.txt) DO (
@psexec -s -w C:\ \\%%G -c -f "USMTCAPTURE.cmd"
IF !errorlevel! equ 0 (
echo %%G >> usmtdone.txt
findstr /V %%G ToPingList2.txt > ToPingList2.tmp
Move /Y ToPingList2.tmp ToPingList2.txt
)
) 

谢谢你的帮助和建议!代码现在运行得很好

可能是因为您正试图打开
ToPingList2.txt
进行编写,而它已经打开供阅读。尝试输出到另一个文件,然后在
findstr
完成后
move/y newfile ToPingList2.txt>NUL
。可能是因为您试图打开
ToPingList2.txt
进行写入,而它已经打开进行读取。尝试输出到另一个文件,然后在
findstr
完成后
move/y newfile ToPingList2.txt>NUL