javascript - How to get %userprofile%My Documents - Stack Overflow

The following function will work if My Documents is omitted, but I need to get to my documents.OpenTe

The following function will work if \My Documents\ is omitted, but I need to get to my documents.

OpenTextFile("test.txt");

function OpenTextFile(file) {
    var ObjShell = new ActiveXObject("Shell.Application");
    var wShell   = new ActiveXObject("WScript.Shell");
    var path     = wShell.ExpandEnvironmentStrings("%userprofile%\My Documents\");
    ObjShell.ShellExecute("Notepad.exe", file, path, "Open", "1");
}

as is, it gives me an error: Unterminated string constant Line 7 Char 80

The following function will work if \My Documents\ is omitted, but I need to get to my documents.

OpenTextFile("test.txt");

function OpenTextFile(file) {
    var ObjShell = new ActiveXObject("Shell.Application");
    var wShell   = new ActiveXObject("WScript.Shell");
    var path     = wShell.ExpandEnvironmentStrings("%userprofile%\My Documents\");
    ObjShell.ShellExecute("Notepad.exe", file, path, "Open", "1");
}

as is, it gives me an error: Unterminated string constant Line 7 Char 80

Share Improve this question asked Sep 12, 2013 at 19:06 Cameron DarlingtonCameron Darlington 3652 gold badges7 silver badges14 bronze badges 1
  • 1 Is the backslash escaping the quote at ("%userprofile%\My Documents\")? – j08691 Commented Sep 12, 2013 at 19:10
Add a ment  | 

3 Answers 3

Reset to default 3

In a String, \ is the escape character. If you want to include a \ you have to escape it.

wShell.ExpandEnvironmentStrings("%userprofile%\\My Documents\\");

You must remember to escape the \ - like this:

"%userprofile%\\My Documents\\"
OpenTextFile("test.txt");

function OpenTextFile(file) {
    var ObjShell = new ActiveXObject("Shell.Application");
    var wShell   = new ActiveXObject("WScript.Shell");
    var path     = wShell.ExpandEnvironmentStrings("%userprofile%\\My Documents\\");
    ObjShell.ShellExecute("Notepad.exe", file, path, "Open", "1");
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745201891a4616374.html

相关推荐

  • javascript - How to get %userprofile%My Documents - Stack Overflow

    The following function will work if My Documents is omitted, but I need to get to my documents.OpenTe

    4小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信