Simple password protected zip file creation on windows desktop

I'd like to create a bat file or smiler that you can drag and drop a file onto and the bat file zips it up files dropped on with a predefined password.

It would be great if I could use 7zip or similar and not have to buy a utility.

Many thanks in advance
LVL 1
Glyn MerrittIT Project ManagerAsked:
Who is Participating?
 
oBdACommented:
In case of a single file, it now still uses the original name as base, for multiple files, it uses a hard-coded prefix and then a time stamp (generated using WMI, so not depending on a certain date format like mm/dd/yyyy):
@echo off
setlocal enabledelayedexpansion
set "Password=Top & Secret"
set BaseNameMulti=Archive-
set SevenZip=%ProgramFiles%\7-Zip\7z.exe
if "%~1"=="" goto :eof
if "%~2"=="" (
	set ArchiveName=%~n1.zip
	goto SkipGetTime
)
echo Getting time ...
for /f "tokens=1-9" %%a in ('wmic Path Win32_LocalTime Get Day^,DayOfWeek^,Hour^,Minute^,Month^,Quarter^,Second^,WeekInMonth^,Year ^| find /v ""') do (
  set /a Line += 1
  if "!Line!"=="1" (set VarA=%%a&set VarB=%%b&set VarC=%%c&set VarD=%%d&set VarE=%%e&set VarF=%%f&set VarG=%%g&set VarH=%%h&set VarI=%%i)
  if "!Line!"=="2" (set !VarA!=%%a&set !VarB!=%%b&set !VarC!=%%c&set !VarD!=%%d&set !VarE!=%%e&set !VarF!=%%f&set !VarG!=%%g&set !VarH!=%%h&set !VarI!=%%i)
)
for %%a in (Month Day Hour Minute Second) do (if !%%a! LSS 10 set %%a=0!%%a!)
set TimeStamp=%Year%%Month%%Day%_%Hour%%Minute%%Second%
set ArchiveName=%BaseNameMulti%%TimeStamp%.zip
:SkipGetTime

"%SevenZip%" a -o"%~dp1" -p"%Password%" -tzip "%ArchiveName%" %*
if errorlevel 1 (
	color 4f
	pause
	goto :eof
)

Open in new window

0
 
joinaunionCommented:
0
 
Glyn MerrittIT Project ManagerAuthor Commented:
This is exactly what I want to do but with all the manual steps cut out.

The idea is that instead of having to right mouse click select add to archive change the archive to zip format and type in a password etc... they just drop the file they wish to zip and password protect onto an icon on the desktop and then it creates a zip file that is protected with a predefined password.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
oBdACommented:
Try this for starters. It creates the archive based on name and the location of the original file, with the extension .zip.
Do you need to process several files dropped at the same time as well (and if so, do you want individual archives for each file, or one archive file)?
What if a folder is dropped on the script?
@echo off
setlocal
set "Password=Top & Secret"
set SevenZip=%ProgramFiles%\7-Zip\7z.exe
if "%~1"=="" goto :eof
"%SevenZip%" a -o"%~dp1" -p"%Password%" -tzip "%~n1.zip" "%~1"
if errorlevel 1 (
	color 4f
	pause
)

Open in new window

0
 
Glyn MerrittIT Project ManagerAuthor Commented:
That looks great.

It would be superb if it could handle both single and multiple files creating just the one archive with all the dropped files contained.
0
 
oBdACommented:
How do you want the archive name generated in case of several files (note that the order in which they're passed to the script by Explorer is random)?
0
 
Glyn MerrittIT Project ManagerAuthor Commented:
I guess it would make sense to use the short date and time for the file name if possible.
0
 
oBdACommented:
Let me rephrase: if only one file is dropped, generating the archive name is obvious.
But if several files are selected in Explorer and then dropped on the batch file, the order in which they appear as arguments in the batch file is random. in this case, do you have any preference which of the files (alphabetically first one?) should be the basis for the new file name, or do you rather want a generic hard-coded name like "MultiFile-Archive.zip"?
0
 
Glyn MerrittIT Project ManagerAuthor Commented:
Hey oBdA

I was thinking the name of the archive file could be based on the current time and date at the time the archive was created if that is possible?

Many thanks,
G
0
 
Glyn MerrittIT Project ManagerAuthor Commented:
This is a stunning solution many thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.