What is the best way to wait until a cmd is completed?

For "dir /R" specifically, which lists ntfs streams, there are not many options available. If the streams are what you are heading for, then using "dir" is fine I think, we had a little observation recently, so I think you know what you're doing: Is this sync procedure using metadata possible?

We could still work on that clipboard way of doing things. As Leo said, that's not optimal for most situations, although it's quite effective compared to not using the clipboard in this exact case. o) But to get around the clipboard, you'd need to capture/redirect the output of dir to some temporary files. The code below might help.

The core of all this is using Shell.Run() with "cmd.exe /C" just like you did, but here stderr + stdout have been redirected to files, which will be read after the command finished. Afaik, this is the only way to get all the output and the returncode of the executable. Consider trimimg down the Log()-thing and what else is not required here.

[code]///////////////////////////////////////////////////////////////////////////////
function RunHiddenAdvanced( exe, params, tmpFileBase, tmpFileExt, shell, fso){
Log("RunHiddenAdvanced():", "t", 1);
if (!fso) fso = new ActiveXObject("Scripting.FilesystemObject");
if (!shell) shell = new ActiveXObject("WScript.Shell");
if (!tmpFileBase) tmpFileBase = "DO.Temp.";
if (!tmpFileExt) tmpFileExt = ".txt";
var tmpFileStdout = CreateTmpFileName(tmpFileBase, tmpFileExt, fso).fullname;
var tmpFileStderr = tmpFileStdout+".err"+tmpFileExt;
var cmd = '%comspec% /c ""'+exe+'" '+params+' >"'+tmpFileStdout+'" 2>"'+tmpFileStderr+'""';
var result = {};
result.cmd = cmd;
Log(" CMD: "+cmd);
if (!fso.FileExists(exe)){
var msg = "Executable not found ["+exe+"]";
Log(msg, "e");
throw msg;
}
result.returncode = shell.Run( cmd, 0, true);

Log("Return: "+result.returncode, "d");
result.stdout = ReadFile(tmpFileStdout, fso); fso.DeleteFile(tmpFileStdout);
result.stderr = ReadFile(tmpFileStderr, fso); fso.DeleteFile(tmpFileStderr);
Log("-", "t", -1);
return result;

}
///////////////////////////////////////////////////////////////////////////////
function ReadFile( path, fso ){
Log("ReadFile():", "t");
fso = fso || new ActiveXObject("Scripting.FilesystemObject");
var content = "";
if (!fso.FileExists(path)){
Log("ReadFile(), file ["+path+"] not found.", "e");
return content;
}
Log("ReadFile(), opening ["+path+"]..", "d");
var file = fso.OpenTextFile( path, 1, -2); // Read, UseDefaultEncoding
if (!file.AtEndOfStream)
content = file.ReadAll();
file.Close();
Log("-", "t", -1);
return content;
}
///////////////////////////////////////////////////////////////////////////////
function CreateTmpFileName(prefix, extension, fso) {
if (!fso) fso = new ActiveXObject("Scripting.FilesystemObject");
var tFolder = fso.GetSpecialFolder(2); // 2 = temp folder
var tFile = fso.GetTempName();
if (prefix!=undefined) tFile=prefix+tFile;
if (extension!=undefined) tFile+=extension;
return {
path : tFolder.Path,
name : tFile,
fullname: tFolder.Path+'\'+tFile
};
}
///////////////////////////////////////////////////////////////////////////////
function Log(text, lvl, ind){ //XLog v0.41
var u,lvs={o:0,x:1,e:2,w:3,i:4,n:5,t:6,d:7,a:8},i=["","X","E","W","I","","","",""];
if (typeof XLog==(u="undefined")){var v=DOpus.Vars; if (v.Exists("XLog") && typeof XLogForce==u) {XLog=v.Get("XLog"); }}
if (typeof XLog==u){var c=Script.Config; if(typeof c!="undefined" && typeof c.XLog!=u){ XLog=c.XLog; }}
if (typeof XLog==u){XLog="normal";} if(XLog.paused===undefined){ if(XLog===true) var L=5;
else if(!isNaN(parseInt(XLog,10))) var L=XLog*1; else var L=lvs[(""+XLog).substring(0,1)]; XLog={paused:false,I:0,L:L};}
lvl=(lvl==undefined?5:lvs[lvl.substring(0,1).toLowerCase()]);
if (!(lvl && XLog.L && !XLog.paused && (lvl<=XLog.L))){ return;} var pad = (XLog.I==0?"":new Array(XLog.I+1).join(" "));
if (i[lvl])pad = i[lvl]+(!pad?" ":pad.substring(1)); if (text!="-"){ var d=DOpus; if (d.Version.AtLeast("11.13.1"))
d.Output(pad+text,((lvl==1||lvl==2)?1:0)); else d.Output(pad+text);}
ind=(ind!==undefined?ind:0);XLog.I+=ind;if(XLog.I<0)throw new Error("XLog indent went sub-zero.");
}

var result = RunHiddenAdvanced( "my.exe", "-param1 /option2");
Log("result.code : " + result.returncode);
Log("result.stdout: " + result.stdout);
Log("result.stderr: " + result.stderr);
[/code]

1 Like