Remove single quote to pass param

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Remove single quote to pass param

#1 Post by darioit » 01 Aug 2017 04:39

Hello,
I run this batch

c:\script.bat 'D:\dir1\dir2\dir3\file'

I want to remove during execution of script.bat single quote in %1 variable like this D:\dir1\dir2\dir3\file

Sorry I try to search on site but I find only to remove double quote

Thank you in advance

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Remove single quote to pass param

#2 Post by penpen » 01 Aug 2017 05:49

This might help you ("script.bat"):

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion

set "param1=%~1"
if "%param1:~0,1%%param1:~0,1%" == "''" set "param1=%param1:~1%"
if "%param1:~-1%%param1:~-1%" == "''" set "param1=%param1:~0,-1%"
echo "%param%"

goto :eof

But you better should use doublequotes around filenames, which also has other advantages ("script.bat"):

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion

echo "%~1"

goto :eof


penpen

Edit: Changed 'echo "%param%"' to echo "%param1%".

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Remove single quote to pass param

#3 Post by darioit » 01 Aug 2017 06:07

doesn't work, result is
echo ""

I can't modify %1 string, it's called with single quote

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Remove single quote to pass param

#4 Post by penpen » 01 Aug 2017 06:18

There was a flaw in the code: Sorry for that.
I echoed "%param%" instead of "%param1%".
The above code should work now.


penpen

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Remove single quote to pass param

#5 Post by darioit » 01 Aug 2017 06:30

yes works, but can I expand "%param1%"

such this echo %~nx1

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Remove single quote to pass param

#6 Post by penpen » 01 Aug 2017 07:38

You could use a for-loop:

Code: Select all

for %%a in ("%param1%") do echo "%%~nxa"


penpen

pieh-ejdsch
Posts: 240
Joined: 04 Mar 2014 11:14
Location: germany

Re: Remove single quote to pass param

#7 Post by pieh-ejdsch » 01 Aug 2017 08:06

Hello,

ist much simplier to use:

Code: Select all

for /f usebackQdelims^= %%i in (%1) do echo %%~nxi


Help for
...
(german (XP only))
...
Wenn Sie die Option usebackq verwenden, gelten die folgenden Syntaxformen:

for /F ["usebackqAnalyseschlüsselwörter"] {%% | %}Variable in ("Dateinamensatz") do Befehl [Befehlszeilenoptionen]
for /F ["usebackqAnalyseschlüsselwörter"] {%% | %}Variable in ('LiteraleZeichenfolge') do Befehl [Befehlszeilenoptionen]
for /F ["usebackqAnalyseschlüsselwörter"] {%% | %}Variable in (`Befehl`) do Befehl [Befehlszeilenoptionen]
...

Phil

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Remove single quote to pass param

#8 Post by darioit » 01 Aug 2017 09:06

I choose this one because I need to expand %1 variable in :sub script

Code: Select all

set File=%~1
set File=%File:'=%
call :sub %File%
goto:eof
:sub

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Remove single quote to pass param

#9 Post by penpen » 01 Aug 2017 14:21

You really should think about using doublequotes around filenames (also protects from '&' characters in filenames):

Code: Select all

c:\script.bat "D:\dir1\dir2\dir3\file"

So you could use:

Code: Select all

echo "%~nx1"
call :sub "%~1"
:sub


penpen

Post Reply