最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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");
}
发布评论

评论列表(0)

  1. 暂无评论