SC: String manipulation without variables being resolved [SOLVED]

Discuss and share scripts and script files...
Post Reply
Filehero
Posts: 2717
Joined: 27 Feb 2012 18:50
Location: Windows 11@100%

SC: String manipulation without variables being resolved [SOLVED]

Post by Filehero »

Hi,

I'm scripting a cascade of string/text manipulations where the source text is read from file.

Problem: the original text contains strings which could be XY variables (e. g. "<br>").

Question: how can I manipulate those strings leaving the var strings unresolved? Can I read the original text file into a single-quoted variable?

Well, actually I should know this after all the years - but to my shame I have to admit I dont. :oops:
Last edited by Filehero on 25 Mar 2022 19:56, edited 1 time in total.

highend
Posts: 14578
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: SC: String manipulation without variables being resolved

Post by highend »

Imho this would be a bug. readfile() shouldn't eval parts of the text, there is eval() for stuff like this...

You could use e.g. a powershell script to copy the contents of a file to the clipboard and use that with XY...

Code: Select all

$content = Get-Content "{your file}"
Set-Clipboard $content
One of my scripts helped you out? Please donate via Paypal

Filehero
Posts: 2717
Joined: 27 Feb 2012 18:50
Location: Windows 11@100%

Re: SC: String manipulation without variables being resolved

Post by Filehero »

Hi highend,

first, thanks for the quick reply.

Yes, readfile() evals the stuff on read. I will try the powershell approach later when finding some time ....
Last edited by Filehero on 25 Mar 2022 19:56, edited 1 time in total.

highend
Posts: 14578
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: SC: String manipulation without variables being resolved

Post by highend »

Code: Select all

    $psFile    = "%TEMP%\~getContent.ps1";
    $psContent = <<<>>>
        $content = Get-Content -LiteralPath "<curitem>"
        Set-Clipboard $content
    >>>;
    $psContent = regexreplace($psContent, "^[ ]{" . strlen(regexmatches(gettoken($psContent, 1, <crlf>), "^[ ]+")) . "}");

    writefile($psFile, $psContent);
    run lax("powershell.exe" -ExecutionPolicy ByPass -File "$psFile"), , 2, 0;

    // Now do whatever you want with <clp>;
One of my scripts helped you out? Please donate via Paypal

Filehero
Posts: 2717
Joined: 27 Feb 2012 18:50
Location: Windows 11@100%

Re: SC: String manipulation without variables being resolved

Post by Filehero »

As always (and famous for) quick and sophisticated. Thanks. :)

Now I need to find some more time to use it ....

jupe
Posts: 3292
Joined: 20 Oct 2017 21:14
Location: Win10 22H2 120dpi

Re: SC: String manipulation without variables being resolved

Post by jupe »

How are you testing that readfile is resolving that var? because if it is by using SC text then that is what is resolving that variable not readfile, so if your only issue is what you stated, then you can adjust <br> to another string (or disable it) so it doesn't interfere by using SC br, also alternatively if you still need to avoid using readfile for some other reason, you should be able to use runret and type to get the string, instead of clipboard contamination.

Filehero
Posts: 2717
Joined: 27 Feb 2012 18:50
Location: Windows 11@100%

Re: SC: String manipulation without variables being resolved

Post by Filehero »

Just came back from a trip with my son. While was driving his scooter I had plenty of time to think about my problem again. I think I'm onto something really stupid.

I'll do some tests later, because I guess it is not readfile(). Thanks for the heads up for now.

Edit/update:
It's not readfile(), it was my stupid brain. :oops:
It is not all variables as it first seemed to be. After I wrote a lot of debug messages into a file it's left to <br> - which is the default linebreaker in msg(). This deserves a very loud OUCH!

Background: I'm setting a up a rather long script/function collection to automatically create (or compare existing) VSCode (and Atom) snippets for XYplorer. The first source is idh_scripting_comref.htm from XYplorer.chm (extracted with 7zip), from which the relevant sections get parsed, followed by a series of html entity replacements, followed by another series of regex-based guesses on the nature of params (strings vs. numbers/booleans), followed by .......
Somewhere down this road I obviously confused myself ...


Thanks again for help - and understanding. :lol:

Post Reply