Have been away from programming for a little while but now finding myself needing to automate some things. Essentially what I would like to do is have the batch file search the local directory for the file named "(U) Password.txt," which consists of 4 lines of string. I want it to search using "findstr" the word, "TEXT," and save skip to the next line capturing that line in a variable to use for the decryption password. The remaining code has been tested and works ok but plenty of room for improvement. Can I use "Skip=1" to capture the line under the market "Text" with findstr to capture the next line?

::Sample Code

     @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 ::
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...
for %%a in (%DB_FILES%) do (
  decryptionSoftware.exe -key "%DB_PASSWORD%" %%a "PRAGMA rekey='';"
ECHO Done!

ECHO Testing...
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 ::

: Move Unencrypted Databases from the Root Directory to the newly Created Unencrypted File Structure ::
echo F| XCOPY "%folderName%\DatabaseTesting\Unencrypted_Databases\*.db"/Y

  • So what's the actual problem? "need the string "Text" to be used to set my search location. Need the line under it."? – Xen2050 Dec 18 '17 at 19:41
  • 3
    It is not very clear as to what specifically your problem is. Your question appears more like "here is some code, please fix it". – Eric F Dec 18 '17 at 19:52
  • Hey guys. Sorry for the delay. I am looking through a text file for a string called "Text." I am wanting to take the line under this and use it as a variable in later code. The issue is that I can find Text and return it to the variable but cannot get the line under it as that text will change depending on the file that I am reading. I will need to use "Text" as a marker to find the line beneath. – Stephen D. Dunn Dec 19 '17 at 16:34
  • Could you please update your question with a real world example of what the password text file looks like. – Squashman Dec 19 '17 at 19:37
  • You have a lot of problems with your code besides reading that text file. – Squashman Dec 19 '17 at 19:42

So if I am doing this right this actually might solve my problem. Instead of searching for the word "Text" in the file I can just search for line 4 in the file spaces and all. So this is my fix I think. Anyone think there is a better way?

@echo off
:: Exctract the Code from the Text Document ::
FOR /F "USEBACKQ SKIP=3" %%C IN ("(U) password.txt") DO ECHO %%C
  • With this I am storing the value in string into the Variable %%C and using that to pass the String for the password into the decryption line of code. – Stephen D. Dunn Dec 20 '17 at 17:42
  • So now you are saying it is the last line in the file? – Squashman Dec 20 '17 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.