Jump to content

Exclamation mark and delayed expansion problem


Recommended Posts

SETLOCAL ENABLEDELAYEDEXPANSION

FOR /F %%A IN ("!") DO (

SET Line=%%A

SET Line=!Line:^^!={#}!

ECHO !Line!

)

Is there a simple way to make it work? Edited by tomasz86
Link to comment
Share on other sites


SETLOCAL ENABLEDELAYEDEXPANSIONFOR /F %%A IN ("!") DO (	SET Line=%%A	SET Line=!Line:^^!={#}!	ECHO !Line!)
Is there a simple way to make it work?
WHAT EXACTLY do you actually EXPECT that piece of batch to do? :unsure:

If you prefer what do you apply that batch to?

And what you would want to have returned by the batch?

jaclaz

Link to comment
Share on other sites

SETLOCAL ENABLEDELAYEDEXPANSIONFOR /F %%A IN ("!") DO (	SET Line=%%A	SET Line=!Line:^^!={#}!	ECHO !Line!)
Is there a simple way to make it work?

Yes there is; don't enable delayed expansion!

SETLOCAL DISABLEDELAYEDEXPANSIONFOR %%A IN (!) DO (	SET Line=%%A	CALL SET Line=%%Line:!={#}%%	CALL ECHO=%%Line%%)
Link to comment
Share on other sites

I've done it this way:

FOR /F %%A IN ("!") DO (	CALL :FIX %%A)PAUSEEXIT:FIXSET Line=%1SET Line=%Line:!={#}%ECHO %Line%GOTO :EOF:EOF
So I guess it's impossible with delayed expansion enabled? Using CALL is extremely slow when processing hundreds of lines like that.
Link to comment
Share on other sites

As usual and as jaclaz alluded to you have not provided us with the genuine use for your script.

There is no way at all that the file you have posted is what you are actually doing! You have decided that you know better than the people you are asking for help, and that is both incorrect and rude.

Link to comment
Share on other sites

I'm sorry for not providing the actual file but I don't think it will really change anything. I'm processing a HTML file and I wanted to replace all special characters with {#}, {##}, etc. This was the original script I tried to use:

SETLOCAL ENABLEDELAYEDEXPANSIONFOR /F "delims=" %%A IN (index.html) DO (	SET Line=%%A	SET Line=!Line:{#}=^&!	SET Line=!Line:{##}=^?!	SET Line=!Line:{###}=^<!	SET Line=!Line:{####}=^>!	SET Line=!Line:{#####}=^^!!	SET Line=!Line:{######}=^|!	ECHO !Line!)
but I encountered the problem just in the very beginning with the first line of the file which is:

<!DOCTYPE HTML>

Link to comment
Share on other sites

GSM, you are very correct.

You may notice that the file they are pretending to be their finished product is only a small portion of a much larger script. The characters they are replacing are only those which may pose further parsing in a batch file problems. I would be certain based upon the previous help they have received that the intention is to run this routine through to a holding file perform several other batch parsing operations on it then attempt to return the original characters back in later.

Despite being aware VBS and small executables such as SED and GSAR and perhaps even lesser known ones like John Stockton's SUBS, tomasz86 is a windows 2000 user and is therefore unable to accept the easy way. :whistle:

Link to comment
Share on other sites

For processing of HTML characters I would choose to use regular expressions. You can use vbscript for this or any other scripting engine like perl or AutoIT.

If you insist on using a batch file you better start writing the script from scratch. Copying from online examples can help you, but you need to understand each line of code.

Edited by Acheron
Link to comment
Share on other sites

If he was to post the full script that he is using I probably might post the VBS equivalent.
Example VBS Script To Process A Text Line By Line

Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")'-> File To Work WithDim Text :Text = "Demo Count Down.hta"'-> Check To See If File Exists  If Fso.FileExists(Text) Then   ReadListText(Fso.GetFile(Text))  Else     WScript.Echo "Missing : " & Text  End If '-> Function To Process Line By Line Text Files  Function ReadListText(T)   Dim i, j, Ts    Set Ts = Fso.OpenTextFile(T.Path, 1)   i = Ts.ReadAll       For Each j In Split(i,vbCrLf)       WScript.Echo j      Next    End Function
Link to comment
Share on other sites

So are you trying to suggest now that the file you've just posted is what you are actually using?

I would strongly suggest that is not the case

There's no larger script this time. The script posted above is the only one I tried to use on the HTML file but then realised that it didn't work with exclamation marks. That's why I posted the question :)

Once again, this is a completely different thing than the other script about merging updates. It's got nothing to do with that. I'm writing a script to process a HTML file in order to remove some parts from it. I've got no other issues with it. I just wanted to ask whether it was possible to process exclamation marks with delayed expansion enabled but it seems that the simplest way to solve the problem will be just disable it and use CALL. Anyhow, thank you for the answer (post #3).

Link to comment
Share on other sites

So are you trying to suggest now that the file you've just posted is what you are actually using?

I would strongly suggest that is not the case

There's no larger script this time. The script posted above is the only one I tried to use on the HTML file but then realised that it didn't work with exclamation marks. That's why I posted the question :)

Once again, this is a completely different thing than the other script about merging updates. It's got nothing to do with that. I'm writing a script to process a HTML file in order to remove some parts from it. I've got no other issues with it. I just wanted to ask whether it was possible to process exclamation marks with delayed expansion enabled but it seems that the simplest way to solve the problem will be just disable it and use CALL. Anyhow, thank you for the answer (post #3).

The script you posted echoes replacements to the console window only, it most certainly does not remove parts of the html file. A html file without its opening and closing tag symbols isn't really a html file.

You asked for help with a specific problem when the problem you had was something different. I gave you a solution to replace a single character in a single character string with three specific characters. You then rejected my solution because you'd decided not to inform us that you were intending to replace several different characters in an entire file with strings of varying lengths. Our Members do not have to be rocket scientists to see that the characters you were intending to replace were those which pose problems to batch script parsing, and that the process you posted if not currently part of a larger script is intended to be so.

You are thinking through a task in small pieces without first looking at the entire picture. Whilst it may not be part of the merging .inf files topic, it certainly has the hall marks of taking light years to achieve a goal which could have been achieved quickly with the full information from the start.

Link to comment
Share on other sites

I didn't ask for help with removing parts from the HTML file because I can do it myself. The only problem I had was that exclamation marks were absent from the final file due to delayed expansion enabled, and that's why I asked the original question. You actually gave the answer already which is:

Yes there is; don't enable delayed expansion!

and I modified the script so that it works with delayed expansion disabled. I was just curious whether it was possible to somewhat escape the exclamation mark while leaving delayed expansion enabled.

Edited by tomasz86
Link to comment
Share on other sites

To me it is still the SAME issue already seen on the good ol' "merge and join" thread. :unsure:

In there I used for simplicity a pre (and post) processing with gsar for substituting characters that created issues in the batch processing.

I don't see in which way this is different.

In my perverted mind, if you can do without an external tool/app, that is very good :), but in this specific case a small .exe like gsar is better (IMHO) than any complexity in batch code and once you have it anyway it is better to re-use it as much as you can (economy of scale ;), the cost - in bytes - of the tool is amortized faster).

jaclaz

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...