How to escape special characters in for-loop?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Reino
Posts: 20
Joined: 14 May 2015 06:13
Contact:

How to escape special characters in for-loop?

#1 Post by Reino » 28 Jul 2015 11:37

More and more I get the feeling I should start learning a more intuitive language like Python, because I have another issue that needs a workaround.

Test script for a valid filename regex:

Code: Select all

@ECHO off

FOR /F "tokens=2 delims=:." %%X IN ('CHCP') DO SET codepage=%%X
CHCP 1252>NUL
SET "name=AVRO: [Wié*,/ïs dê\ Mòl?!] (08012009^)"
CHCP %codepage%>NUL

ECHO %name%
ECHO %name% | xidel.exe -q - -e "replace(replace($raw,':','-'),'[<>:\"/\\\^|?*]','')"
FOR /F "delims=" %%A IN ('ECHO %name% ^| xidel.exe -q - -e "replace(replace($raw,':','-'),'[<>:\"/\\\^|?*]','')"') DO ECHO %%A
Output:

Code: Select all

AVRO: [Wié*,/ïs dê\ Mòl?!] (08012009)
AVRO- [Wié,ïs dê Mòl!] (08012009)

"') DO ECHO %A was unexpected at this time.

Xidel is a wonderful piece of software that lets me use XPath/XQuery in Batch. It first replaces the colon with a dash, because colons aren't allowed in a filename. If it then encounters any of the invalid filename characters (between squarebrackets), it will remove those.
The first xidel-command works fine, but my question is about the for-loop. I've tried to double escape the pipes, escape the less/greater than signs and tried numerous other things, but no matter what I always end up with ""') DO ECHO %A was unexpected at this time.".
Does anyone know how I can make the for-loop work?

Reino
Posts: 20
Joined: 14 May 2015 06:13
Contact:

Re: How to escape special characters in for-loop?

#2 Post by Reino » 15 Oct 2016 15:08

I found this old thread of mine and seeing nobody has answered it yet, I might as well do it myself.

The reason the for-loop didn't work was because of the single double-quot between the initial double-quotes.
It appears there always needs to be a closing double-quot:

Code: Select all

"replace(replace($raw,':','-'),'[<>:\"\"/\\\^|?*]','')"
This works, but since 2 double-quotes in an array of characters to be replaced doesn't make sense, you can also use XQuery and use &quot; instead of \":

Code: Select all

--xquery "replace(replace($raw,':','-'),'[<>:&quot;/\\\^|?*]','')"

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: How to escape special characters in for-loop?

#3 Post by foxidrive » 20 Oct 2016 02:40

It's likely you didn't get a reply because you were using a third-party tool and a complex 'for' expression and the whole task wasn't all that clear - as the output is showing foreign language characters.

Non-latin characters are known poison characters and it just adds a layer of uncertainty to a question where the task is not clear.

If you'd used plain latin characters in your input file and shown the before and after results of the third-party tool, then it's far more likely that someone would have piped up with a better solution or at least dicsussed your question.

Post Reply