Really struggling with double quotes, double percent signs etc in batch script.
So i have a folder, lets call it C:\EncryptedFiles It can have one or more subfolders, and each subfolder can have one or more encrypted file (with extension gpg).
I need to look at everything inside C:\EncyptedFiles folder and iterate over files in each subfolder, and decrypt those files in same place where encrypted file is there.
So if we have another folder called Subfolder1 with file EncryptedFile1.csv.gpg, its absolute path will be
C:\EncryptedFiles\Subfolder1\EncryptedFile1.csv.gpg
I need to take this file, run it through gpg command line and output a file
C:\EncryptedFiles\Subfolder1\EncryptedFile1.csv.gpg
So this is what i have so far -
Set ROOTDIR=C:\EncryptedFiles
for /f %%g in ('dir /b /s ""%ROOTDIR""') do (
for /r %%i in (%%g) do (
REM this is where i need to pass each file to gpg
)
)
I know a command to decrypt files looks like this -
gpg --batch --yes --passphrase myPassword --output "D:\testOutput.csv" --decrypt "D:\testOutput.csv.gpg"
Above command will take D:\testOutput.csv.gpg and decrypt it, and create an output file testOutput.csv
I need to put these two together, and i can't seem to get it working. For decryption, i am able to decrypt files by hardcoding their full path in double quotes, as shown above.
Now in the nested for loop, in outer loop, %%g is each subfolder within C:\EncryptedFiles, and it holds full path of that folder (which is what /s is for). In the inner loop %%i will have full path of each file within the subfolder.
Within Inner for loop i tried this -
setlocal EnableDelayedExpansion
setoutputName=%g%\%%~ni
setinputName=%%i
gpg --batch --yes --passphrase mypassword --output "%setoutputName%" --decrypt "%setinputName%"
endlocal
so the complete code is something like this -
Set ROOTDIR=C:\EncryptedFiles
for /f %%g in ('dir /b /s ""%ROOTDIR""') do (
for /r %%i in (%%g) do (
setlocal EnableDelayedExpansion
setoutputName=%g%\%%~ni
setinputName=%%i
gpg --batch --yes --passphrase mypassword --output "%setoutputName%" --decrypt "%setinputName%"
endlocal
)
)
My understanding behind setoutputName=%g%\%%~ni is that %g will give the directory name (from outer loop) and %%~ni will give input file's name without the extension. So .gpg will be removed. So output path will be same as path of current file being iterated over in the innerloop, with extension removed.
My batch script just hangs forever until i force quit it. Can someone please help me out. And hopefully, provide answers for batch script and not the command prompt. I know everyone says the only difference is that we use double percent instead of single percent when it comes to batch script, but i have run into scenrioa where behavior changes from command prompt to batch script