The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 2,388 other followers

string – Change backslash to forward slash in windows batch file – Stack Overflow

Posted by jpluimers on 2021/06/10

One of the situations where setlocal enabledelayedexpansion comes on handy to replace \ with / which some unix based tools like better: [WayBack] string – Change backslash to forward slash in windows batch file – Stack Overflow

echo off

setlocal enabledelayedexpansion

for %%f IN ("C:\tools\workspace\*") DO (
  set old=%%f
  echo !old!
  set new=!old:\=/!
  echo !new!  
  echo.                 
)

Related, as it explains when the source and target replacements are in variables themselves: [WayBack] command line – String replacement in batch file – Stack Overflow, thanks Joey and Tom Warfield!

You can use the following little trick:

set word=table
set str="jump over the chair"
call set str=%%str:chair=%word%%%
echo %str%

The call there causes another layer of variable expansion, making it necessary to quote the original % signs but it all works out in the end.

Upvoting this answer because it works both ways, with the environment variable in either position, or in both the “before” and “after” positions:

set word=table
set str="jump over the chair"
call set str=%%str:chair=%word%%%
echo %str%
set word1=chair
set word2=desk
set str="jump over the chair"
call set str=%%str:%word1%=%word2%%%
echo %str%'

–jeroen

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

 
%d bloggers like this: