7zip basic script

Discuss and share scripts and script files...
PeterH
Posts: 2826
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: 7zip basic script

Post by PeterH »

Just some hints...

Instead of
perm $p_note = """C:\Documents and Settings\Administrator\My Documents\ConTEXT\ConTEXT.exe""";
you might write
perm $p_note = '"C:\Documents and Settings\Administrator\My Documents\ConTEXT\ConTEXT.exe"';
(as long as there's no variable in the string - it wouldn't be replaced)

Why
openwith "$p_ZipFM";
Quotes are to specify a string, and *allow* to include variables. But there is no string...
So you could write
openwith $p_ZipFM;

But if you want this statement *to add* quotes around the variable, it could be
openwith """$p_ZipFM""";
or
openwith '"'.$p_ZipFM.'"';
or
openwith quote($p_ZipFM);

// Please make sure that path to 7z.exe does not contain spaces
:arrow: Why don't you just add quotes - than there might be spaces or not...
(For openwith you did that, but not for Run?)
(And for $p_note, but not for $p_zip + $pZipFM)

If you did it everywhere, you just could do it once when setting the variable, saving dozens of quotes...

Edit: above '" is a single and a double quote - sorry that it's hardly to recognize :roll:

totmad1
Posts: 131
Joined: 24 Jun 2013 12:37

Re: 7zip basic script

Post by totmad1 »

Hi PeterH
found your post most enlightening.
Totally agree about your first point it would have been easier to use the single quotes.
Chose to use tripled double quotes to try to make it easier to see , as you point out at the end.

As for other use of quotes in other places. I found that if path to 7z.exe had spaces and paths to
file also had spaces it could cause problems either in win7 or XP.

Have to admit had not seen the use of dots inside quotes used in the way you have used them.
i.e. openwith '"'.$p_ZipFM.'"';
Had seen the use of the word quote as stated by you i.e. openwith quote($p_ZipFM);
So can honestly say I will be trying these out.

One request I do have is. I see you have access to win 7 and XP. If you have time to test on both
and give me your results, I will have something to work with. Reason being I am not sure on some results I'm getting.
Sometimes I am having trouble with "self" i.e. ("path") returning ("base")

PLEASE take note line Nos. 36 50 92 107 (if ($p = "-1") { $p = $l ; } ) were used when testing (had small problem) can be commented out i.e //

totmad1 (totally mad one)
totmad1 (totally mad one)

40k
Posts: 234
Joined: 09 Dec 2011 21:25

Re: 7zip basic script

Post by 40k »

From experience I would avoid using triple quoting.

In general I have found the following modus operandi to work very well.

Use single quotes when your string does not include variables.

Code: Select all

$ZipExe = 'c:\Program Files\7-zip\7zip.exe';
Use single quotes in combination with double quotes to quote command line paths that include spaces

Code: Select all

run ('"'.$ZipExe.'" "'.$MyFile.'"', , 0, 0);
//Script engine sees this:
//"c:\Program Files\7-zip\7zip.exe" "c:\document.doc"
Use double quoting when you need to resolve variables inside a string without the requirement of having them quoted

Code: Select all

$IniFile = "<xyscript>\$Username\IniFile.ini";
In general I find the use of single quotes much easier as they ensure what you type in single quotes is what the script engine will see when you run the script. It also makes the use of special characters that require an escape sequence if you would use double quotes such as ", \, etc... easier
I develop scripts that integrate media functionality into Xyplorer.
Hash - Twitch.tv in VLC (NEW 2.0!) - FFmpeg GUI - Youtube downloading
XYplorer for Linux! Tutorial

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: 7zip basic script

Post by TheQwerty »

40k wrote:Use single quotes in combination with double quotes to quote command line paths that include spaces

Code: Select all

run ('"'.$ZipExe.'" "'.$MyFile.'"', , 0, 0);
Personally I find this more difficult to read and understand than:

Code: Select all

run("""$ZipExe"" ""$MyFile""",, 0, 0);
If I was trying to avoid escaped quotes I'd even prefer using the Quote() SC instead:

Code: Select all

run(Quote($ZipExe).' '.Quote($MyFile),, 0, 0);
That said, when building a complex string such as a command line it's probably best to consider using a HEREDOC:

Code: Select all

$cmdline = <<<CMD_LINE
"$ZipExe"
"$MyFile"
CMD_LINE;
  $cmdline = Replace($cmdline, "<crlf>", ' '); //Join lines.
  Run($cmdline, , 0, 0);
Though obviously in this case you'd need to be certain that neither $ZipExe or $MyFile could contain <crlf>.

PeterH
Posts: 2826
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: 7zip basic script

Post by PeterH »

Shorter:

Code: Select all

$cmdline = <<<CMD_LINE
"$ZipExe" "$MyFile"
CMD_LINE;
  Run($cmdline, , 0, 0);
:?:

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: 7zip basic script

Post by TheQwerty »

PeterH wrote:Shorter:

Code: Select all

$cmdline = <<<CMD_LINE
"$ZipExe" "$MyFile"
CMD_LINE;
  Run($cmdline, , 0, 0);
:?:
Yes the example is a simple one.. I was trying to show that for more complex ones it might be preferable to handle the arguments one per a line.

EDIT: To expand on the whys...
  1. I feel this makes it easier to know the number of arguments and their index/positions.
  2. It's easier to pick out which argument(s) are missing quotes.
  3. It keeps your lines shorter.

PeterH
Posts: 2826
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: 7zip basic script

Post by PeterH »

Aaah :shock:

What I thought about was to avoid doubling quotes by use of heredoc.
While I think breaking operands to multi lines is a matter of taste: don't think I would do that.

totmad1
Posts: 131
Joined: 24 Jun 2013 12:37

Re: 7zip basic script

Post by totmad1 »

I am thankful for the previous posts from PeterH , 40k and TheQwerty.
I had not been satisfied completely with what I had achieved,
so after further attempts at trying to get better results, I finally arrived at
the examples shown as follows. Of course the main aim was to reduce the risks concerning
spaces in paths. At end of post are the full scripts one using single quotes named "7zip2013b.xys"
and one using the Heredoc method named "7zip2013c.xys".

In conclusion the three different methods all have their pros and cons.
Method 1 (all double quotes) hard to get to work with spaces in paths.
Method 2 (single & double quotes) easier.
Method 3 (Heredoc) easiest to use but found had to juggle switches.(not a big problem).

Code: Select all

"_Initialize";
  //this has been tested on win7 64bit  and XP
        perm $p_zip = '"C:\Users\user\Documents\Virtual Machines\zTemp\7-Zip\7z.exe"' ;
        perm $p_ZipFM ="C:\Users\user\Documents\Virtual Machines\zTemp\7-Zip\7zFM.exe";
        perm $p_note  = '"C:\Users\user\Documents\Virtual Machines\zTemp\ConTEXT\ConTEXT.exe"';

"&Open <curname> : open"
    openwith $p_ZipFM;
Further are examples from each section.

Code: Select all

"List  <curname> single"
        run'cmd /c "'.$p_zip.' l "'.<curitem>.'" >"'.<curitem>.".txt".'"';

-
"7-Zip and move into <curbase>.&7z and move : 7zm"
       $a =  get("path", i);
 	   openwith ''.$p_zip.' a -t7z "'.$a\.<curbase>.".7z".'"';
-
"Compress into <curbase>.&zip : zip"
     focus;
     #113;
     focus;
     $curbase = <clipboard>;
     $l = strlen ($curbase);
     $p = strpos ($curbase, <crlf>);
    $curbase = regexreplace ($curbase,"^-1$","$l");
    if ($p =="-1") { $p = $l ; }  // if single item selected
     $curbase = substr ($curbase, 0, $p);
     $curbase = replace ($curbase,"","_");
    set $curbase,"$curbase.zip";
   //Compress file with .zip. Name= base of file.
               openwith ''.$p_zip.' a -tzip "'.$curbase.'"'; 
-
"&Extract contents of <curname> to <curbase> other pane (overwrite NO warning) : extfop"
          $a =  get("path", i);
      openwith''.$p_zip.' x -y -o"'.$a\<curbase>\.'"  <items>' ;
-
"Compress into <curbase>.zi&p (password protection) : pzip"
     focus;
    #113;
    focus;
    $curbase= <clipboard>;
    $l = strlen ($curbase);
    $p = strpos ($curbase, <crlf>);
   $curbase = regexreplace ($curbase,"^-1$","$l");
     if ($p =="-1") { $p = $l ; }  // if single item selected
    $curbase = substr ($curbase, 0, $p);
    $curbase = replace ($curbase," ","_");
   set $curbase,"$curbase.zip";
    $i = input  (Input password for zipfile, password);
   //Compress with .zip and password protect archive.
     openwith''.$p_zip.' a -tzip "'.$curbase.'" -p"'.$i.'"';
-
"?Set Password to <curname> zip : zp"
         set $file, <curbase>;
         openwith''.$p_zip.' x -y "'.<items>.'"' ;
         msg"wait until extract",1;
         focus ;
         filter $file* ;
          delete 0 , 0 ;
          sortby created, d ;
          sel 1 ;
          sub pzip ;
          filter $file ; 
-
-
"Edit script : edit"
   $ScriptFile= self ("file");
	    run $p_note "$ScriptFile" ,w;
7zip2013b.xys
(5.72 KiB) Downloaded 142 times
7zip2013c.xys
(8.19 KiB) Downloaded 177 times
totmad1(totally mad one)

Update 18/07/2013

Had further thoughts on Set password to 7z. I came up with the idea of extracting to a folder named zTmp.
So produced the following code

Code: Select all

"?Set Password to <curbase> .7z(extract to zTmp) : z7"
      set $file, <curbase>; set $Path1, <curpath>;
      // change zTmp in cmd line and goto "$Path1\ to suit you
      $cmdline = <<<CMD_LINE
cmd /c "$p_zip x "<curitem>" -aou -o"zTmp\"
CMD_LINE;
         $cmdline = Replace($cmdline, "<crlf>", ' ');
          Run($cmdline, , 0, 0);
      msg"wait until extract",1;
      goto "$Path1\zTmp";
      sel a;
      sub p7z;
      msg "don't forget to move (7z'd) file,<br> then delete zTmp folder and original archive";
totmad1 (totally mad one)

Post Reply