Page 1 of 1

[Solved] How to escape special characters on InputFile with Drag and Drop ?

Posted: 05 Oct 2019 04:30
by Hackoo
Hi :wink:
The goal of this code is to drag and drop a file onto the script and it encodes or decodes it in Base64 with Certutil Command.
So my question is : "What can i do to escape special characters when a file to be encoded or decoded have them ?"
Thank you !

Code: Select all

@echo off
Set "file=%~1"
If "%file%"=="" GOTO:EOF
>nul find "-----BEGIN CERTIFICATE-----" "%file%" && (
 certutil -f -v -decode "%file%" "%file%"
) || (
 certutil -f -v -encode "%file%" "%file%"
)
@EXIT

Re: How to escape special characters on InputFile with Drag and Drop ?

Posted: 05 Oct 2019 06:15
by aGerman
Could you provide the absolute path of a file where you are facing an error?

The only real problems I can think of are
a) The ampersand bug of cmd.exe. It occurs if a path contains an ampersand but no space that triggers quoting of the path. You would need a complicated processing of the cmdcmdline variable, but still the process would be in an error state and not stable.
b) Characters that are not representable in the console codepage you have set.

Steffen

Re: How to escape special characters on InputFile with Drag and Drop ?

Posted: 05 Oct 2019 06:26
by Hackoo
Exactly This & make the script down !
If i have for example a file named Key&.txt the script dosen't works !

Re: How to escape special characters on InputFile with Drag and Drop ?

Posted: 05 Oct 2019 06:36
by aGerman
As I said, even that should work as long as you have a space somewhere in the path. Otherwise it's getting complicated.

Code: Select all

@echo off &setlocal DisableDelayedExpansion

setlocal EnableDelayedExpansion
set "params=!cmdcmdline:~,-1!"
set "params=!params:*" =!"

if "!params!"=="" exit

endlocal&set "params=%params:"=""%"
set "params=%params:^=^^%"
set "params=%params:&=^&%"
set "params=%params: =^ ^ %"
set params=%params:""="%
set "params=%params:"=""Q%"
set "params=%params:  ="S"S%"
set "params=%params:^ ^ = %"
set "params=%params:""="%"

setlocal EnableDelayedExpansion
set "params=!params:"Q=!"

for %%i in ("!params:"S"S=" "!") do (
  if "!!"=="" endlocal
  REM only files ...
  if not exist "%%~i\" (
    set "file=%%~i"
    call :proc
  )
)

echo ready.
pause
exit


:proc
echo process "%file%" here ...
goto :eof
Exceptionally you need an EXIT in this code to suppress the error that still exists under the hood if the ampersand bug occurs.

This script is based on algorithms other people discovered, such like
viewtopic.php?f=3&t=5479
https://stackoverflow.com/questions/124 ... 80#5370380
https://stackoverflow.com/questions/547 ... 44#7940444

Steffen

Re: How to escape special characters on InputFile with Drag and Drop ?

Posted: 05 Oct 2019 07:46
by aGerman
Now that koko reminds me ... there is a recent thread about parsing of cmdcmdline. viewtopic.php?f=3&t=9285 I didn't do any examination of the code yet. But maybe you could give some feedback of how it works for you.

Steffen

Re: How to escape special characters on InputFile with Drag and Drop ?

Posted: 05 Oct 2019 09:45
by Hackoo
Thank you for your helpful answer :wink:
The problem is solved :wink: :mrgreen: