[英]read n-th line in a text file and process in MS Batch
嗯..實際上,我需要讀取明文文件的第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加密的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
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.