• BATCH > enlever les guillemets

      the simplest method is to use %~1 Parameter Extensions to remove the quotes automatically

       

         @ECHO OFF
         Set _string=%~1
         Echo the string passed to this script is %_string%

       

      (you may also want to test IF the variable is empty/NULL)

      This is equivalent to the following, which can be used to remove outer quotes from any string, not just a parameter string:

       

         :: Remove quotes
         SET _string=###%_string%###
         SET _string=%_string:"###=%
         SET _string=%_string:###"=%
         SET _string=%_string:###=%

       

      Pour enlever ALL quotes from a string, this can be done in one line with variable replace syntax:

       

         Set _somevariable=%_somevariable:"=%

       

      File and folder names cannot (legally) contain quotes so the above is often all that’s needed.

      A one line function, using a FOR command to run parameter expansion and remove quotes (the %~A removes the quotes), this is a good approach when you need to dequote multiple strings, just call the function with each string:

       

      ::::::::: one line Dequote example ::::::::::::
       @Echo Off
       Setlocal
       Set _mypath="C:\Program Files\ss64\"
       CALL :dequote _mypath
       Echo %_mypath%
       Goto :eof
      
       :DeQuote
       for /f "delims=" %%A in ('echo %%%1%%') do set %1=%%~A
       Goto :eof

       

      cela donnera"C:\Program Files\ss64\" > C:\Program Files\ss64\

       

       

      On peut écrire dans un fichier DeQuote.cmd :

       

      for /f "delims=" %%A in ('echo %%%1%%') do set %1=%%~A

       

      et appeler la fonction à partir d’un autre script :

       

      CALL DeQuote.cmd VariableName

       

      There may be cases when you only want to affect the string if it both starts and ends with a quote and raise an error or some other action if mismatched quotes are found. Some more complex scripts to handle this can be found here in the

 

Aucun commentaire

 

Laissez un commentaire