7-Zip - How do I create a list file %UF with quotes inside the list?

English support forum

Moderators: white, Hacker, petermad, Stefan2

Post Reply
PiotrMPPL
Junior Member
Junior Member
Posts: 44
Joined: 2018-10-26, 07:04 UTC

7-Zip - How do I create a list file %UF with quotes inside the list?

Post by *PiotrMPPL »

Hi

How do I create a list file %UF with quotes inside the list?

" test1\"
" image.jpg"

Igor Pavlov wrote that 7-Zip requires a quoted list if filenames start with a space.
Igor Pavlov:

Now 7-Zip removes spaces from left and from right in lines of list file.
Note that spaces at right without quotes can be problem, when user can insert such space by mistake, and user doesn't see that space in text editor.
Space at left is not big problem, and user sees that space, but 7-Zip removes also these left spaces for simmetry.
Please help me
User avatar
Stefan2
Power Member
Power Member
Posts: 4157
Joined: 2007-09-13, 22:20 UTC
Location: Europa

Re: 7-Zip - How do I create a list file %UF with quotes inside the list?

Post by *Stefan2 »

 
How do I create a list file %UF with quotes inside the list?
I don't know.


Didn't that just work with @listfile >>like>>"@"%UF" ?

7za <command> [<switches>...] <archive_name> @"%UF"

Note that the length for the whole <file_names> string is limited any way.
To 2000 or 8000 signs, see https://support.microsoft.com/en-us/help/830473.
7za <command> [<switches>...] <archive_name> [<file_names>...]
That's why there is the @listfile parameter of 7zip.


- - -


How do I use the list file %UF with quotes inside the list?
Maybe I would do it like this:


If one really need to quote each filename on the fly, you have to use a script and quote all on your own:
For all lines in "%UF" do LINE= "+LINE+"


For example with Powershell, all in one button

TEST:
Command: powershell -NoExit
Parameters: Get-Content "%UF" | ForEach{ $_ ; '"""' + $_+ '"""' }
(((Note that is ' " " " ' for use in TC button)))


Collect all lines:
Command: powershell -NoExit
Parameters: Get-Content "%UF" | ForEach{} {$List += '"""' + $_+ '""" ' } {$List}
(((Note the space in '""" ' )))


Collect all lines and individual program syntax, here for 7zip:
Parameters: Get-Content "%UF" | ForEach{} {$List += '"""' + $_.TRIM() + '""" ' } {7za <command> [<switches>...] <archive_name> $List }
(((Explanation: {FirstProcessBlock; one time at "ForEach"-start} {MiddleProcessBlock; for each and every file} {LastProcessBlock; one time at the end after the last file})))

Do that work?

- - -

"Trick" from elgonzo, escape the doublequote to not have the need to triple the doublequote -sign:
Parameters: Get-Content "%UF" | ForEach{} {$List += '\"' + $_+ '\" ' } {$List}
(((Note that is ' \" ' for use in TC button)))

- - -





For others:

TOTALCMD.CHM > 3. Operation > a. User interface >> (Button bar) > "Dialog box to change"

%L, %l, %F, %f, %D, %d, %WL, %WF, %UL, %UF : create a list file in the TEMP directory with the names of the selected files and directories

%L : Long file names including the complete path, e.g. c:\Program Files\Long name.exe
%l (lowercase L) : Short file names including the complete path, e.g. C:\PROGRA~1\LONGNA~1.EXE
%F : Long file names without path, e.g. Long name.exe
%UL,%UF : like %L and %F, but with UTF-8 Unicode list file (with byte order mark)


TC provides you a list of selected files and folders.
Next use a script (VBS,PowerShell,JS, AutoHotkey) to parse that list line-by-line and for each line add quotes to that line yourself, next use that as parameter to call your wanted program.



 
User avatar
Horst.Epp
Power Member
Power Member
Posts: 6489
Joined: 2003-02-06, 17:36 UTC
Location: Germany

Re: 7-Zip - How do I create a list file %UF with quotes inside the list?

Post by *Horst.Epp »

I use the %UL parameter with 7zip to create an archive from the TC selection.
There is no need for any manipulation of the file list and it works with spaces inside of file names.
But it doesn't work with spaces at the beginning of file names
which I never would use as it gives all sorts of problems with scripts and on command lines.
Windows 11 Home x64 Version 23H2 (OS Build 22631.3447)
TC 11.03 x64 / x86
Everything 1.5.0.1372a (x64), Everything Toolbar 1.3.3, Listary Pro 6.3.0.73
QAP 11.6.3.2 x64
PiotrMPPL
Junior Member
Junior Member
Posts: 44
Joined: 2018-10-26, 07:04 UTC

Re: 7-Zip - How do I create a list file %UF with quotes inside the list?

Post by *PiotrMPPL »

Try to archive 7-Zip using the file list %UL, directory or file with a name starting with a space.

oops...
User avatar
Horst.Epp
Power Member
Power Member
Posts: 6489
Joined: 2003-02-06, 17:36 UTC
Location: Germany

Re: 7-Zip - How do I create a list file %UF with quotes inside the list?

Post by *Horst.Epp »

PiotrMPPL wrote: 2020-09-02, 15:43 UTC Try to archive 7-Zip using the file list %UL, directory or file with a name starting with a space.

oops...
I know, 7zip just ignores such entries.
But as I said, to much problems on other places with such contructs
and therefore better to avoid.
I don't see any real benefit or need for it.
Windows 11 Home x64 Version 23H2 (OS Build 22631.3447)
TC 11.03 x64 / x86
Everything 1.5.0.1372a (x64), Everything Toolbar 1.3.3, Listary Pro 6.3.0.73
QAP 11.6.3.2 x64
gdpr deleted 6
Power Member
Power Member
Posts: 872
Joined: 2013-09-04, 14:07 UTC

Re: 7-Zip - How do I create a list file %UF with quotes inside the list?

Post by *gdpr deleted 6 »

Disclaimer: While i am offering a possible approach here, note that i strongly agree with Horst.Epp, and like to discourage you from utilizing such file names. But then again, it is not me who risks wallowing in pain when faced with other problems caused by such unusual file names down the road, so why should i care? And if you have no other choice than to work with such file names, well, you will have to endure... :P

It is also worth menitoning that i only address the issue of quoted items in the list file here. I haven't tested with file names starting with a white-space, so i can't say whether quoting such file names in the list file will make 7z work as desired. That's up to you to test and figure out...



Unless you are running a very old Windows OS, you can utilize a Powershell script to preprocess the file list and surround the items within it with double quotes. Here the "pure", readable PS script:

Code: Select all

$l = Get-Content <PathToListFile>
$l | ForEach-Object {$_ -replace '^"?|(?<!")$', '"'} | Set-Content <PathToListFile>
& <PathTo7z.exe> a <PathArchiveFile> @<PathToListFile>
(The arguments with <...> are just placeholders for the actual values that have to be inserted there)

The script is rather straight-forward. It reads the content of the list file into a variable ($l). Then it goes through the items of the list, doing some regex replacement (which there is a particular reason of why it looks that way; explanation below) to make sure each item is being surrounded by double-quotes and writes the items back into the same list file (overwriting it). Finally, it starts the 7z process.

Now, you either can turn this into a script file. Or, if you don't like having a separate script file lying around, you can pass these script commands as part of Powershell.exe's command line and thus encapsulating the whole shebang within the parameter field of a TC button.

I am going to do the latter and encapsulate the script in the parameter field of a TC button. Not because it is fun, but because of Windows' mildy flustercucked command line processor (which it is since decades, and everyone has to deal with it).

The button should be setup like this:
Command:

Code: Select all

powershell.exe
Parameters:

Code: Select all

-Command "&{$l = Get-Content '%UL'; $l | ForEach-Object {$_ -replace '^\"?\"?|(?<!\")$', '\"'} | Set-Content '%UL'; & '<PathTo7z.exe>' a '<PathArchiveFile>' '@%UL'}"
Note me using TC's variable %UL in place of <PathToListFile>.

Don't forget to replace <PathArchiveFile> with the path of the target archive to create/to be updated, and <PathTo7z.exe> with the real path to your 7z.exe. If the path contains spaces, do not use double-quotes here. Instead, keep the single-quotes i have already there in the script. (Powershell itself will recognize the single-quoted string containing spaces correctly. And by using single-quotes you don't risk upsetting the command-line processor.)

You might also want to modify the arguments for the 7z.ex invocation according to your needs. If you need to quote some argument because it contains a space, just use single-quotes within the script, not double-quotes. Not only do single-quotes not interfere with the command line processor, but in Powershell a single-quoted string is different from a double-quoted string. A double-quoted string in Powershell will also do variable expansion. Variables in PS begin with a "$" character. So, if by chance any of your arguments happen to contain a "$" character, double-quoting such an argument will trigger an variable expansion attempt, and thus quite likely changing the argument in unforeseen ways.

Okay. That doesn't look as tidy as the pure script above. It looks rather messy. Well, that's life. If you are interested in some explanations, read on. If not, you can tune out now, just use what i wrote above, and pray that i didn't make any mistake.




You are still here? Alright... To pass the script to Powershell.exe, i'll use this argument syntax:

Code: Select all

powershell.exe -Command "&{Script}"
The obvious problem of passing the script as part of the command line for Powershell is two-fold here with my script. It uses double-quote, circumflex (^) and pipe (|) and redirection (<) characters that normally are processed as part of the command line handling outside of double-quoted argument strings. Additionally, if you count the double-quotes in the script, you will notice the odd/uneven count. This imbalance can and will screw up the command line processing if not addressed. (Oh, and for whatever reason you will need to escape the double-quotes within the script with backslashes when passing the script via command line, otherwise Powershell will protest. Don't ask me why, i have no enthusiasm to figure out the reasons behind this :P )

To balance the double-quotes, i will abuse the regex '^"?|(?<!")$' in my script (that is actually the "sneaky" reason why i employed a regex). This regex pattern simply matches the beginning of the string including an optional double-quote directly at the beginning of the string, or it matches the end of the string if the last character in the string is not a double-quote. Together with the replacement you can see in the script, this will result in a string always having a double-quote at the beginning and end. Now, we can keep this regex replacement essentially working the same by just expanding the regex pattern to match a (pointless) second optional double-quote at the beginning of the string: '^"?"?|(?<!")$'. Slightly stupid, but it adds a double-quote character to the script, and now the script features an even number of double-quote characters. Yay, the command line processor is happy now. (Dear reader, would you like an Aspirin?)

On to the circumflex and the pipe characters. Luckily, we get lucky here. If you follow the sequence of opening double-quotes to the next closing double-quote and so on, you'll notice that both the circumflex and the pipe characters are enclosed within double-quoted blocks as far as the command line processor is concerned. This will make the command line processor happy and we don't need to escape the circumflex and the pipe characters. Phew, dodged a bullet there...
Last edited by gdpr deleted 6 on 2020-09-04, 17:26 UTC, edited 7 times in total.
gdpr deleted 6
Power Member
Power Member
Posts: 872
Joined: 2013-09-04, 14:07 UTC

Re: 7-Zip - How do I create a list file %UF with quotes inside the list?

Post by *gdpr deleted 6 »

Small, but important correction to my previous post. My previous post has already been edited to incorporate these corrections.
The regex pattern was not entirely correct with regard to a double-quote at the end of a string.
User avatar
Stefan2
Power Member
Power Member
Posts: 4157
Joined: 2007-09-13, 22:20 UTC
Location: Europa

Re: 7-Zip - How do I create a list file %UF with quotes inside the list?

Post by *Stefan2 »

2elgonzo
As Dalai told me often, it is better to quote the paths.

As %UL is replaced by TC with the path to an temporally file in users temp folder,
and if users name contains an space... therefor its saver to quote the parameters and use ' %UL ' in single quotes.


For that I will steal that neat \" -trick, ok?

 
gdpr deleted 6
Power Member
Power Member
Posts: 872
Joined: 2013-09-04, 14:07 UTC

Re: 7-Zip - How do I create a list file %UF with quotes inside the list?

Post by *gdpr deleted 6 »

Ah, my bad. If %UL doesn't include quotes, it certainly is advised to use single-quotes there in the script. I'll go and correct the script in my post above.
(...and also add quoting for the 7z archive name inside the -ArgumentList string. Doh!)

Thanks!

(EDIT: And one more improvement to my script: Start-Process is ackshually not really necessary. Executing 7z directly from the script (using the & operator is just working fine.)
Post Reply