JScriptを使用して既存のフォルダーから.zipファイルを作成しようとしていますが、copyHere関数が.zipフォルダーにコピーされていないようです。代わりに、file.attributesプロパティ(32 )。
これが私が使用しているスクリプトです:
//Get commman line arguments
var objArgs = WScript.Arguments;
var zipPath = objArgs(0);
var sourcePath = objArgs(1);
//Create empty ZIP file and open for adding
var fso = new ActiveXObject("Scripting.FileSystemObject");
var file = fso.CreateTextFile(zipPath, true);
// Create twenty-two byte "fingerprint" for .zip
file.write("PK");
file.write(String.fromCharCode(5));
file.write(String.fromCharCode(6));
file.write('\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0');
var objShell = new ActiveXObject("shell.application");
var zipFolder = new Object;
zipFolder = objShell.NameSpace(zipPath);
sourceItems = objShell.NameSpace(sourcePath).items();
if (zipFolder != null)
{
zipFolder.CopyHere(sourceItems);
WScript.Sleep(1000);
}
これで、CopyHere関数はsourcePathの内容を通常のフォルダーにコピーするために機能しますが、.zipファイルを作成してその内容をコピーしようとしても、何も起こりません。copyHereがsourcePathの内容を.zipにコピーしない理由について何か考えはありますか?
このスクリプトを呼び出す例は次のとおりです。
cscript win-zip.js C:\desired\zip\file.zip C:\path\to\source\folder
そして、望ましい結果は、file.zipが作成され、ソースフォルダーのコンテンツが含まれるようになることです。これは権限の問題でしょうか?この動作の原因は何ですか?
補足、vbScriptと同じコマンドを使用して、.zipを正常に作成してデータを入力できるので、jscriptを使用して機能しないのはなぜですか。
Set objArgs = WScript.Arguments
ZipFile = objArgs(0)
SourceFolder = objArgs(1)
' Create empty ZIP file and open for adding
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)
Set zip = CreateObject("Shell.Application").NameSpace(ZipFile)
' Get items in source folder
Set sourceItems = CreateObject("Shell.Application").NameSpace(SourceFolder).Items
' Add all files/directories to the .zip file
zip.CopyHere(sourceItems)
WScript.Sleep 1000 'Wait for items to be copied
有益なコメントをいただければ幸いです。