Change backslash to forward slash in windows batch file

user3253319 picture user3253319 · May 8, 2014 · Viewed 40.5k times · Source

I'm trying to convert all backslashes () to forward slashes (/) in a variable which contains a file name and location. I've read about this and seen:

%variable:str1=str2%

and

set "var=%var:\=/%"

which I've attempted, but I'm obviously not getting it right.

Here is the relevant section of my .bat script:

FOR %%f IN ("E:\myfiles\app1\data\*.csv") DO (
  echo %%f  
  set "f=%%f:\=/%"
  echo %%f  
  echo.                 
)

The output show each filename listed twice.

i.e. this line:

set "f=f:\=/%"

is not doing what I want it to. Can anyone see what I am doing wrong?

Answer

foxidrive picture foxidrive · May 8, 2014

This will change the back-slashes to forward-slashes in a variable:

set "variable=E:\myfiles\app1\data\*.csv"
set "variable=%variable:\=/%"
echo "%variable%"