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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Hackoo
Posts: 103
Joined: 15 Apr 2014 17:59

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

#1 Post by Hackoo » 05 Oct 2019 04:30

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
Last edited by Hackoo on 05 Oct 2019 16:31, edited 1 time in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

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

#2 Post by aGerman » 05 Oct 2019 06:15

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

Hackoo
Posts: 103
Joined: 15 Apr 2014 17:59

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

#3 Post by Hackoo » 05 Oct 2019 06:26

Exactly This & make the script down !
If i have for example a file named Key&.txt the script dosen't works !

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

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

#4 Post by aGerman » 05 Oct 2019 06:36

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

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

#5 Post by aGerman » 05 Oct 2019 07:46

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

Hackoo
Posts: 103
Joined: 15 Apr 2014 17:59

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

#6 Post by Hackoo » 05 Oct 2019 09:45

Thank you for your helpful answer :wink:
The problem is solved :wink: :mrgreen:

Post Reply