Search code examples
vbscript

Get path using FSO.GetAbsolutePathName displaying the result with a forward slash "/"


I have the following code in visual basic script that detects the path and replaces a word with the path

When the code is executed, the word will be replaced by the path. The result will be as follows: C:\projects. What I want is for the result to come out with the slash "/". It should look like this: C:/projects. I have not been able to find the exact function to achieve what I want, I don't know if it exists, or if something should be added or modified in the code.

How could I do to achieve this?

OldTextFile = ".\file.txt" 
NewTextFile = ".\file1.txt" 
ReplaceFrom = "word" 
ReplaceTo = FSO.GetAbsolutePathName(".") 

FSO.CreateTextFile(NewTextFile, 2). _ 
Write(Replace(FSO.OpenTextFile(OldTextFile, 1).ReadAll(), _ 
ReplaceFrom, ReplaceTo, 1, -1, vbTextCompare))

Solution

  • You can modify your code by adding a Replace function that transforms the backslashes in the ReplaceTo variable.

    Here's how you can implement in your code (added to your previous code):

    OldTextFile = ".\file.txt"
    NewTextFile = ".\file1.txt"
    ReplaceFrom = "word"
    
    ReplaceTo = Replace(FSO.GetAbsolutePathName("."), "\", "/")
    FSO.CreateTextFile(NewTextFile, 2). _
    Write(Replace(FSO.OpenTextFile(OldTextFile, 1).ReadAll(), _
    ReplaceFrom, ReplaceTo, 1, -1, vbTextCompare))
    

    Hope that helps!