batch file to run a series of specific commands - beginner

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
philo_w
Posts: 4
Joined: 10 Jun 2015 08:13

batch file to run a series of specific commands - beginner

#1 Post by philo_w » 10 Jun 2015 08:26

First of all, let me start by saying that I have absolutely zero knowledge about coding or scripting, I've literally just started looking up on the subject last night, so bear with me.

Background info:
Basically I want to send some people a .pfx file (Its a public key component of an encrypted file), and I want them to install that in their computer so they can view PDFs from me that is encrypted with their ID. The problem is that they can send that key to a third party and that third party will be able to view the PDFs I send to the first party, and I want to avoid that. In order to do so, I want to create a .bat file that the user will run which will contain commands to find the .pfx file, run and install it, and after installation delete the .pfx file so they can't pass the key along. (I understand I'm doing this backwards as the user is supposed to send me their key to which I then encrypt the PDF with, but my users are even more computer illiterate than me so this is the work around)


Here's an analogy to my problem:

Alice downloads "key.pfx" and "install.bat" files from a link to my cloud storage --> Alice saves files to their desktop in a folder called "password" --> Alice runs install.bat --> command lines in install.bat installs key.pfx --> after installation it deletes the key.pfx file and the install.bat itself.

My coding in the install.bat for the above would be the following:

@echo off
cls

start /wait C:\Users\Alice\Desktop\password\key.pfx

del /q C:\Users\Alice\Desktop\password\key.pfx*.*

del /q C:\Users\Alice\Desktop\password\install.bat*.*

But that would only work if Alice downloads the .bat and .pfx files in to a folder on her desktop called "passwords". If she downloads the files anywhere else and runs the install.bat, it would not work.

So how can I make a batch file to first find out what path the key.pfx is stored, and then use that path to run the "start" and "del" commands?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: batch file to run a series of specific commands - beginn

#2 Post by foxidrive » 10 Jun 2015 10:10

philo_w wrote:But that would only work if Alice downloads the .bat and .pfx files in to a folder on her desktop called "passwords". If she downloads the files anywhere else and runs the install.bat, it would not work.

So how can I make a batch file to first find out what path the key.pfx is stored


You can search every drive on Alice's PC - but what if she makes a copy and saves it to dropbox, or a USB stick etc?

philo_w
Posts: 4
Joined: 10 Jun 2015 08:13

Re: batch file to run a series of specific commands - beginn

#3 Post by philo_w » 10 Jun 2015 21:00

Thanks Foxidrive for the response.

Can you please tell me what the code is to do a search command of their entire hard drive?
And once it has found the .pfx file, then what are the run and del commands to run and delete the file?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: batch file to run a series of specific commands - beginn

#4 Post by foxidrive » 10 Jun 2015 23:52

philo_w wrote:Thanks Foxidrive for the response.


No worries.
JFTR initial posts from a user are moderated to reduce spam - and which is why your multiple replies weren't visible.
I think your posts will appear as normal now.

As to your question - you didn't comment on the limitation that I raised regarding your strategy.

philo_w
Posts: 4
Joined: 10 Jun 2015 08:13

Re: batch file to run a series of specific commands - beginn

#5 Post by philo_w » 11 Jun 2015 02:24

but what if she makes a copy and saves it to dropbox, or a USB stick etc?


What I do is create interactive children's educational PDFs and sell them to my subscribers on O2O platforms. The majority of my users are your average house mums/wives. Currently I'm only password protecting my PDFs, but I've found out that some subscribers have been sending the PDFs they've bought from me along with the passwords to other parents. I want to get around that. I understand that there are limitations to my strategy, so I'm sending the files via a once off link to my cloud storage. Once the user has used that link to download the files, it can't be used again. But there's no way around for me if they use a usb stick.
Also, I know you're supposed to let the user create a private key which they send to me, and I use that key to encrypt the PDF so only they can open (which is how it's supposed to be done,) you'll have to remember that most of my subscribers are you average mums, and they are not that computer literate. So it's a balance between security and user friendliness.
I hope that by doing it this way, if I put enough obstacles in their way, they won't be bothered to try to figure out a way to pirate my products, because they are really cheap anyway.
But in order for my way to work I need to be able to delete the .pfx file from their hard drive, other wise, they can just send the .pfx along with the PDF to anyone.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: batch file to run a series of specific commands - beginn

#6 Post by foxidrive » 11 Jun 2015 03:59

philo_w wrote:Can you please tell me what the code is to do a search command of their entire hard drive?


Ok. Regarding your question - we don't know how many drives are on the system, and which drive is the download drive.

A solution would need to search every writable drive, and this presents a further few problems:

A) What should be done if there is more than one .pfx file on all the writable drives?
B) There is a significant time penalty to search all writable drives, which may include network drives.
C) The solution is going to have to run in a non-administrator account and without using any admin-only tools.

philo_w
Posts: 4
Joined: 10 Jun 2015 08:13

Re: batch file to run a series of specific commands - beginn

#7 Post by philo_w » 11 Jun 2015 22:35

Ok found this in another forum:

Anyway, the working directory is stored in the "%CD%" variable, and the directory where the bat was launched is stored on the argument 0. Then if you want to know the working directory on any computer you can do:

@Echo OFF
Echo Launch dir: "%~dp0"
Echo Current dir: "%CD%"


So that means the bat file will always know where it is being launched from no matter where you place the file, correct? Since my bat and pfx files will be put in a same subfolder, can't the bat file use it's own directory path to find and run the pfx file?

But to answer your questions:

A) What should be done if there is more than one .pfx file on all the writable drives?
What if the name of the .pfx is known? Would that be easier?
B) There is a significant time penalty to search all writable drives, which may include network drives.
Remember most of them will be home users with average home computers, so I don't think they will have network drives.
C) The solution is going to have to run in a non-administrator account and without using any admin-only tools.
Will it help if I include instructions to tell them to run as admin?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: batch file to run a series of specific commands - beginn

#8 Post by foxidrive » 12 Jun 2015 09:24

Ahh, I missed the most important part, that the batch file will be in the same folder as the pfx file.

This might work for you - the temporary file is one way to eliminate the error message that occurs when a running batch file deletes itself.

All paths are relative to the directory that has the batch file in it, which is clicked on.

Code: Select all

@echo off
mode con: cols=1 lines=1
cls
start "" /b /wait key.pfx
del key.pfx
>"%temp%\test.bat" echo @del install.bat
"%temp%\test.bat"

Post Reply