SendTo / Batch / 7zip

480 views Asked by At

I am trying to create a SendTo shortcut to a batch script that will take the selected files (in this case, jpg's), zip them with 7zip and name them the folder name (that the selected files are in) and place them in the same folder. The directory names have spaces in them too. I am real close but have hit a stump..

Here's what I've got so far..

@echo off set folder=%~dp1 if exist "%folder%- Photos.zip" del "%folder%- Photos.zip" "C:\Program Files\7-Zip\7z" a -mx9 -tzip "%folder%- Photos.zip" "%~dp1*.jpg" pause

It is doing just about everything I want, except not naming the zip file right. It always ends up "- Photos.zip" in the directory that I want.

Any help? Thanks in advance..

2

There are 2 answers

1
Marged On BEST ANSWER

You are just specifying C:\folder\ - Photos.zip but you want C:\folder\folder - Photos.zip

Try this to get the folder and filename prefix separately:

set folder=%~dp1
set filename=%~p1
set filename=%filename:~1,-1%

Couldn't try this because my tablet does not have a dosbox ;-)

Then 7z this way:

"C:\Program Files\7-Zip\7z" a -mx9 -tzip "%folder%%filename% - Photos.zip" "%~dp1*.jpg"

If you want to debug this, you should add

echo folder is %folder% >> C:\temp\batch.log
echo filename is %filename% >> C:\temp\batch.log

and investigate that file later on.

0
Jordan On

Ended up coming up with this and it works!

@echo off

set folder=%~dp1
IF %folder:~-1%==\ SET folder=%folder:~0,-1%

set name=%~p1
if %name:~-1%==\ set name=%name:~0,-1%


if exist "%folder%%name% - Photos.zip" del "%folder%%name% - Photos.zip"

"C:\Program Files\7-Zip\7z" a -mx9 -tzip "%folder%%name% - Photos.zip" "%~dp1*.jpg"