Remove single quote to pass param
Moderator: DosItHelp
Remove single quote to pass param
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
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
Re: Remove single quote to pass param
This might help you ("script.bat"):
But you better should use doublequotes around filenames, which also has other advantages ("script.bat"):
penpen
Edit: Changed 'echo "%param%"' to echo "%param1%".
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%".
Re: Remove single quote to pass param
doesn't work, result is
echo ""
I can't modify %1 string, it's called with single quote
echo ""
I can't modify %1 string, it's called with single quote
Re: Remove single quote to pass param
There was a flaw in the code: Sorry for that.
I echoed "%param%" instead of "%param1%".
The above code should work now.
penpen
I echoed "%param%" instead of "%param1%".
The above code should work now.
penpen
Re: Remove single quote to pass param
yes works, but can I expand "%param1%"
such this echo %~nx1
such this echo %~nx1
-
- Posts: 240
- Joined: 04 Mar 2014 11:14
- Location: germany
Re: Remove single quote to pass param
Hello,
ist much simplier to use:
Phil
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
Re: Remove single quote to pass param
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
Re: Remove single quote to pass param
You really should think about using doublequotes around filenames (also protects from '&' characters in filenames):
So you could use:
penpen
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