Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 616
George Hunda Avatar asked Sep 18 '26 22:09

George Hunda


1 Answers

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:\=\\%"
like image 147
SomethingDark Avatar answered Sep 20 '26 19:09

SomethingDark



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!