Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows shell string operations (changing backslash to slash)

Tags:

I need to write a script that takes the current path (%~dp0), transforms backslashes into forward slashes and passes it further to some command.

Due to the environment I'm working in the only option that I have is windows shell (not Powershell where the issue would not a problem).

Is it even possible to do that?

like image 591
Matthias Hryniszak Avatar asked May 25 '10 09:05

Matthias Hryniszak


People also ask

How do I change backslash to forward slash in Windows?

Press \/ to change every backslash to a forward slash, in the current line. Press \\ to change every forward slash to a backslash, in the current line.

Why are slashes backwards on Windows?

The reason Microsoft is backwards on this goes back to MS-DOS 2.0 (DOS 1.0 had no directory hierarchy), which used a backslash to stay compatible with Dos 1.0 commands, which used slash for command line switches.

How do you change a forward slash in a string?

To replace all forward slashes in a string:Call the replace() method, passing it a regular expression that matches all forward slashes as the first parameter and the replacement string as the second. The replace method will return a new string with all forward slashes replaced.

How do I change a slash in bash?

You usually use " / " instead of the " # ", but as long as it is there, it doesn't matter. I am writing this on a windows PC so I hope it is right, you may have to escape the slashes with another slash. sed explained, the -e lets you edit the file in place. You can use -i to create a backup automatically.


1 Answers

The set command has a substitution feature:

set a=C:\test\dir set a=%a:\=/% echo %a% 

Results in:

C:/test/dir 
like image 55
Dennis Williamson Avatar answered Nov 07 '22 05:11

Dennis Williamson