DealMakerz

Complete British News World

Powershell: How to insert quotes into code without interpreting them as program instructions?

Powershell: How to insert quotes into code without interpreting them as program instructions?

I’m creating a simple PS script that scans file names for letters/characters that should not be allowed in file names, and changes those characters. If the script finds a “Ÿ” character in the file name, this character must be replaced with a “Y”, and so on.

The problem occurs when I want to enter all characters that are illegal for example. a variable.

$UnsupportedChars = '[!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ¡¢£¤¥¦§¨©ª«¬®¯]'

Characters that will be blocked also include the character – the apostrophe in this case – that begins and ends the variable. Simplified example:

$UnsupportedChars = '[ab/def']'

In other words, in the variable content above, the apostrophe appears as one of the characters that should be considered an undeclared character (see the seventh character in both variable contents above). But this apostrophe then signals to the program that this is the end of the variable contents – which is not what is intended.
I want to be able to specify “all” characters as content in the variable, without interpreting any of these characters as instructions/terminals of the variable. You are having difficulty understanding how to overcome this dilemma. Limited knowledge of PS. Help/tips/ideas would be appreciated.

You have a similar problem later in the code, specifically in the loop that scans file names and replaces illegal characters with legal equivalents. What is meant is as follows:

if ($_.Value -match "Å") { $newFileName = ($newFileName -replace "Å", "A") }

But there is a problem when I want to specify that the quote character is exactly the one I want to replace/delete from file names, because the quote character is strictly used to specify the character to be entered/processed:

if ($_.Value -match """) { $newFileName = ($newFileName -replace """, "-") }

PS doesn’t even accept the code as executable.
When the quotation mark is the character to be entered, what can be done next? Same principle as the above problem.

See also  Janet Jackson's megawatts crash on older hard drives

note. Understand that people might be thinking “but who the hell puts quotes in file names?”. I thought so, but some of the users of the simple server I run for work are like “computer dummies” in nature .DS