File Name Quoting

Off-topic posts of interest to the "Everything" community.
Post Reply
therube
Posts: 4610
Joined: Thu Sep 03, 2009 6:48 pm

File Name Quoting

Post by therube »

File Name Quoting

on a drag & drop, or on a SentTo,
who controls quoting?

is it Windows, Windows shell, cmd.exe or... ?

Code: Select all

1. C:\TMP\BRU\red_blue.avi
2. C:\TMP\BRU\red_blue - eggo - -^=^33.avi
3. C:\TMP\BRU\red_blue-^=^33.avi
drag&drop or SendTo to go.bat:

Code: Select all

1. C:\TMP\BRU\red_blue.avi
2. "C:\TMP\BRU\red_blue - eggo - -^=^33.avi"
3. C:\TMP\BRU\red_blue-=33.avi
-

1. is straight forward, is not quoted
2. contains spaces, so is quoted
3. is "straight forward", is not quoted - BUT is not parsed correctly - the ^'s cause issues

-

if all were quoted, there wouldn't be any issues
how do you get all quoted, or how do you have (cmd.exe, a batch file) to not mangle things up when seeing
"illegal" characters (like, ^) - in a "simple" manner ?

hmmm... well, you can escape the ^, so ^^, so C:\TMP\BRU\red_blue-^^=^^33.avi
but that's not particularly feasible... ?
void
Developer
Posts: 15353
Joined: Fri Oct 16, 2009 11:31 pm

Re: File Name Quoting

Post by void »

Quoting is handled by the shell.
Drag & drop is badly implemented for batch files.
The names are quoted, if a space is present, but not if a special character is found, like &,;^

For spaces only in your filenames, you need to change your code only a bit.

@ECHO OFF
ECHO "%~1"
COPY "%~1" "%CD%\test\" /Y /S
MOVE "%CD%\mob\*.png" "%CD%\test\test.png"
7za u -tzip "%appdata%\.virto\pack.zip" "test" -r

%~1 expands always to an unquoted version, so can always quote them in a safe way.

"c:\Docs and sets" -> %~1 -> c:\Docs and sets -> "%~1" -> "c:\Docs and sets"
c:\Programs -> %~1 -> c:\Programs -> "%~1" -> "c:\Programs"

For more details read Drag and drop batch file for multiple files?
https://stackoverflow.com/a/14787643



A work around:


Dealing with %1, shift or %* could fail with drag&drop, because the explorer is not very smart, when it creates the command line.

Files like Cool&stuff.cue are not quoted by the explorer so you get a cmdline like
pcutmp3.bat Cool&stuff.cue

So in %1 is only Cool even in %* is only Cool, but after the pcutmp3.bat ends, cmd.exe tries to execute a stuff.cue.

To handle with this stuff you could use this, it catch all filenames by using the cmdcmdline variable.

Code: Select all

@echo off
setlocal DisableDelayedExpansion
set index=0
setlocal EnableDelayedExpansion

rem *** Take the cmd-line, remove all until the first parameter
rem *** Copy cmdcmdline without any modifications, as cmdcmdline has some strange behaviour
set "params=!cmdcmdline!"
set "params=!params:~0,-1!"
set "params=!params:*" =!"
echo params: !params!
rem Split the parameters on spaces but respect the quotes
for %%G IN (!params!) do (
    for %%# in (!index!) do (
        endlocal
        set /a index+=1
        set "item_%%#=%%~G"
        setlocal EnableDelayedExpansion
    )
)

set /a max=index-1

rem list the parameters
for /L %%n in (0,1,!max!) DO (
  echo %%n #!item_%%n!#
)
pause

REM ** The exit is important, so the cmd.exe doesn't try to execute commands after ampersands
exit
Btw. there is a line limit for drag&drop operations of ~2048 characters, in spite of the "standard" batch line limit of 8191 characters.
https://stackoverflow.com/a/5192427/463115
therube
Posts: 4610
Joined: Thu Sep 03, 2009 6:48 pm

Re: File Name Quoting

Post by therube »

Seems I never posted what "go.bat" was (even though I knew what it was).

go.bat:

Code: Select all

@echo off
echo %*
pause

Dang ^, yep caret's (not carrots).
If SendTo (or Drag & Drop) didn't eat caret's, life would be much easier ;-).
therube
Posts: 4610
Joined: Thu Sep 03, 2009 6:48 pm

Re: File Name Quoting

Post by therube »

The names are quoted, if a space is present, but not if a special character is found, like &,;^
I'm not quite sure if that is clear or if I'm misunderstanding...
if a space is present
That is pertinent.


And I'll note that similar happens with a closing-paren, ")" with a batch file as below:
(An opening-paren, "(", is immaterial.)

go.bat:

Code: Select all

	for %%i in (%*) do echo %%~i

Code: Select all

	  bad:		   ok:

	x(1).mpg	x(1) .mpg
	x(2).mpg	x(2.mpg
	x(3).mpg	x (3)testing123.mpg
And with that, I've been running the same batch file for 8 years now, & you mean to tell me that I've never used a data set like:
x(1).mpg x(2).mpg x(3).mpg...
I find that rather hard to believe.


In Everything, how do you find files that have a ')', but no <sp> in the filename?
(I came up with a number of machinations, but none were "right". Close, but not right.)

^(?!\s+$).+
& then I guess ^(?!\s+$).+\) finishes it (which I guess says "no spaces, but with a closing-paren").


One day, I ought to try to understand these lookaheads & lookbacks.
One day, I ought to try to understand batch files ;-).
NotNull
Posts: 5261
Joined: Wed May 24, 2017 9:22 pm

Re: File Name Quoting

Post by NotNull »

therube wrote: Fri Oct 14, 2022 7:51 pm In Everything, how do you find files that have a ')', but no <sp> in the filename?
Does this get the desire results?

Code: Select all

)  !" "
therube
Posts: 4610
Joined: Thu Sep 03, 2009 6:48 pm

Re: File Name Quoting

Post by therube »

By George, I think you've got it.

(What I came up with, above, is not correct.)
Which is because I didn't understand the subject line, what they were trying to accomplish: "Regex - Match String That Does Not Contain Only Spaces".


Heh. So when did ! become a NOT?
NotNull
Posts: 5261
Joined: Wed May 24, 2017 9:22 pm

Re: File Name Quoting

Post by NotNull »

therube wrote: Fri Oct 14, 2022 8:14 pm Heh. So when did ! become a NOT?
it has been this way ever since I stertad using Everything (in 2017).
(I should ask you; you're the veteran here :) )
therube
Posts: 4610
Joined: Thu Sep 03, 2009 6:48 pm

Re: File Name Quoting

Post by therube »

! == NOT
NOT sure where my brain is today, but it's probably tied up in a knot!
NotNull
Posts: 5261
Joined: Wed May 24, 2017 9:22 pm

Re: File Name Quoting

Post by NotNull »

:lol: :lol:
Post Reply