I have a root folder C:\EncryptedFiles
. There could be multiple subfolders. Each subfolder could have up to one file. No way of knowing before hand how many subfolders will be there. I need to iterate over each file, decrypt it and output in a separate folder the decrypted file with a file name matching the naming convention ParentDirectoryOfFile_YYYYMMDD.
Initially I was struggling with looping over to decrypt, but that was answered in this post:
set "ROOTDIR=C:\EncryptedFiles"
for /R %ROOTDIR% %%i in (*.gpg) do (
gpg --batch --yes --passphrase mypassword --output "%%~dpni" --decrypt "%%~i"
)
In above code --output "%%~dpni"
puts the output file (which has same name as input file, but without extension .gpg
) in same folder. But I want all decrypted files to be stored in C:\DecryptedFiles
with naming convention of ParentDirectoryOfFile_YYYYMMDD.
For example, I have this directory structure:
C:\EncryptedFiles
\Folder1
File1.csv.gpg
\Folder2
File2.csv.gpg
Current code will produce a final output as:
C:\EncryptedFiles
\Folder1
File1.csv.gpg
File1.csv
\Folder2
File2.csv.gpg
File2.csv
So it puts the decrypted files exactly where respective encrypted files were. However, what I want is that all decrypted files go into a separate folder (and leave C:\EncryptedFiles
folder structure as is). I want the output to be like this:
C:\DecryptedFiles
Folder1_20191119.csv
Folder2_20191119.csv
I know the requirement may sound foolish, but that's how it is. Original names of encrypted files have no significance here. What folder they were originally put in is what identifies them.
So this is what I tried so far:
set "ROOTDIR=C:\EncryptedFiles"
set "DESTINATIONDIR=C:\DecryptedFiles"
for /R %ROOTDIR% %%i in (*.gpg) do (
gpg --batch --yes --passphrase mypassword --output "%DESTINATIONDIR%\%%~ni_%date:~10,4%%date:~7,2%%date:~4,2%" --decrypt "%%~i"
)
The problem with above code is that %%~ni
gives original file name, so I get following output:
C:\DecryptedFiles
File1.csv_20191119.csv
File2.csv_20191119.csv
Instead of that File1.csv
and File2.csv
parts, I want their parent directory's name to appear.
I understand that %%i
contains the full path name of the encrypted file. I should somehow be able to extract immediate parent directory's name from it. There are some good discussions here: https://www.dostips.com/forum/viewtopic.php?t=2427, but when I try it, parent directory turns out to be blank.