Search code examples
windowsbatch-file7zipsendto

SendTo / Batch / 7zip


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


Solution

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