Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I automatically encrypt all file extensions using my batchtool?

I currently have this code I use to encrypt and decrypt files on my disk, but it checks for specific file extensions only, but I want it to encrypt every single file extension. Here is the code I currently have:

:1layer
echo Encrypting disk, this may take a while.
set ForLoopFile=%VaultPath%
FOR /f %%G IN ('dir /s /b /a-d %ForLoopFile%') DO (call :Encrypt %%G)
if %layer%==2 goto :2layer
goto :actionask

:Encrypt
set targetFile=%1
set outputFile=%targetFile:txt=txt.aes%
aes -e %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:dll=dll.aes%
aes -e %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:ini=ini.aes%
aes -e %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:exe=exe.aes%
aes -e %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:zip=zip.aes%
aes -e %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:rar=rar.aes%
aes -e %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:png=png.aes%
aes -e %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:jpg=jpg.aes%
aes -e %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:ico=ico.aes%
aes -e %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:gif=gif.aes%
aes -e %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:mp4=mp4.aes%
aes -e %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:m4v=m4v.aes%
aes -e %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:.py=.py.aes%
aes -e %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:asc=asc.aes%
aes -e %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:vbs=vbs.aes%
aes -e %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:bat=bat.aes%
aes -e %encryptionKey% %targetFile% %outputFile% >NUL
set outputFile=%targetFile:ovpn=ovpn.aes%
aes -e %encryptionKey% %targetFile% %outputFile% >NUL
del /f %targetFile%
exit /b

See, I have to change the target extension file for the extensionsI want to check for, but does anyone know how I can make it encrypt every single file, no matter the extension?

If anyone can help me, I’d greatly appreciate it.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have no idea on how to do this, all I tried was checking for file extensions manually, but this is limitted to the ones I specify, I want every file extension to get encrypted.

>Solution :

FOR /f "delims=" %%G IN ('dir /s /b /a-d %ForLoopFile%') DO if /i "%%~xG" neq ".aes" (call :Encrypt "%%G")

...

:Encrypt
aes -e %encryptionKey% "%~1" "%~1.aes" >NUL
del /f "%~1"
goto :eof

should accomplish this Note: untried

Always verify against a test directory before applying to real data.

The modification to the for line:
"delims=" to assign the entirety of the filenames to %%G, else only the first token will be assigned (see for/? from the prompt for docco.
if /i – only if (case-insensitive)
"%%~xG" neq ".aes" – the current file extension is Not EQual to .aes
"%%G" – entire filename is one parameter

This should skip .aes files so that you dont get .aes.aes.aes.aes as the procedure is repeated.

The :encrypt subroutine:
apply aes to the first parameter delivered to the routine (%1) stripped of the enclosing quotes (%~1); simply add .aes to the string for destination file.
then delete the file

This should allow filenames including spaces to be processed.

Note
add echo before the aes and del and remove the >nul to produce a tester version which will simply show the proposed actions on the console.

Note: untried

Always verify against a test directory before applying to real data.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading