Question

Batch file or VB Script- accepting user input for a 7zip command line tool.

Asked by: sjl1986

Hello. 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.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-09-09 at 06:09:57ID24717952
Tags

Batch

,

Command

,

VBS

,

DOS

,

Script

Topics

MS DOS

,

VB Script

,

File Compression Utilities

Participating Experts
3
Points
500
Comments
28

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

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.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

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.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

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.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Batch print PDF files from VB
    Hi, I want to be able to silently print batch pdf files from VB using user's default printer. Using shell "AcroRd32 -p " command opens up print dialog that I don't need, and it can print only one pdf. Thanks
  2. pdf batch printing
    i have a heap of pdf files. all within a folder of folders of folders. is there a way i can batch print the one folder and it will print all the pdfs within the other folders within it. i have tried some basic batch printing software but it only seems to do the inital folder...
  3. BATCH OR VB: SCAN HARD DRIVE FOR FILES, ZIP FOU…
    I need a batch or VB script that will do the following: *Scan a hard drive for *.CFG files; *Zip those found files; *Email the zip file to an email address. Any help would be appreciated!
  4. batch or vbs script
    I need a batch script or .vbs script which will zip a set of file shown in the pic, and zip it in the same filename in the same folder. But it should not use any third party zip applications, it should only use windows own add to zip utility. (compact.exe ) which resides in ...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

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.

Join the Community

Answers

 

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

 

by: jwarnkenPosted on 2009-09-09 at 07:00:39ID: 25291359

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

set pass=%1
zip.exe %pass% c:\temp.zip c:\temp\file.xxx

                                              
1:
2:

Select allOpen in new window

 

by: TakedaTPosted on 2009-09-09 at 07:03:51ID: 25291384

Theres a number of methods to do this.  The easiest of which is the set /p command in DOS.

set /p passwd=Enter your password here:

That would prompt the user to enter a password.  That pass would be stored in the passwd variable which is callable with %passwd%.

 

by: sjl1986Posted on 2009-09-09 at 07:19:52ID: 25291545

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!

 

by: jwarnkenPosted on 2009-09-09 at 07:43:01ID: 25291782

the %1 variable is the 1st command line option passed to the batch file
the 2nd would be %2 and so on.

zip.bat  password File1.xxx c:\temp

in zip.bat
%1 = password
%2 = File1.xxx
%3 = c:\temp

I hope that help you understand accessing the command line parameters in dos.



 

by: sjl1986Posted on 2009-09-09 at 08:38:39ID: 25292404

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?

 

by: t0t0Posted on 2009-09-09 at 08:42:05ID: 25292442

So, you want the user to supply a DIFFERENT password every time he or she runs 7zip then? Is that right?

 

by: t0t0Posted on 2009-09-09 at 08:53:44ID: 25292565

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 !!

 

by: t0t0Posted on 2009-09-09 at 09:03:18ID: 25292665

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?

 

by: TakedaTPosted on 2009-09-09 at 09:56:52ID: 25293225

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.

 

by: t0t0Posted on 2009-09-09 at 10:15:44ID: 25293442

Sorry TakedaT.

that doesn't help my understanding at all.

if somebody wouldlike to break that down to me in simple logical steps then please do so.

afterall, if the asker is to get the best service from us then we need to be certain of what he wants to do.

 

by: sjl1986Posted on 2009-09-09 at 10:17:11ID: 25293461

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.

@ ECHO OFF
ECHO.
ECHO. > 						"%userprofile%\desktop\racslog.txt"
ECHO Running 7zip to zip PDF documents.
ECHO Documents will be in C:\RACS\RACS.zip with password protection.
ECHO Documents will be in C:\RACS\RACS.zip with password protection. >> "%userprofile%\desktop\racslog.txt"
ECHO.
ECHO.
ECHO.
ECHO We need to delete the old zip file first.
ECHO.
ECHO This is the zip file from your previous RACS incident.
ECHO.
ECHO.
ECHO ***************************************************************
ECHO  IF YOU HAVE A PENDING ZIP FILE TO BURN PLEASE DO NOT CONTINUE
ECHO ***************************************************************
ECHO.
ECHO.
: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
 
:DESTROYZIP
DEL C:\RACS\*.zip /Q
ECHO.
ECHO ZIP file deleted.
 
pause
CLS
ECHO. >>						"%userprofile%\desktop\racslog.txt"
ECHO. >>						"%userprofile%\desktop\racslog.txt"
ECHO. >>						"%userprofile%\desktop\racslog.txt"
ECHO.
ECHO.
ECHO.
ECHO Please enter a password to encrypt the zipped files.
ECHO.
ECHO DO NOT forget this password.
ECHO.
set /p pwd="Please enter a password.   "
c:\7z\7z.exe a c:\RACS\RACS.zip c:\RACS\ -p%pwd% >> 	"%userprofile%\desktop\racslog.txt"
ECHO. >>						"%userprofile%\desktop\racslog.txt"
ECHO. >>						"%userprofile%\desktop\racslog.txt"
ECHO. >>						"%userprofile%\desktop\racslog.txt"
ECHO. >>						"%userprofile%\desktop\racslog.txt"
ECHO.
ECHO.
ECHO.
ECHO.
ECHO Check the report and see if any errors occurred.
ECHO.
ECHO Be certain that the correct files have been added to the zip.
ECHO.
ECHO Check for the message "Everything is OK".
ECHO.
ECHO.
ECHO.
ECHO Close the log file to continue.
ECHO.
notepad %userprofile%\desktop\racslog.txt
ECHO Moving uploaded files if yes is selected. >> 	"%userprofile%\desktop\racslog.txt"
ECHO Original files will be moved to stroage if you select YES
ECHO.
ECHO If an error occurred during the zip process select NO
ECHO.
 
:MOVEIF
set /p MOVE="Please Y for Yes or N for No.   "
IF %MOVE%==N GOTO EXIT1
IF %MOVE%==n GOTO EXIT1
IF %MOVE%==Y GOTO MOVE
IF %MOVE%==y GOTO MOVE
IF NOT %MOVE%==N GOTO MOVEIF
IF NOT %MOVE%==n GOTO MOVEIF
IF NOT %MOVE%==Y GOTO MOVEIF
IF NOT %MOVE%==y GOTO MOVEIF
 
:MOVE
move c:\RACS\*.PDF H:\RACSBACKUP			
ECHO.
ECHO.
GOTO EXIT2
 
:EXIT1
cls
ECHO.
ECHO.
ECHO.
ECHO Files were NOT zipped or moved to storage successfully
ECHO.
ECHO Please contact the IT dept.
ECHO.
pause
exit
 
:EXIT2
cls
ECHO.
ECHO.
ECHO.
ECHO Files were zipped and moved to storage successfully.
ECHO.
pause
Exit

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:

Select allOpen in new window

 

by: TakedaTPosted on 2009-09-09 at 10:40:25ID: 25293700

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.

 

by: sjl1986Posted on 2009-09-09 at 11:15:25ID: 25294025

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?

 

by: sjl1986Posted on 2009-09-09 at 11:26:30ID: 25294125

I also fixed the other IF phrases in the MOVE section to do the same thing, and added an IF phrase and a PASSWORD header at the point where a password is requested to force the user to put in a password she she can't leave it blank by entering through.

 

by: t0t0Posted on 2009-09-09 at 11:30:59ID: 25294155

Please allow me to comment later (I can't just now because I'm removing some asbestos from a bathroom and toilet ceiling) before you decide to close this question as there are a few things I'd like to add....

Please be patient...

 

by: TakedaTPosted on 2009-09-09 at 11:35:30ID: 25294206

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.

 

by: TakedaTPosted on 2009-09-09 at 11:40:02ID: 25294244

And just on a side note.  I would be happy with an even 3 way split of points.  t0t0 and jwarnken both answered your question and helped in your understanding of what you are trying to do.

 

by: sjl1986Posted on 2009-09-09 at 12:17:57ID: 25294572

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.

 

by: t0t0Posted on 2009-09-10 at 04:38:39ID: 25299172

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\racslog.txt"

There's no purpose for that whatsoever. This should be:

   ECHO. >>"%userprofile%\desktop\racslog.txt"


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

 

by: sjl1986Posted on 2009-09-10 at 06:50:11ID: 25300181

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\racslog.txt"
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.

@ ECHO OFF
ECHO.
ECHO. > 						"%userprofile%\desktop\racslog.txt"
ECHO Running 7zip to zip PDF documents.
ECHO Documents will be in C:\RACS\RACS.zip with password protection.
ECHO Documents will be in C:\RACS\RACS.zip with password protection. >> "%userprofile%\desktop\racslog.txt"
ECHO.
ECHO.
ECHO.
ECHO We need to delete the old zip file first.
ECHO.
ECHO This is the zip file from your previous RACS incident.
ECHO.
ECHO.
ECHO ***************************************************************
ECHO  IF YOU HAVE A PENDING ZIP FILE TO BURN PLEASE DO NOT CONTINUE
ECHO ***************************************************************
ECHO.
ECHO.
: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
 
pause
:DELZIP
DEL C:\RACS\*.zip /Q
ECHO.
ECHO ZIP file deleted.
 
pause
CLS
ECHO. >>						"%userprofile%\desktop\racslog.txt"
ECHO. >>						"%userprofile%\desktop\racslog.txt"
ECHO. >>						"%userprofile%\desktop\racslog.txt"
ECHO.
ECHO.
ECHO.
ECHO Please enter a password to encrypt the zipped files.
ECHO.
ECHO DO NOT forget this password.
ECHO.
:PASSWORD
set /p PWD=Please enter a password.   
IF /i "%PWD%"=="" GOTO PASSWORD
c:\7z\7za.exe a c:\RACS\RACS.zip c:\RACS\ -p%PWD% >> 	"%userprofile%\desktop\racslog.txt"
ECHO. >>						"%userprofile%\desktop\racslog.txt"
ECHO. >>						"%userprofile%\desktop\racslog.txt"
ECHO. >>						"%userprofile%\desktop\racslog.txt"
ECHO. >>						"%userprofile%\desktop\racslog.txt"
ECHO.
ECHO.
ECHO.***********************************************************************
ECHO.
ECHO Check the report and see if any errors occurred.
ECHO.
ECHO Be certain that the correct files have been added to the zip.
ECHO.
ECHO Check for the message "Everything is OK".
ECHO.
ECHO.***********************************************************************
ECHO.
ECHO Close the log file to continue.
ECHO.
notepad %userprofile%\desktop\racslog.txt
ECHO Moving uploaded files if yes is selected. >> 	"%userprofile%\desktop\racslog.txt"
ECHO Original files will be moved to storage if you select YES
ECHO.
ECHO If an error occurred during the zip process select NO
ECHO.
 
 
:MOVEIF
set /p MOVE=Please press Y for Yes or N for No.   
IF /i "%MOVE%"=="y" GOTO MOVEPDF
IF /i "%MOVE%"=="n" GOTO EXIT1
GOTO MOVEIF
 
:MOVEPDF
move c:\RACS\*.PDF H:\RACSBACKUP			
ECHO.
ECHO.
GOTO EXIT2
 
:EXIT1
cls
ECHO.
ECHO.
ECHO.
ECHO Files either were NOT zipped or moved to storage successfully
ECHO.
ECHO Please contact the IT dept.
ECHO.
pause
exit
 
:EXIT2
cls
ECHO.
ECHO.
ECHO.
ECHO Files were zipped and moved to storage successfully.
ECHO.
pause
Exit

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:

Select allOpen in new window

 

by: t0t0Posted on 2009-09-10 at 10:19:56ID: 25302321

Sorry, will reply later today...

 

by: sjl1986Posted on 2009-09-16 at 10:47:34ID: 25348133

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.

 

by: t0t0Posted on 2009-09-18 at 04:14:16ID: 25364638

Just had my internet connetction repaired (faulty cable modem knocked me out for 3 days)... will respond tonight

Thank you for your patience

 

by: sjl1986Posted on 2009-09-21 at 10:43:12ID: 25385455

I understand that. EE is asking that I close the question, so I was just checking back with you.

 

by: t0t0Posted on 2009-09-21 at 15:14:47ID: 25388178

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\racslog.txt"
   ECHO. >>                                                                                       "%userprofile%\desktop\racslog.txt"
   ECHO. >>                                                                                       "%userprofile%\desktop\racslog.txt"
   ECHO. >>                                                                                       "%userprofile%\desktop\racslog.txt"
   ECHO. >>                                                                                       "%userprofile%\desktop\racslog.txt"

then please consider the following code instead:

   >>"%userprofile%\desktop\racslog.txt" c:\7z\7za.exe a c:\RACS\RACS.zip c:\RACS\ -p%PWD%
   >>"%userprofile%\desktop\racslog.txt" ECHO.
   >>"%userprofile%\desktop\racslog.txt" ECHO.
   >>"%userprofile%\desktop\racslog.txt" ECHO.
   >>"%userprofile%\desktop\racslog.txt" ECHO.

or better still, this code instead:

   set racslog="%userprofile%\desktop\racslog.txt"
   :
   :: 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%\desktop\racslog.txt"
   :
   :: 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\racslog.txt"

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'.

 

by: sjl1986Posted on 2009-09-22 at 05:45:41ID: 25392127

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.

 

by: sjl1986Posted on 2009-09-29 at 11:34:49ID: 31626586

Thanks to everyone for help. This is definitely one for the knowledge base. I'll be referring back to it.

T0t0 if you care to contact me anymore, you can do so at sjl86 -@- bellsouth (dot) net

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...