读取文本文件中的第n行并在MS Batch

时间:2017-04-02 00:05:41

标签: windows batch-file cmd

嗯..我需要的只是读取明文文件的第n行并保存在变量中以便在批处理中进一步处理它。所以我一直在谷歌搜索很多时间,并找到了许多解决方案(所有在for循环中)。 Stack Overflow上也有很多,但是,没有一个对我有用。

以下是我尝试的一种方式的示例

for /f "skip=%toskip%" %%G IN (passwd.txt) DO if not defined line set "line=%%G"

所以这里有关于我项目的详细信息。

内容:pwcracker.bat(如下所示)
要求:7za.exe,passwd.txt(每行可能有1个密码的数据库)

pwcracker.bat:

    @echo on

    for /f %%i in ("passwd.txt") do set size=%%~zi
    if %size% leq 0 (
     echo Please create a dictionary of passwords first [File not found: passwd.txt]!>result.txt
     exit 1
    )

    @Overwrite result.txt if it exists
    echo [RESULT]>result.txt

    @Try to delete the log if it exists
    IF EXIST logging.txt del /F logging.txt

    @REM If the file wasn't deleted for some reason, stop and error
    IF EXIST logging.txt (
     echo The log file must be removed!>>result.txt
     exit 1
    )

    @Ask user for the file to unzip
    set /p filename=Enter 7z filename [name.7z]:

    @Check amount of PWs to try within the dictionary
    cls
    setlocal EnableDelayedExpansion
    set "cmd=findstr /R /N "^^" passwd.txt | find /C ":""

    @Set the amount to try
    for /f %%a in ('!cmd!') do set tries=%%a

    @####################################

    @Begin the for loop to try all PWs until unzipped or all PWs are tried
    FOR /L %%X IN (1,1,%tries%) DO (

     @Set PW to try
     @CODE INPUT TO READ AND STORE PW in local variable %passwd%

     @try to unzip using the given password and log the output
     7za e %filename% -o%CD%\unzipped -p%passwd%>>logging.txt

     @check the size of the log file
     for /f %%i in ("logging.txt") do set size=%%~zi

     @see whether it was succesful and log the tried password in the resuts
     findstr /m "Error" logging.txt
     if %errorlevel%==1 (
      echo It didn't work with PW: %passwd%>>result.txt

      @Try to delete the log if it exists
      IF EXIST logging.txt del /F logging.txt

      @REM If the file wasn't deleted for some reason, stop and error
      IF EXIST logging.txt (
       echo The log file couldn't be removed!>>result.txt
       exit 1
      )
      @end of error-check clause
     ) 


     else (
      if %size% leq 0 (
      echo Something went wrong, please check manually>result.txt
      echo Tried PW : %passwd% >>result.txt
      exit 1
     )
    @end of prior else block
    )

    else (
     echo Unzipped succesfully with PW: %passwd%>result.txt
    exit 1
    )

   @end of for loop (1,1,tries)
   )

这个批处理文件基本上只是"破解"我的aes-encrypted 7zip文件,因为我输错了密码并且不知道它是什么。是的,像7zcracker这样的工具不会起作用并最终说明"通道是1"顺便说一句,但这个"工具"可以看出是否存在"错误"解压缩或不解压缩(因为只有数据是加密的而不是名称,7z文件可以是"解压缩",但内容'大小是0 ofc)

1 个答案:

答案 0 :(得分:0)

您无需获取tries的数量。只需读取密码文件中的每一行。

FOR /F "usebackq tokens=*" %%p IN (`TYPE passwd.txt`) DO (
    ECHO Trying password %%p
    REM try the password
)