Compress to "FileName.xpi"

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Yaron
Posts: 14
Joined: 02 Apr 2014 21:48

Compress to "FileName.xpi"

#1 Post by Yaron » 02 Apr 2014 22:22

Hello,


I use WinRar.

In Options > Settings > Integration > User defined archive extensions, I've associated XPI and JA files (Firefox add-ons and omni.ja) with the program.

I would like to add the following entries to Windows Explorer's context menu:

1) Compress to "FileName.xpi"
2) Compress to "FileName.ja"

I'd like the entries to appear below the default "Compress to 'FileName.zip'", and also the compression format to be in my zip settings.

*

I've googled and created a batch file. For the time being, the file is placed in 'Send to'.

If I use the following code:

Code: Select all

for %%* in (.) do set CurrentFolder=%%~n*
"C:\Program Files\WinRar\WinRar" a -afzip -r "%CurrentFolder%.xpi"


The file name is that of the current folder (correct).
All files and folders are archived whether I select one item or multiple items (wrong).

If I use the following code:

Code: Select all

set file=%~f1
"C:\Program Files\WinRar\WinRar" a -afzip "%file:~0,-4%.xpi" %1


The file name is that of the file on which I right-click (correct).
Only that file is archived even if multiple files are selected (wrong).
If I right-click a folder, the code should be different (wrong).

I'm not familiar with batch coding.
How can I know if a single item is selected or more?
What's the condition syntax?

(Win 7, 32 bit).

I'd appreciate your help.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Compress to "FileName.xpi"

#2 Post by foxidrive » 02 Apr 2014 23:16

EDITED:

When dragging items then the full drive:\path\filenameORfolder is given to the batch file as a command line.

When selecting more than one item, they are all added onto the command line.


You can use this batch code as a testing tool - to show you what is dragged onto the batch file.

Code: Select all

@echo off
:loop
echo "%~1"
if exist "%~1\" (
     echo "%~1" is a folder
  ) else (
     echo "%~1" is a file
 )
shift
if not "%~1"=="" goto :loop
pause

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Compress to "FileName.xpi"

#3 Post by Squashman » 03 Apr 2014 06:20

Yaron, Welcome to DosTips. Saw your post on Tech Guy but didn't get to it last night. If you post batch files on TechGuy try and put them in the DOS or Software Development Categories.

Matt Williamson
Posts: 82
Joined: 30 Dec 2013 10:16
Location: United States by the big waterfall

Re: Compress to "FileName.xpi"

#4 Post by Matt Williamson » 03 Apr 2014 06:28

This isn't working for me Foxi. When I drag a file or multiple files from a different folder, it works. Folders, single files from same folder and multiple files from the same folder do not.

Single File from same folder

Code: Select all

"U:\Scripts\Batch\test_config1.xml"
"U:\Scripts\Batch\test_config1.xml" is a folder
"U:\Scripts\Batch\test_config1.xml" is a file
Press any key to continue . . .


Single File from different folder

Code: Select all

"C:\Temp\yesterday.cmd"
"C:\Temp\yesterday.cmd" is a file
Press any key to continue . . .


Folder from same folder

Code: Select all

"U:\Scripts\Batch\AasciniHTMLTools"
"U:\Scripts\Batch\AasciniHTMLTools" is a folder
"U:\Scripts\Batch\AasciniHTMLTools" is a file
Press any key to continue . . .


Folder from a different Folder

Code: Select all

"C:\Temp\Office Install"
"C:\Temp\Office Install" is a folder
"C:\Temp\Office Install" is a file
Press any key to continue . . .


Multiple files from a different folder

Code: Select all

"C:\Temp\apple.doc"
"C:\Temp\apple.doc" is a file
"C:\Temp\apple.docx"
"C:\Temp\apple.docx" is a file
"C:\Temp\apple2.doc"
"C:\Temp\apple2.doc" is a file
Press any key to continue . . .

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Compress to "FileName.xpi"

#5 Post by Squashman » 03 Apr 2014 06:53

Only fails on the Folders for me.
Windows 7 64bit.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Compress to "FileName.xpi"

#6 Post by foxidrive » 03 Apr 2014 06:55

Thanks guys.

It needs to test for the folder and then branch, or to use an ELSE clause which is what I put in my edited post above.

Matt Williamson
Posts: 82
Joined: 30 Dec 2013 10:16
Location: United States by the big waterfall

Re: Compress to "FileName.xpi"

#7 Post by Matt Williamson » 03 Apr 2014 07:19

Better but I'm still getting "Is a folder" when dragging multiple files from the same dir as the script. Everything else is working.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Compress to "FileName.xpi"

#8 Post by Squashman » 03 Apr 2014 07:56

Matt Williamson wrote:Better but I'm still getting "Is a folder" when dragging multiple files from the same dir as the script. Everything else is working.

Would help to know what version of Windows you are using.

Matt Williamson
Posts: 82
Joined: 30 Dec 2013 10:16
Location: United States by the big waterfall

Re: Compress to "FileName.xpi"

#9 Post by Matt Williamson » 03 Apr 2014 09:35

Win7 pro x64 Running from a network share drive.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Compress to "FileName.xpi"

#10 Post by foxidrive » 03 Apr 2014 09:43

It works in Win 8.1 here

Code: Select all

"D:\abc\input.txt"
"D:\abc\input.txt" is a file
"D:\abc\y.txt"
"D:\abc\y.txt" is a file
"D:\abc\g.txt"
"D:\abc\g.txt" is a file
Press any key to continue . . .


Are you in a mapped drive or dragging from a network share, Matt? Can you give further details?

Yaron
Posts: 14
Joined: 02 Apr 2014 21:48

Re: Compress to "FileName.xpi"

#11 Post by Yaron » 03 Apr 2014 10:09

foxidrive wrote:EDITED:

Code: Select all

@echo off
:loop
echo "%~1"
if exist "%~1\" (
     echo "%~1" is a folder
  ) else (
     echo "%~1" is a file
 )
shift
if not "%~1"=="" goto :loop
pause

Thank you foxidrive. I appreciate your help.
With your permission, two more questions:

1)

Code: Select all

for %%* in (.) do set CurrentFolder=%%~n*

If the folder name is Test.Test1, I only get "Test". How can I get "Test.Test1"?


2)

Code: Select all

setlocal EnableDelayedExpansion
@echo off
set /a counter=1
:loop
echo !counter!
set /a counter=!counter!+1
shift
if not "%~1"=="" goto :loop
pause

What's the syntax for checking if counter > 1?

Yaron
Posts: 14
Joined: 02 Apr 2014 21:48

Re: Compress to "FileName.xpi"

#12 Post by Yaron » 03 Apr 2014 10:12

Squashman wrote:If you post batch files on TechGuy try and put them in the DOS or Software Development Categories.

Thanks for the tip. :)

Matt Williamson
Posts: 82
Joined: 30 Dec 2013 10:16
Location: United States by the big waterfall

Re: Compress to "FileName.xpi"

#13 Post by Matt Williamson » 03 Apr 2014 11:12

It's a mapped drive. I tested it from a UNC as well.

Test from UNC

Code: Select all

'\\poldataserv\p16\users\williamsonm\Scripts\Batch'
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported.  Defaulting to Windows directory.
"\\poldataserv\p16\users\williamsonm\Scripts\Batch\ActivateWindow.cmd"
"\\poldataserv\p16\users\williamsonm\Scripts\Batch\ActivateWindow.cmd" is a folder
"\\poldataserv\p16\users\williamsonm\Scripts\Batch\addPath.cmd"
"\\poldataserv\p16\users\williamsonm\Scripts\Batch\addPath.cmd" is a folder
"\\poldataserv\p16\users\williamsonm\Scripts\Batch\AddPrinters.cmd"
"\\poldataserv\p16\users\williamsonm\Scripts\Batch\AddPrinters.cmd" is a folder
Press any key to continue . . .


Test from mapped drive to same location

Code: Select all

"U:\Scripts\Batch\addPath.cmd"
"U:\Scripts\Batch\addPath.cmd" is a folder
"U:\Scripts\Batch\AddPrinters.cmd"
"U:\Scripts\Batch\AddPrinters.cmd" is a folder
"U:\Scripts\Batch\AddPrinterSingle.cmd"
"U:\Scripts\Batch\AddPrinterSingle.cmd" is a folder
Press any key to continue . . .


I've also copied the script locally and it works in all scenarios.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Compress to "FileName.xpi"

#14 Post by foxidrive » 03 Apr 2014 11:33

Yaron wrote:1)

Code: Select all

for %%* in (.) do set CurrentFolder=%%~n*

If the folder name is Test.Test1, I only get "Test". How can I get "Test.Test1"?


Use this. Avoid using * or ? as for loop metavariables - stick to letters is my advice.

Code: Select all

for %%a in (.) do set CurrentFolder=%%~nxa


2)

Code: Select all

setlocal EnableDelayedExpansion
@echo off
set /a counter=1
:loop
echo !counter!
set /a counter=!counter!+1
shift
if not "%~1"=="" goto :loop
pause

What's the syntax for checking if counter > 1?


Code: Select all

if !counter! GTR 1 echo it's over one


See IF /?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Compress to "FileName.xpi"

#15 Post by foxidrive » 03 Apr 2014 11:36

Matt, what does this return from a cmd prompt? I am assuming that it is indeed a file and not a folder with a .cmd in the name.

I don't have a convenient LAN atm to test with.

Code: Select all

@echo off
if exist "\\poldataserv\p16\users\williamsonm\Scripts\Batch\AddPrinters.cmd" echo one
if exist "\\poldataserv\p16\users\williamsonm\Scripts\Batch\AddPrinters.cmd\" echo two

Post Reply