Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I'm totally oblivious to DOS batch programming so I hope somone can help me. What I have is an executable that can encrypt an individual file in a directory. What I pass to the executable is the folder\filename and output folder\filename.
Ex: encrypt.exe c:\myfolder\myfile.ext | c:\myfolder\myfile.ext
The encrypt executable takes the file "myfile.ext" and encrypts it to the same folder with the same name (myfile.ext). This works great if I only do it on an individual files basis. What I want to do is have a batch file that loops through the folder in these steps:
1. Indentifies the filename of each file in a folder (so I don't need to know what it is in advance) and sets that as a variable.
2. Loops through and executes the encrypt.exe on each file, while passing the filename variable as the output filename.
In a perfect world, I'd be able to enter the folder name that contains the files I want to encrypt from the command line, and even possibly tell it the file type (extension) I want to encrypt:
encrypt.bat c:\myfolder\
or
encrypt.bat c:\myfolder\*.ext
Thanks!

The following batch is what you want
@Echo Off
:: Encrp.bat Syntax Encrp Folder File_Extension
:: Folder is the folder to be processed
:: File_Ext the extension of fiules without the . (dot)
:: Use * to encrypt all files stored in the folder
:: Example: Encr C:\Myfolder\MySubFolder batEcho.
If %1.==. (Echo Folder missing & GoTo :EOF)
If not exist %1.\Nul (Echo Folder "%~f1" not found & GoTo :EOF)
If %2.==. (Echo Extension missing & GoTo :EOF)Echo Encrypting Files *.%2 in Folder %~f1
Echo.
For /F "tokens=*" %%A in ('Dir /B /A:-D %1.\*.%2') Do (
Encrypt %~f1.\%%A %~f1.\%%A
Echo Encrypt %~f1.\%%A %~f1.\%%A)
Echo.
Echo Processing completedHow to use it is explained in the comment lines at the top of the code, anyway type Encrp C:\MyFoder\Sub1\Sub2 * to encrypt all files stored in C:\MyFolder\Sub1\Sub2 or use the extension you like. The folder name and files names must not contain blanks. Win NT/2K/XP required. I suggest you do NOT name the batch as your exe (Encrypt.exe).
If you need more support, post again.

I do not fully understand the syntax of your Encrypt command. If it requires a pipe symbol between input and output operands replace my statement
Encrypt %~f1.\%%A %~f1.\%%A
with
Encrypt %~f1.\%%A | %~f1.\%%A

Thanks IVO. This works great on files located in the root folder, which is good enough for now. Couple of other "nice to haves", however, would be:
1. A way to recurse through (and run my utilitiy) on files located in subdirectories under the root.
2. A way to read each ASCII file prior to running the encryption utility to see if they contain a text string in them. If they do have the text string - run the encryption, if they don't - bypass the file (take no action on that file) and move to the next.
Anyway, thanks a bunch this works for now.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |