有一段时间没有编程了,但现在发现自己需要自动化一些事情。本质上,我想做的是让批处理文件在本地目录中搜索名为“(U) Password.txt”的文件,该文件由 4 行字符串组成。我希望它使用“findstr”搜索单词“TEXT”,然后保存跳到下一行,将该行捕获到变量中以用于解密密码。其余代码已经过测试并且运行正常,但仍有很大的改进空间。我可以使用“Skip=1”捕获市场“Text”下的行,并使用 findstr 捕获下一行吗?
::示例代码
@ECHO OFF
REM version 0.4
:: Get the Current Directory ::
SET FolderName=%cd%
:: Make the new Encrypted and Unencrypted Folders
echo D| md %FolderName%\'Encrypted_Databases'
echo D| md %FolderName%\'Unencrypted_Databases'
echo D| md %FolderName%\'Difference_Reports'
set findtext="TEXT"
set findfile="(U) Password.txt"
findstr %findtext% %findfile%
for /f "delims=" %%a in ('findstr %findtext% %findfile%') do echo %%a
:: Decrypt the Databases ::
SET /p DB_PASSWORD= %%a
IF ["%DB_PASSWORD%"]==[""] ECHO Password not set. & GOTO EXIT
SET "DB_FILES=db1.db db2.db db3.db db4.db db5.db db6.db db7.db db8.db db9.db"
ECHO Decrypting...
SET /a DB_TOTAL_COUNT=0
for %%a in (%DB_FILES%) do (
decryptionSoftware.exe -key "%DB_PASSWORD%" %%a "PRAGMA rekey='';"
SET /a DB_TOTAL_COUNT+=1
)
ECHO Done!
ECHO.
ECHO.
ECHO Testing...
SET /a DB_COUNT=0
3>nul 2>nul (
for %%b in (%DB_FILES%) do (
for /f "tokens=*" %%I in ('decryptionSoftware.exe %%b "SELECT COUNT(*) FROM SQLITE_MASTER;"') do (
REM The following line will not execute if the SQLite command above fails
SET /a DB_COUNT+=1
)
)
)
IF %DB_COUNT%==%DB_TOTAL_COUNT% ECHO Success decrypting!
IF %DB_COUNT% LSS %DB_TOTAL_COUNT% ECHO Problem decrypting!
::EXIT ::
ECHO.
ECHO.
: Move Unencrypted Databases from the Root Directory to the newly Created Unencrypted File Structure ::
echo F| XCOPY "%folderName%\DatabaseTesting\Unencrypted_Databases\*.db"/Y
PAUSE
exit
答案1
因此,如果我做得正确,这实际上可能会解决我的问题。我不用在文件中搜索单词“Text”,而是只需在文件空间中搜索第 4 行即可。所以我认为这是我的解决方案。有人认为有更好的方法吗?
@echo off
:: Exctract the Code from the Text Document ::
FOR /F "USEBACKQ SKIP=3" %%C IN ("(U) password.txt") DO ECHO %%C