This is from a previous answer
@if (@This==@IsBatch) @then
@echo off
rem **** batch zone *********************************************************
rem Check if started from javascript part of script.
rem We are checking an environment variable set from javascript part.
if "%_run_hidden_%"=="true" (
goto startBatchWork
)
rem if not started from javascript, call javascript part to restart batch.
wscript //E:JScript "%~dpnx0"
exit /b
:startBatchWork
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "fullstamp=%YYYY%%MM%%DD%.%HH%%Min%%Sec%"
MOVE /Y C:\MyPath\Scripts\origfile.txt C:\MyPath\Scripts\997\newfile_%fullstamp%.txt
sleep 1
goto startBatchWork
rem End of batch area. Ensure batch ends execution before reaching javascript zone
exit /b
@end
// **** Javascript zone *****************************************************
// Instantiate the needed component to interact with Shell
var shell = WScript.CreateObject('WScript.Shell');
// Set the environment variable that the batch part will check to know
// it's running hidden
shell.Environment('Process').Item('_run_hidden_')='true';
// start the batch part of the script calling %comspec% with the current
// script as parameter, hidden (0) and not waiting for it to end (false)
shell.Run('"' + shell.ExpandEnvironmentStrings('%comspec%') + '" /c "' + WScript.ScriptFullName + '"', 0, false );
// All done. Exit
WScript.Quit(0);
It is a hybrid cmd/javascript file with your code included after the :startBatchWork
. Save it as .cmd
file.
You can call it as a cmd file. It will start a console (if not in a console) to run the initial batch part. It will check for the presence of a flag variable to determine if the file is running hidden (started from the js part) or not. If it is not running hidden, the javascript part of the code is called and it sets the flag variable and starts the batch file hidden.
Alternatively, you can directly call the javascript part with wscript //e:JScript batchfile.cmd
and launch all the process without a initial visible console.
In any case the process will continue running hidden without visible console.