I would like to write to a new text file with JavaScript, and I tried to use WSH for that,

var fs = WScript.CreateObject ("Scripting.FileSystemObject");
SCRIPT5009: SCRIPT5009: 'WScript' is not defined

I got an error message.

Referenced sites Nothing was mentioned, but is there any description that needs to be added or added? Please give me an answer if you understand.

  • Answer # 1

    Isn't the wrong way to execute JavaScript with WSH?

    To execute JavaScript (exactly JScript which is Microsoft's implementation) using WSH, it is necessary to execute from C: \ Windows \ System32 \ wscrit.exe or C: \ Windows \ System32 \ cscript.exe There is. Try to execute the following at the command prompt or PowerShell.

    cscript script file name

    If you don't have a console, run it as follows (use it in a shortcut).

    wscript script file name

    In the Windows standard, the ".js" extension is not related to cscript.exe or wscript.exe, so it may be executed by double-clicking. Please note that other software may overwrite the ".js" link association with another.

    The global objectWScriptis a WSH-specific object. It cannot be used with other JavaScript engines such as JavaScript and Node.js in each browser. WSH is only available on Windows, so it cannot be used on Mac or Linux.

    Please note the following when using WSH.

    WSH's JavaScript (JScript) engine is old and has not been updated at all. The standard is not compliant with ES5 or even ES5 after ES2015. However, if you specify the engine of Microsoft Edge by class ID, it seems that you can also use the latest JavaScript grammar.

    The character code must be Windows-31J (CP932, MS932, Shift_JIS variants) or UTF-16LE. If you use Japanese with any other character code, it will be garbled or an error. (You can check whether the character code is correct by checking whether it is "ANSI" (Windows-31J) or "Unicode" (UTF-16LE) when saving with Notepad.)

    WSH is confirmed to be usable on Windows 10, but Microsoft recommends using PowerShell for future script processing, and WSH functionality may be removed or disabled by default in the future .

  • Answer # 2

    WSH creates a.jsfile and executes it by double-clicking it. Can't use it from the browser, is that a problem?

  • Answer # 3

    It's not JScript but JScript.
    It works with scripts executed in Windows and server-side scripts on Windows Server (IIS), and cannot be read and run as HTML as JS.


    I want to write a new text file with JavaScript

    You should write this part more specifically. What kind of processing do you want to save to a file in which process?

  • Answer # 4

    Are you sure you want only IE?

    var fs = new ActiveXObject ("Scripting.FileSystemObject");