- 1 year ago
batch, dealing with spaces in path
Comment
1 / 1
When dealing with spaces in paths in a batch script, you need to enclose the path in double quotes ("
). Here are a few scenarios and how to handle them:
Assigning a path to a variable:
batchset "path_with_spaces=C:\Folder with Spaces"
Using a path in a command:
batchecho "This is a path: %path_with_spaces%"
Using a path in a
for
loop:batchfor %%F in ("%path_with_spaces%\*.txt") do (
echo "File: %%F"
)
Passing a path as an argument to a program:
batchprogram.exe "%path_with_spaces%\file.txt"
By enclosing the path in double quotes, you ensure that the entire path is treated as a single entity, even if it contains spaces. This allows the batch script to handle paths correctly, regardless of spaces or special characters.