2017-02-08 39 views
-2

我已經搜索了大量的Internet,並且找不到使用批處理代碼在文本文件的最後10行中查找特定單詞的方法。到目前爲止,我有這個在整個文件中查找文本的東西。如何在批量文件的最後10行中查找特定單詞

>nul find "Example Words" examplefile.txt && (
REM Found... 
goto next) || (
REM Not Found... 
goto home 
) 

所以,再次,這樣做是它搜索整個文件例詞,而我需要的是一種方法來尋找在只有最後10行那些話?

+0

是最後的** 10 **還是最後的** 2 **行?請澄清! – aschipfl

+1

@aschipfl:存在10種類型的人:那些知道二元系統的人和那些不知道... – Aacini

+0

Hehe,@Aacini ...我希望OP能澄清... – aschipfl

回答

0

首先,計算出現在文本文件中的行數:

for /F %%C in ('^< "examplefile.txt" find /C /V ""') do set "COUNT=%%C" 

然後確定有多少行被跳過(所有,但最後10行,例如):

if %COUNT% GTR 10 (set /A "SKIP=COUNT-10") else (set "SKIP=0") 

最後,使用more command實際跳過線:

more +%SKIP% "examplefile.txt" | > nul findstr /L /I "Example Words" && (
    rem Found 
    goto next 
) || (
    rem Not Found 
    goto home 
) 

使用findstr而不是find指定多個SPACE - 分隔的搜索字符串或單詞(如ExampleWords這裏)。使用/I選項執行不區分大小寫的搜索。

請注意,more需要用戶在輸入文件包含65535或更多行時輸入。它將空間轉換爲TAB,但這不應該改變任何內容,因爲您對搜索沒有興趣,因爲您只搜索單詞。


下面是一個更復雜的和效率低的方法依賴於for /F loop,也使用可變COUNT如上得到。它需要skip選項for /F,這是建立如下:

set /A "SKIP=COUNT-10" 
if %SKIP% GTR 0 (set "SKIP=skip=%SKIP%") else (set "SKIP=") 

於是就有了上述for /F循環:

for /F "%SKIP% delims=" %%L in ("examplefile.txt") do (
    echo(%%L| > nul findstr /L /I "Example Words" && (
     rem Found 
     goto next 
    ) 
) 
ren Not Found 
goto home 

每一道線條都是單獨搜索的循環;只要遇到匹配,就執行goto next; goto打破了循環上下文,因此for /F不再執行其正文內的任何命令。

注意for /F會忽略空行,並且還會以默認分號(;)開頭。一些特殊的字符(^&()"<>|)可能會導致與在管(|)的echo命令麻煩。

+0

看來這不起作用。每當我將它添加到我的代碼中時,它立即關閉。 – R2bEEaton

+0

你爲代碼添加了什麼,究竟是什麼問題? – aschipfl

+0

在這裏,我替換了後面的部分:After QPos with ...'for/F %% C in('^ <「%Appdata%\。minecraft \ logs \ latest.log」find/C')do set「COUNT = %% C「New Line」如果%COUNT%GTR 10(set/A「SKIP = COUNT-10」)else(set「SKIP =」)'New Line'more +%SKIP%「%appdata%\。 \ logs \ latest.log「| > nul findstr/L/I「連接到服務器...」&&( rem發現 轉到下一個 )|| ( rem未找到 轉到家 )'當我這樣做,而不是重複,直到它找到的話,它立即關閉。 – R2bEEaton

0

這包括/排除文件頂部或底部的n行。

剪切

filter cut {t|b} {i|x} NumOfLines 

剪切從文件的頂部或底部的行數。

t - top of the file 
b - bottom of the file 
i - include n lines 
x - exclude n lines 

cscript //nologo c:\folder\filter.vbs cut t i 5 < "%systemroot%\win.ini" 

腳本filter.vbs

Set Arg = WScript.Arguments 
set WshShell = createObject("Wscript.Shell") 
Set Inp = WScript.Stdin 
Set Outp = Wscript.Stdout 
Set rs = CreateObject("ADODB.Recordset") 
With rs 
    .Fields.Append "LineNumber", 4 

    .Fields.Append "Txt", 201, 5000 
    .Open 
    LineCount = 0 
    Do Until Inp.AtEndOfStream 
     LineCount = LineCount + 1 
     .AddNew 
     .Fields("LineNumber").value = LineCount 
     .Fields("Txt").value = Inp.readline 
     .UpDate 
    Loop 

    .Sort = "LineNumber ASC" 

    If LCase(Arg(1)) = "t" then 
     If LCase(Arg(2)) = "i" then 
      .filter = "LineNumber < " & LCase(Arg(3)) + 1 
     ElseIf LCase(Arg(2)) = "x" then 
      .filter = "LineNumber > " & LCase(Arg(3)) 
     End If 
    ElseIf LCase(Arg(1)) = "b" then 
     If LCase(Arg(2)) = "i" then 
      .filter = "LineNumber > " & LineCount - LCase(Arg(3)) 
     ElseIf LCase(Arg(2)) = "x" then 
      .filter = "LineNumber < " & LineCount - LCase(Arg(3)) + 1 
     End If 
    End If 

    Do While not .EOF 
     Outp.writeline .Fields("Txt").Value 

     .MoveNext 
    Loop 
End With 

這裏的一個示例代碼通過在dir命令輸出計數線計數的文件夾。

@SETLOCAL ENABLEDELAYEDEXPANSION 
@CD C:\newfolder 
@set count=0 
@For /f "delims=" %%A in ('dir C:\newfolder\*.* /ad /s /b') Do (
    set /a count=!count!+1 
@rem  If !Count! gtr 1 echo Num of folders 
) 

Set /a num=%random% %% %Count% + 1 
Echo %num% 

@For /f "skip=%num% delims=" %%A in ('dir C:\newfolder\*.* /ad /s /b') Do (
Echo this is nth random folder 
Echo Copy "%~dpnx0" "%%A" 
Exit /b 
) 
+0

只是爲了澄清,是否沒有辦法批量執行此操作? – R2bEEaton

+0

因爲有。這將是痛苦的緩慢。 – Freddie

+0

使用'for/f'循環,在延遲擴展模式下對行進行計數,再次使用'for/f'丟棄除第1步中制定的最後n行外的所有文件。 – Freddie

相關問題