Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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
Variables Batch file可删除少于3行的文件_Variables_Batch File_Store_Delete File_Findstr - Fatal编程技术网

Variables Batch file可删除少于3行的文件

Variables Batch file可删除少于3行的文件,variables,batch-file,store,delete-file,findstr,Variables,Batch File,Store,Delete File,Findstr,我需要一个批处理文件,该文件将检查指定文件夹中具有.txt扩展名的文件中包含的行数,并删除任何少于3行的文件 到目前为止,我的代码打印指定文件夹中每个.txt文件中的行总数。问题是我不知道如何将这些信息捕获到变量中: @echo off for %%A in ("D:\test\*.txt") do ( findstr /R /N "^" %%A | find /C ":" ) 我试图修改此代码以执行所需的操作失败: @echo off set count=0 for %%A in (

我需要一个批处理文件,该文件将检查指定文件夹中具有.txt扩展名的文件中包含的行数,并删除任何少于3行的文件

到目前为止,我的代码打印指定文件夹中每个.txt文件中的行总数。问题是我不知道如何将这些信息捕获到变量中:

@echo off
for %%A in ("D:\test\*.txt") do (
    findstr /R /N "^" %%A | find /C ":"
)
我试图修改此代码以执行所需的操作失败:

@echo off
set count=0
for %%A in ("D:\test\*.txt") do (
    set /a count=findstr /R /N "^" %%A | find /C ":"
    if %count% LSS 3 del %%A
)
请有人能为这个问题提供一些建议或解决方案

任何帮助都将不胜感激。

请尝试以下方法(也可以在子文件夹中使用):

我认为这应该行得通,(如果没有一行以
开头)

希望这有帮助

Mona

试试这个(它也会在子文件夹中这样做):

我认为这应该行得通,(如果没有一行以
开头)

希望这有帮助

Mona

试试这个(它也会在子文件夹中这样做):

我认为这应该行得通,(如果没有一行以
开头)

希望这有帮助

Mona

试试这个(它也会在子文件夹中这样做):

我认为这应该行得通,(如果没有一行以
开头)

希望这有帮助


Mona

只需获取for变量中的值并对其进行测试

rem Enumerate the files
for %%f in (*.txt) do (
    rem Count lines in file not containing some inexistent text, 
    rem so we get the number of lines in file
    for /f %%c in ('type "%%f" ^| find /v /c "TeXtNoTiNfIlEs"') do (
        rem if count is less than 3, delete file
        if %%c lss 3 del %%f
    )
)

只需获取for变量中的值并对其进行测试

rem Enumerate the files
for %%f in (*.txt) do (
    rem Count lines in file not containing some inexistent text, 
    rem so we get the number of lines in file
    for /f %%c in ('type "%%f" ^| find /v /c "TeXtNoTiNfIlEs"') do (
        rem if count is less than 3, delete file
        if %%c lss 3 del %%f
    )
)

只需获取for变量中的值并对其进行测试

rem Enumerate the files
for %%f in (*.txt) do (
    rem Count lines in file not containing some inexistent text, 
    rem so we get the number of lines in file
    for /f %%c in ('type "%%f" ^| find /v /c "TeXtNoTiNfIlEs"') do (
        rem if count is less than 3, delete file
        if %%c lss 3 del %%f
    )
)

只需获取for变量中的值并对其进行测试

rem Enumerate the files
for %%f in (*.txt) do (
    rem Count lines in file not containing some inexistent text, 
    rem so we get the number of lines in file
    for /f %%c in ('type "%%f" ^| find /v /c "TeXtNoTiNfIlEs"') do (
        rem if count is less than 3, delete file
        if %%c lss 3 del %%f
    )
)
这在这里起作用:

@echo off
for %%a in ("d:\test\*.txt") do for /f %%b in ('find /c /v "" ^< "%%a" ') do if %%b LSS 3 del "%%a"
@echo关闭
对于“%d:\test\*.txt”中的%%a,为/f%%b执行('find/c/v”“^<“%a”)如果%%b LSS 3 del”%%a,则执行
这在这里起作用:

@echo off
for %%a in ("d:\test\*.txt") do for /f %%b in ('find /c /v "" ^< "%%a" ') do if %%b LSS 3 del "%%a"
@echo关闭
对于“%d:\test\*.txt”中的%%a,为/f%%b执行('find/c/v”“^<“%a”)如果%%b LSS 3 del”%%a,则执行
这在这里起作用:

@echo off
for %%a in ("d:\test\*.txt") do for /f %%b in ('find /c /v "" ^< "%%a" ') do if %%b LSS 3 del "%%a"
@echo关闭
对于“%d:\test\*.txt”中的%%a,为/f%%b执行('find/c/v”“^<“%a”)如果%%b LSS 3 del”%%a,则执行
这在这里起作用:

@echo off
for %%a in ("d:\test\*.txt") do for /f %%b in ('find /c /v "" ^< "%%a" ') do if %%b LSS 3 del "%%a"
@echo关闭
对于“%d:\test\*.txt”中的%%a,为/f%%b执行('find/c/v”“^<“%a”)如果%%b LSS 3 del”%%a,则执行

您可以使用FIND/C快速计算所有*.txt文件中的行数,并通过管道将结果传递给FINDSTR,以便仅过滤出以计数0、1或2结尾的行。最后一个比较复杂的部分是使用一对FOR/F循环来解析结果,以仅给出文件名列表,去掉前导的
------------
以及冒号后面的所有内容

@echo off
pushd "D:\test\"
for /f "tokens=1*" %%A in (
  'find /c /v "" *.txt^|findstr /rc:" [012]$"'
) do for /f "delims=:" %%F in ("%%B") do del "%%F"
popd

您可以使用FIND/C快速计算所有*.txt文件中的行数,并通过管道将结果传递到FINDSTR,以便仅过滤出以计数0、1或2结尾的行。最后一个比较复杂的部分是使用一对FOR/F循环来解析结果,以仅给出文件名列表,去掉前导的
------------
以及冒号后面的所有内容

@echo off
pushd "D:\test\"
for /f "tokens=1*" %%A in (
  'find /c /v "" *.txt^|findstr /rc:" [012]$"'
) do for /f "delims=:" %%F in ("%%B") do del "%%F"
popd

您可以使用FIND/C快速计算所有*.txt文件中的行数,并通过管道将结果传递到FINDSTR,以便仅过滤出以计数0、1或2结尾的行。最后一个比较复杂的部分是使用一对FOR/F循环来解析结果,以仅给出文件名列表,去掉前导的
------------
以及冒号后面的所有内容

@echo off
pushd "D:\test\"
for /f "tokens=1*" %%A in (
  'find /c /v "" *.txt^|findstr /rc:" [012]$"'
) do for /f "delims=:" %%F in ("%%B") do del "%%F"
popd

您可以使用FIND/C快速计算所有*.txt文件中的行数,并通过管道将结果传递到FINDSTR,以便仅过滤出以计数0、1或2结尾的行。最后一个比较复杂的部分是使用一对FOR/F循环来解析结果,以仅给出文件名列表,去掉前导的
------------
以及冒号后面的所有内容

@echo off
pushd "D:\test\"
for /f "tokens=1*" %%A in (
  'find /c /v "" *.txt^|findstr /rc:" [012]$"'
) do for /f "delims=:" %%F in ("%%B") do del "%%F"
popd


您将在计算所有行之前终止该文件。?文件长度超过3行,它将删除每个文件,因为!计数第一次通过循环时,在每种情况下都等于1。您需要将
第二个
右括号移到
if
测试上方。即使逻辑是固定的,该方法也相当慢,并且它将无法计数空行或以开头的行;如当前所写。您将在计算所有行之前终止该文件。?文件长度超过3行,它将删除每个文件,因为!计数第一次通过循环时,在每种情况下都等于1。您需要将
第二个
右括号移到
if
测试上方。即使逻辑是固定的,该方法也相当慢,并且它将无法计数空行或以开头的行;如当前所写。您将在计算所有行之前终止该文件。?文件长度超过3行,它将删除每个文件,因为!计数第一次通过循环时,在每种情况下都等于1。您需要将
第二个
右括号移到
if
测试上方。即使逻辑是固定的,该方法也相当慢,并且它将无法计数空行或以开头的行;如当前所写。您将在计算所有行之前终止该文件。?文件长度超过3行,它将删除每个文件,因为!计数第一次通过循环时,在每种情况下都等于1。您需要将
第二个
右括号移到
if
测试上方。即使逻辑是固定的,该方法也相当慢,并且它将无法计数空行或以开头的行;如前所述。+1,我发布了一个稍微更有效的方法,使用FINDSTR只过滤小文件,因此FOR/F只处理小文件。但是这段代码可能更容易理解。+1,我发布了一个稍微更有效的方法,使用FINDSTR只过滤小文件,因此FOR/F只处理小文件。但是这段代码可能更容易理解。+1,我发布了一个稍微更有效的方法,使用FINDSTR只过滤小文件,因此FOR/F只处理小文件