简体   繁体   English

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

[英]read n-th line in a text file and process in MS Batch

Well.. What I need to is actually just to read the n-th line of a cleartext file and save in a variable to process it further within the batch. 嗯..实际上,我需要读取明文文件的第n行并保存在变量中以在批处理中进一步处理它。 So I have been googling quite maaany hours and found many solutions (all in a for loop). 因此,我花了很多时间在谷歌上搜索,发现了很多解决方案(全部在for循环中)。 Also many here on Stack Overflow, but well, none worked for me. 在Stack Overflow上也有很多内容,但是,没有一个对我有用。

Here is an example of one way I tried out 这是我尝试的一种方法的示例

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

So here the details on my project. 所以这里是我项目的详细信息。

contents: pwcracker.bat (shown in the following) 内容:pwcracker.bat(如下所示)
requires: 7za.exe, passwd.txt (a database with 1 possible password on each line) 要求:7za.exe,passwd.txt(每行有1个可能的密码的数据库)

pwcracker.bat: 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)
   )

This batch file will basically just "crack" my aes-encrypted 7zip file as I have mistyped the password and don't know what it is for certain. 这个批处理文件基本上只会“破解”我的经aes加密的7zip文件,因为我输错了密码,并且不确定它到底是什么。 And yeah, tools like 7zcracker don't work and end up stating "The passwod was 1" btw, but this "tool" can see whether there was an "Error" while unzipping, or not (since only the data is encrypted and not the names, the 7z file can be "unzipped", but the contents' size is 0 ofc) 是的,像7zcracker这样的工具无法正常工作,并最终提示“密码为1”,但是此“工具”可以查看解压缩时是否存在“错误”(因为仅加密了数据,名称,可以“解压缩” 7z文件,但内容的大小为0 ofc)

You don't need to get the number of tries . 您无需获取tries Just read each line in the password file. 只需阅读密码文件中的每一行。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM