简体   繁体   中英

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

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.

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM