Skip to main content

How to Remove Leading/Trailing Quotes from a String in Batch Script

When a batch script receives a file path, especially one with spaces, it's often enclosed in double quotes (e.g., "C:\My Project\data.csv"). While these quotes are necessary for the command line to correctly interpret the path as a single argument, they can become a problem inside your script. If you try to concatenate a quoted path with another string, you can end up with invalid or messy results.

This guide will teach you the essential, built-in technique for stripping these surrounding quotes using the tilde (~) modifier. You will learn how to apply it to script arguments and FOR loop variables, and understand the crucial workaround needed for regular environment variables.

The Challenge: Why Quotes Are a Problem

If a variable contains a quoted string, using it directly can lead to syntax errors or incorrect paths.

An example of a script with error: imagine a variable holds "C:\My Folder".

@ECHO OFF
SET "MyVar="C:\My Folder""

REM This creates an incorrect path with a quote in the middle.
ECHO The log file is at: %MyVar%\log.txt

Output:

The log file is at: "C:\My Folder"\log.txt
note

This is not a valid path and would cause most commands to fail. We need a way to get the clean content from inside the quotes.

The Core Method: The Tilde (~) Modifier

The solution is the tilde (~) modifier, a special character used in parameter expansion. When placed between the % and the variable identifier (a number for an argument or a letter for a FOR variable), it removes any surrounding double quotes.

  • For an argument: %~1
  • For a FOR variable: %%~F

This is the standard, built-in, and most efficient way to solve the problem.

Removing Quotes from Script Arguments (%~1)

This is the most common use case. A user might drag-and-drop a file onto your script, which passes the full, quoted path as an argument.

This script shows the difference between the raw argument (%1) and the cleaned argument (%~1).

ShowArg.bat
@ECHO OFF
ECHO The raw argument (%%1) is: %1
ECHO The cleaned argument (%%~1) is: %~1

And an example of usage and output:

C:\> ShowArg.bat "C:\Program Files\My App\run.exe"

The raw argument (%1) is: "C:\Program Files\My App\run.exe"
The cleaned argument (%~1) is: C:\Program Files\My App\run.exe
note

You can now safely use "%~1" in your script, knowing that you are working with a clean path that you are re-quoting yourself.

Removing Quotes from FOR Loop Variables (%%~F)

When a FOR loop iterates over a set of quoted strings or file paths with spaces, the loop variable itself will contain the quotes. The ~ modifier works here as well.

@ECHO OFF
REM The file list contains a quoted path.
FOR %%F IN ("file_A.txt" "C:\My Documents\report.docx") DO (
ECHO Raw variable (%%F): %%F
ECHO Cleaned variable (%%~F): %%~F

REM Best practice: use the cleaned variable and re-quote it yourself.
ECHO Using the path: "%%~F"
ECHO.
)

Output:

Raw variable (%F):   file_A.txt
Cleaned variable (%~F): file_A.txt
Using the path: "file_A.txt"

Raw variable (%F): "C:\My Documents\report.docx"
Cleaned variable (%~F): C:\My Documents\report.docx
Using the path: "C:\My Documents\report.docx"

The Limitation: Handling Regular Variables

This is a critical point that trips up many scripters: the ~ modifier does not work on regular environment variables (like %MyVar%). It only works on script arguments (%1, %2, etc.) and FOR loop variables (%%F, %%G, etc.).

An example of script with error:

@ECHO OFF
SET "MyVar="C:\My Path""
REM This will NOT work.
ECHO %~MyVar%

This will literally print %~MyVar% to the screen.

Solution: The FOR Loop Workaround

The standard and accepted way to strip quotes from a regular variable is to pass it through a single-iteration FOR loop.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "MyVar="C:\My Path""
ECHO Original variable: !MyVar!

REM Use a FOR loop to process the variable and apply the ~ modifier.
FOR %%I IN ("!MyVar!") DO SET "CleanVar=%%~I"

ECHO Cleaned variable: !CleanVar!
ENDLOCAL
note

Using DelayedExpansion (!MyVar!) makes this trick more robust, especially if the variable itself is being changed inside a loop.

Practical Example: A Robust File-Processing Script

This script takes a folder path as an argument. It safely removes quotes from the argument and then loops through all .txt files in that folder, using the ~ modifier again to handle any filenames with spaces.

ProcessFolder.bat
@ECHO OFF
SETLOCAL
SET "TargetFolder=%~1"

IF NOT EXIST "%TargetFolder%\" (
ECHO [ERROR] Folder not found.
GOTO :End
)

ECHO --- Processing text files in: "%TargetFolder%" ---
ECHO.

FOR %%F IN ("%TargetFolder%\*.txt") DO (
REM %%~fF gives the full, clean path to the file.
ECHO Found file: "%%~nxF"

REM Now we can safely use the clean path.
ECHO Full path is: "%%~fF"
ECHO.
)

:End
ENDLOCAL

Conclusion

The tilde (~) modifier is the essential tool for handling quoted strings in batch scripting, allowing you to create robust scripts that don't fail on paths with spaces.

  • Use %~1 to remove quotes from a script argument.
  • Use %%~F to remove quotes from a FOR loop variable.
  • To remove quotes from a regular variable, you must use the FOR loop workaround.

Mastering this simple modifier is a fundamental step toward writing professional and reliable batch scripts.