just have the user pass the password as a command line option to your batch. If the attached sudo code was in zip.bat
zip.bat password
Main Topics
Browse All TopicsHello. I'm working with the 7zip command line tool, which is pretty basic. I've got my command set up to take all the PDF documents in a folder and place it in a zip with a password. I'm using the command line because I'm creating it for a user who isn't prepared to do the steps manually. It's just easier to let them do something that only takes a click or two.
The problem is, the batch file I'm writing for 7zip is always going to use the same password, so what I really need is for the user to be able to input the password once she starts the batch file, whether it should be read from a text or input straight into the command prompt window when it opens. Preferably the second option.
I'm trying to find a way to ask for input and place it in a temporary variable I guess, and then call that from 7zip's command line tool, but I'm not entirely sure that's even possible. Any other suggestions on how to go about doing this will be appreciated as well.
I've tried reading up on the variables in DOS, but I get lost when it starts using FOR %%A in ??? and those things. Thanks a lot!
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
t0t0. Thanks for the quick reply. After reading the other comments I understand how yours works, but yours reads from the file first and then asks for a password if one is not found. Is that correct? I'm needing a password that will change every time hence I need the user input.
jwarnken, I'm thinking you are using the same method as TakedaT. I'm not sure what the %1 means, but it looks like you placed that there to just mean "some password"?
In any case, thanks TakedaT. I tried your method, and I should have figured it out before, but it seems to work perfectly. I'll simply prompt the user in that way to enter her password.
I'll give any of you a chance to reply if you want/need to and then I'll close the question in a while. Thanks again!
Ok, I just did a little test file and I think I understand what you're saying.
So basically the user would have to manually run "zip.bat password" in the run command or somewhere to put in her own password.
Just for clarity, would it still work if she just ran "zip.bat password" and instead of using the set pass=%1, I could just run "zip.exe %1 c:\temp.zip c:\temp\file.xxx" in the batch file? This would save a step of taking a variable and setting it to another variable.
Thanks very much for the clarity. I was actually completely unaware that you could run a batch file with a set of parameters behind it like this. That could be very helpful one day. Do you know of a website that explains more command line functions very well?
I can't understand WHY you would want someone to enter a DIFFERENT password EVERY TIME they fire up the SAME program....
Please explain this to me.
OR, do you intend the batch file to be used by different people and that THAT is the reason why you need the ability to prompt for different passwords. Because..... if that is the case then you need to be aware of a morte efficient method of doing this.
Please respond ASAP !!
Using command-line parameters directly with your command as in the following example:
7zip %1 ..... etc
is all very well if the user doesn't mind going through the following steps:
1) start a DOS box
2) type in the command (your batch file name AND a pssword etc..)
providing of course she CAN remember what the command is AND what the passwrod is....
If you want the user to double-click an icon on the desktop to start the batch file then %1 will not work.... which is why you will need to use SET /P instead.
Please report back stating which is it to be.
Also, CAN YOU PLEASE CLARIFY, is the password you're asking for to zip up documents OR, to unzip documents?
I believe he wants all of this to make a "create passworded zip" shortcut batch file. Thats why he wants to have the ability to supply a different password each run.
I actually created something similar to this, but for a dir listing of directorys outputed to a txt file. I added it into the registry so that a right click on a folder showed that option. Something could be done similar to this for this case. You could make your batch file, drop it into your windir, then add the context option so it prompts to zip ..and voila.
I apologize for the delay. I went to a long lunch.
t0t0 What I was looking for was exactly what TakedaT said.
I have a single user that will be exporting some documents to PDF. She will then need to ZIP these PDFs with encryption. After which she will burn the ZIP to a CD and mail it to a company who will review it with the password she created and sent to them through email. This is why each password will need to be different. These are medical records and have to be thoroughly encrypted. I have no idea why the company wants everything on CD rather than VPN, or SFTP, but I'm just trying to make them happy.
Here is my final batch file. Basically, it deletes any previous ZIP files first that she would have already burned, then it asks her for the new password. Grabs all the files in the folder and adds them to the ZIP. She checks a log for errors and then it moves the PDF files to a backup folder leaving only the ZIP file which she would then burn to CD. There are a few Y or N choices that help guide her into making sure she has completed everything before she deletes or moves her files by accident.
If I should change anything or there are more suggestions I'd love to hear it. I love learning things like this.....Sometimes I wonder if I should learn a real programming language because I always have something new to try to automate.
I dont use 7zip to test, but I think I can see a few things that can use tweaking.
When asking for input, you need to be ready for the user to make errors. Like hitting enter without typing anything for example.
You can use the /i switch (incasesensitive) instead of checking for both upper and lower case on your if statements. Look at this example.
:START
set /p destroy=Destroy it?
If /i "%destroy%"=="" (
ECHO. Please enter something.
goto start
) else (
If /i "%destroy%"=="y" (
echo.Destroy it please.
) else (
echo.Dont destroy it.
)
)
Other than that, looks good to me.
Ok..I see what you mean. Thanks for the tip on the "/I" I assumed when I said IF NOT == n or y that the 'enter' key would be counted in there. But I tested and when hitting enter the script closed, so I modified what you sent a little and came up with this. I feel like I'm missing something because I had to put IF twice without ELSE, but I guess that's ok? If I did ELSE IF all the way through it didn't work at all, so I did two IFs in a row.
:DELZIPIF
set /p DEL="Please Y for Yes or N for No. "
IF /i "%DEL%"=="" (
GOTO DELZIPIF
)
IF /i "%DEL%"=="n" GOTO EXIT1
) else (
IF /i "%DEL%"=="y" GOTO DELZIP
) else (
GOTO DELZIPIF
)
This checks for blanks and starts over if blank, then checks for N and exits with error if N is hit, then for Y and moves forward to delete if Y is hit, and if any other key is hit at it starts over again.
The reason I did it this way is because if she accidentally hits any other key (or leaves it blank) I don't want her script to close. I'd rather it just ask her again. Everything look ok with this piece?
As long as everything works for you, it doesnt matter how you organize it. I always design my batches/vbscripts differently than most, just because I understand my own logic. Whichever way YOU understand best is what you should do. Remember, you are the one who will be finding the problems.
One thing with if statements in batches that always got me was the fact that there is no "then" like in vbscript. And you always need to pay close attention to where your parenthesis() are . They will cause your batch to end prematurely, leaving you wondering what is wrong.
Ok t0t0. Don't breathe any of that junk. I'll wait on your response.
You have a good point Takeda. I think the parenthesis was breaking it earlier which is why I went to two IF statements. Not having a "then" does make it a little tricky, but I don't know well VB either. I've only taken a class on Pseudo code (which is stupid because you have to learn a fake language. I don't know why they don't just give you a basic REAL language to learn), and a class on Java which I've forgotten most of by now.
Also, thanks for the good sportsmanship on splitting the points. I was trying to decide how to do that.
Right, I did look at you r code last night and there are several areas where you should concentrate more thought.
I won't go into this in great detail at the moment as I'm mid-way through plastering a couple of ceilings - I didn't want you to think I had abandoned you.
Firstly, the password thing.
You leave it up to the user to 'remember' each password for each job.... What would be neater is to generate a list of passwords on-the-fly. Each job would be numbered job #21, job #22, job #23 etc....
Your list of passwords would appear as:
job 1: Customer name, Password1234
job 2: The Gate keeper, Cj44PasswordT
::
::
job 34: Trinity Corporation, fFtTT66R5rpo
etc...
I'm assuming here that all password are a mix of uppercase and lowercase alphabetic characters with a fixed length of 12 characters.
The 'get_password' section of your code should validate the password for correct length, corrent characters, at least one of each type of character included and uniqueness.
Ideally, each customer's company name would be read from another file and the batch file would just work down that list prompting the user to just enter a 12-character passwrod (or how many you decide on). In fact, I might be easier to actually have the batch file itself auto-generate passwords.
This could be coded for you quite easily.
The next consideration is job-completion markers. Let me explain. I notice you've left it up to the user to decide whether it's okay to proceed - to delete the previous set of PDF files and start a new 'project'. I would also take this decision out of the user's hands too.
The easiest way to accomplish this is to create a sentinel file at the start of a new project and then delete it once the project is complete. In the meantime, during every stage the ot the batch file, a status is written to a status file so that if the batch file crashes or the user aborts or there's a system failure etc then when the batch file is re-run, it can resume fro where it left off.
This is vital for the proper maintenance of the password list and the project start-stop sentinel function.
I've given this some thought and the way I would approach this is as follows (this is just example code which can be modified to fit your program):
@echo off
if not exist sentinel (
copy nul sentinel>nul
echo Running normal...
start cmd /c %~dpnx0
) else (
echo Restoring session...
set /p status=<status
start cmd /c %~dpnx0 %status%
)
if not "%1"=="" goto %1
:continue
>status echo continue
echo Continuing normally....
:section2
>status echo section2
:section3
>status echo section3
:section4
>status echo section4
:getpassword
>status echo getpassword
set pwd=
set /p pwd=Enter password:
echo.
pause
:end
>status echo end
echo closing normal
del status
del sentinel
exit /b
But rather than closing, the batch file can just loop back to the start for the next job. Obviously, the user would have the option to end at any point.
These are just two ideas which I'm quite happy to work into your batch file if you decide upon it.
Pushing all that aside, I've noticed the code itself could do with some re-crafting.... one area for example is your IF statements.... Look at this:
YOUR CODE
:DELZIPIF
set /p DESTROY="Please Y for Yes or N for No. "
IF %DESTROY%==N GOTO EXIT1
IF %DESTROY%==n GOTO EXIT1
IF %DESTROY%==Y GOTO DESTROYZIP
IF %DESTROY%==y GOTO DESTROYZIP
IF NOT %DESTROY%==N GOTO DELZIPIF
IF NOT %DESTROY%==n GOTO DELZIPIF
IF NOT %DESTROY%==Y GOTO DELZIPIF
IF NOT %DESTROY%==y GOTO DELZIPIF
This should be changed to:
:DELZIPIF
set option= <-- must always clear previous option first
set /p option=Please confirm [y/n]: <-- no need for double-quotes. Include a space after the colon
if /i "%option%" equ "y" goto destroyzip <-- use the '/i' switch to ignore case. Can use 'equ' instead of '=='
if /i "%option%" equ "n" goto exit1
goto delzipif <-- use indentation for clarity.
Furtermore, the following line contains tabs:
ECHO. >> "%userprofile%\desktop\rac
There's no purpose for that whatsoever. This should be:
ECHO. >>"%userprofile%\desktop\rac
To close this comment (I will return later), I personally feel the Notepad thing is rather cheesy. Again, you're leaving it to the user to decide whether an error has occured. The only possibly appendage to the log file where errors are concerned is with the 7zip command. Personally, I would get the batch file to test for these errors instead and trap them. Sure, a message could still be displayed to the user and an entry written to a log file however, if you're aware of the types of errors that are likely to occur then your batch file could take appropriate action to recover from it. Again, this is an area which needs consideration.
The less interaction the user has with the batch file the less is likely to go wrong. As for asking a user to type in a password for a job - well, my mind boggles....
Please reply if you feel you feel some of these suggestions are worth considering...
By the way, the IF statement above could also be written as:
:DELZIPIF
set option=
set /p option=Please confirm [y/n]:
if /i "%option%" equ "y" (
goto destroyzip
) else (
if /i "%option%" equ "n" (
goto exit1
)
)
goto delzipif
t0t0. Thanks for the very detailed reply. That's quite a bit of information to take in and a lot to think about. For starters, I'd like to say I definitely like the ideas that you've got. The reason I haven't thought of them myself is because honestly I just don't understand the batch language enough to really know what all it can do. For instance, that first bit of code you wrote me about generating passwords/sentinel files; I really don't understand it at all. I get lost when I see things like:
copy nul sentinel>nul
and
start cmd /c %~dpnx0
I like the idea of generating a password for the user and even keeping a log of those passwords, or perhaps placing the single password in a text on her desktop so she can easily access it until she starts a new batch which would then delete the old file/password.
The process I'm trying to accomplish is as follows so that might help you better understand why I'm doing what I'm doing: The user will take a medical record and save it to PDF. She will then ZIP the PDF and encrypt it. Then she'll burn the encrypted ZIP to a CD and mail it to a company called RACS. She will then EMAIL them the password so when they receive the CD they can open the file. As far as I know, at this point she can discard the password from her personal records as RACS will have what they need. That is why I've let her make up the password and why it can change every time, but it would be great to have it force her to use a secure method of creating passwords and not having to memorize them.
Thanks for the code rearrangement yet again on the IF statements. What you gave me was much shorter and works just as well, and I should have seen that to start with. One thing I don't understand why we need is:
set option= <-- must always clear previous option first
I believe you are saying this sets the variable back to NUL every time, but I tested the code with and without it and it acts the same way, so it seems unnecessary.
I'm reattaching my latest code so you can see the changes I've made to it (changed the names of some of the variables/sections) and incorporated yours and Takeda's ideas. I used == instead of equ just because I'm used to it.
Also, the reason I used:
ECHO. >> "%userprofile%\desktop\rac
was simply to line up all the log entries to make it easier to understand what was going to log and what wasn't. I understand that it doesn't need the spaces, but also that the spaces don't cause any problems, so it was just for me to see and edit more easily. Thanks again for all the ideas and I'd like to try the password generator idea, and even the sentinel file would be cool. Because this really is overkill for this small task this lady will be doing, I might be limited on the amount of time my boss will let me keep playing with the file, but I'd like to learn what you have to show me for my personal knowledge base as well. If it would be better to start emailing back and forth we can do that, but coming here does keep everything together and I can publish it to my knowledge base when we are finished.
Thanks.
Just checking to see if you still plan to reply or if you want me to just close the question. I understand you are busy. I've got enough to do what I need but if you still want to try to help me further that's fine. If it would be easier for you to email me at a later date we could do something like that. Thanks.
sjl198
I understand.... I've looked at your code above and I have noted improvements where changes have been made.
If the following lines are merely to line up the output log file in your batch file for easier reading:
c:\7z\7za.exe a c:\RACS\RACS.zip c:\RACS\ -p%PWD% >> "%userprofile%\desktop\rac
ECHO. >> "%userprofile%\desktop\rac
ECHO. >> "%userprofile%\desktop\rac
ECHO. >> "%userprofile%\desktop\rac
ECHO. >> "%userprofile%\desktop\rac
then please consider the following code instead:
>>"%userprofile%\desktop\rac
>>"%userprofile%\desktop\rac
>>"%userprofile%\desktop\rac
>>"%userprofile%\desktop\rac
>>"%userprofile%\desktop\rac
or better still, this code instead:
set racslog="%userprofile%\des
:
:: etc...
:
>>racslog c:\7z\7za.exe a c:\RACS\RACS.zip c:\RACS\ -p%PWD%
>>racslog ECHO.
>>racslog ECHO.
>>racslog ECHO.
>>racslog ECHO.
Here's yet another method:
set racslog="%userprofile%\des
:
:: etc...
:
(
c:\7z\7za.exe a c:\RACS\RACS.zip c:\RACS\ -p%PWD%
ECHO.
ECHO.
ECHO.
ECHO.
) >>racslog
or just:
(
c:\7z\7za.exe a c:\RACS\RACS.zip c:\RACS\ -p%PWD%
ECHO.
ECHO.
ECHO.
ECHO.
) >>"%userprofile%\desktop\rac
As you can see, there's no single method however, you should choose one you're comfortable with and be consistent in your code.
Another area which merits a mention is the following section of code:
:DELZIPIF
set /p DEL=Please press Y for Yes or N for No.
IF /i "%DEL%"=="y" GOTO DELZIP
IF /i "%DEL%"=="n" GOTO EXIT1
GOTO DELZIPIF
For now, I'll stick to using '==' instead of 'EQU'. The formal way to write this code is something like this:
:delzipif
SET yn=
SET /P yn=Would you like continue [Y/N]:
IF /I "%yn%"=="y" (
GOTO delzip
) ELSE (
IF /I "%yn%"=="n" (
GOTO exit1
) ELSE (
GOTO delzipif
)
)
You could even consider the following instead:
:delzipif
SET yn=
SET /P yn=Would you like continue [Y/N]:
IF DEFINED yn (
IF /I %yn%==y (
GOTO delzip
) ELSE (
IF /I %yn%==n (
GOTO exit1
) ELSE (
GOTO delzipif
)
)
) ELSE (
GOTO delzipif
)
Admittedly, the following is shorter, easier to read and works just as well:
:delzipif
SET /P yn=
SET /P yn=Would you like to continue [Y/N]:
IF /I "%yn%"=="y" GOTO delzip
IF /I "%yn%"=="n" GOTO exit1
GOTO delzipif
The reason for using 'SET yn=' before using 'SET /P yn=Enter [Y/N]: ' (or whatever) is, if the variable 'yn' has been previously set by an earlier section of code, then merely pressing the ENTER key without actually typing anything would set 'yn' to whatever it's previous value was - so, if the cureent value of 'yn' is 'Y' then simply pressing the ENTER key at 'SET /P yn=Enter [Y/N]: ' would fool the logic of your code into believing the user had entered a 'Y'.
Hey t0t0. Thanks for very useful information. I was unaware that you can send text to a log file in so many ways. Several of those ways are much easier than the one I was using with all the tabs. I will be sure to remember that.
Also, thanks for the explanation on the reason for setting the variable back to nul blank before re-using it. That makes sense.
Do you still want to work on the sentinel file and automatic password generation that you spoke to me about before? It isn't absolutely necessary for this project, but it would be a nice addition and I'd love the knowledge just for future reference. This is definitely a knowledge base article already.
Business Accounts
Answer for Membership
by: t0t0Posted on 2009-09-09 at 06:59:57ID: 25291348
Yep, when the batch file runs, it looks for a special file that contains a pssword. If it doesn't find it (for example if the batch file is run for the first time) then the user is prompted to enter a password. This password is then saved to the special file so that next time the batch file is run, the batch file will retrieve the password from the file rather than ask the user to enter it again - providing the special file can be found.
The only problem with this is that the password is actually saved to a disk file and this might pose a security risk.
Here goes anyway...
if exist file.pwd (
set /p pwd=<file.pwd
) else (
set /p pwd=Please enter password for 7zip application:
echo %pwd%>file.pwd
)
The variable %pwd% is now set to the password