none
Special Characters in String Variable RRS feed

  • Question

  • Hi,

    I'm in the process of converting a Google Chrome JSON to individual folders and favorites for a migration back to IE.

    The script I have is works 99% perfectly, except when I encounter a bookmark that has a special character.  Consider this:

    Bookmark Name is "This is the name of the bookmark, |, \, / etc etc"

    I'm using a hash table to get the details I need:

    $hash= [ordered]@{
    Folder = $parent
    Name = $child.name <---This of course is the name of the bookmark.
    URL = $child.url
    Added = [datetime]::FromFileTime(([double]$child.Date_Added)*10)
    Valid = $Null
    Status = $Null
            }

    I set a variable such as $ShortcutName = $child.name

    I create the shortcut using $Favorite = $Shell.CreateShortcut($ShortcutPath + "\" + $ShortcutName.'value' + ".url")

    However, using this...mostly because of the special characters in the $child.name, the shortcut is created with the correct URL, but it's created with a blank name.

    How can I escape the special characters in $child.name (or $ShortcutName)?

    Regards

    Monday, June 5, 2017 2:06 AM

Answers

  • You cannot use and cannot escape them in a file name.  The displayed name of a shortcut can contain any characters you wish to display.  A shortcut must follow all rules that apply to a file path name or a url.


    \_(ツ)_/

    • Marked as answer by Stan.S Monday, June 5, 2017 4:03 AM
    Monday, June 5, 2017 3:35 AM

All replies

  • What special characters?


    \_(ツ)_/

    Monday, June 5, 2017 2:57 AM
  • | or \ or /, or : (among potentially others).

    For example, some users may have the URL as the actual bookmark name:  http://microsoft.com

    Therefore, the shortcut doesn't create correctly because of those characters.

    So consider the script:

    $ShortcutName = $child.name
    Write-Host "*********************" $Shortcutname
    $Favorite = $Shell.CreateShortcut($ShortcutPath + "\" + $ShortcutName.'value' + ".url")
    $Favorite.TargetPath = $child.url;
    $Favorite.Save()

    Which produces the following output:

    ********************* ConfigMgr 2012 Windows Update Client Process |

    Unable to save shortcut "C:\Users\USER\Downloads\Google Chrome\Chrome BookMarks\users\User3\Chrome
    Bookmarks\Bookmarks Bar\IT & T\Microsoft\SCCM\ConfigMgr 2012 Windows Update Client Process |.url".
    At C:\Users\USER\Downloads\Google Chrome\Chrome BookMarks\Get-BrowserInformation_WHOFFICE.ps1:174 char:4
    +             $Favorite.Save()
    +             ~~~~~~~~~~~~~~~~
        + CategoryInfo          : OperationStopped: (:) [], COMException
        + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

    Folder : SCCM
    Name   : ConfigMgr 2012 Windows Update Client Process |
    URL    : https://blogs.technet.microsoft.com/configmgrdogs/2014/06/29/configmgr-2012-windows-update-client-process/
    Added  : 25/01/2017 2:11:13 PM
    Valid  :
    Status :

    So the name of the shortcut can be in either $ShortcutName, $Child.Name or $Hash.Name.

    You can see when I find out what the variables value is via:

    Write-Host "*********************" $Shortcutname

    ...it gives me what I expect (including the pipe at the end):

    ********************* ConfigMgr 2012 Windows Update Client Process |

    But then of course, I get the error when it's trying to save the shortcut, presumably because of the "|" in the name.

    Monday, June 5, 2017 3:28 AM
  • You cannot use and cannot escape them in a file name.  The displayed name of a shortcut can contain any characters you wish to display.  A shortcut must follow all rules that apply to a file path name or a url.


    \_(ツ)_/

    • Marked as answer by Stan.S Monday, June 5, 2017 4:03 AM
    Monday, June 5, 2017 3:35 AM
  • OK, so it's essentially the same as right-clicking the file and being presented with the same error upon renaming with a special character.

    Makes sense.

    I appreciate the quick responses.

    Monday, June 5, 2017 4:03 AM
  • So now that I'm aware that Powershell will follow the standard file naming rules, how can I determine if $ShortcutName contains special characters?

    If I use: if ($ShortcutName -match '\W\s'), it will identify all special characters and more.

    I need for it to be able to detect the same characters that wouldn't otherwise be permitted for a file name such as \ / : * ? " < > |

    I'm finding that the script is attempting to create the shortcut filename, but the complete file name seems to escape at the special character.

    Eg. Intended Name: This is a : shortcut

    Actual filename:  This is a

    What I'm left with is a filename called 'This is a' with no extension, size or properties.  Files like this are very difficult to delete.  So I figure, if I can pick these characters up BEFORE the script creates the file, my problem will be solved.

    Regards

    Monday, June 5, 2017 12:14 PM
  • Your question is very hard to decode.

    if($name -nomatch '[/\\:*?"<>|]'){


    \_(ツ)_/

    Monday, June 5, 2017 6:12 PM