在文本文件中读取第n行并在MS批处理中处理

问题描述:

嗯..我需要的实际上只是读取明文文件的第n行并保存在变量中以便在批处理中进一步处理。所以我一直在搜索相当长的时间,并找到了很多解决方案(全部在for循环中)。还有很多这里堆栈溢出,但很好,没有为我工作。在文本文件中读取第n行并在MS批处理中处理

这里是我尝试了

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加密的7zip文件,因为我错误输入了密码,不知道它是什么。是的,像7zcracker这样的工具不工作,并最终陈述“密码为1”,但这个“工具”可以看到在解压缩过程中是否有“错误”(因为只有数据被加密,而不是名称,7z文件可以“解压”,但内容的大小为0 ofc)

您不需要获得tries的编号。只需阅读密码文件中的每一行即可。

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

不幸的是,这不会以某种方式工作。我由含有TESTFILE.TXT:(“
” =换行符注释点击‘发送’一次击中输入)
'1 5'
和try.bat含有代码和呼应成新文件tryresult.txt并没有输出... –

+0

我编辑了答案,添加'usebackq'。一定要有'ECHO ON'才能看到真正发生的事情。 – lit

+0

谢谢。使用'usebackq',它在测试批处理文件中工作得很好。猜猜这里是最好的,而不是整天使用谷歌搜索。现在尝试尝试200k以上的可能密码大声笑。 –