regexperlbatch-file

Modify Batch %0 variable backslashes


I'm trying to use my batch script's file path as part of a Perl find and replace using the %0 variable, but since Windows uses backslashes this doesn't work as I'd hope. The string expands from s/^%~dp//gi into "s/^C:\Users\user\Desktop\scripts\//gi" with unescaped backslashes.

Custom delimiters like "m\\\gi" wont do anything here. All I can think of is to echo the %0 into a file and find/replace it into my desired format there, but this feels like a terrible workaround.

Ideally, the format would be C:\\Users\\user\\Desktop\\scripts\\script.bat in order for it to work plainly within the perl script.

I suppose I have two questions, can I somehow change the backslashes in the %0 batch variable to \\ or is there anything within Perl to workaround this?


Solution

  • If you want to manipulate the contents of a string that gets passed as an argument, you need to store it in a variable first.

    set "script_path=%0"
    set "script_path=%script_path:\=\\%"