I've got a batch file that calls a node program:
#echo off
echo Starting Deploy.js: %date% %time%
echo CurrentDir: %CD%
node .deploy/deploy.js
IF %ERRORLEVEL% NEQ 0 goto error
echo Finished Deploy.js: %date% %time%
echo %~n0: Completed %date% %time%
goto end
:error
endlocal
echo An error has occurred during web site deployment.
call :exitSetErrorLevel
call :exitFromFunction 2>nul
:exitSetErrorLevel
exit /b 1
:exitFromFunction
()
:end
endlocal
I'd like to be able to have my node.js program be able to have it hit the error condition on failure of the application.
How do I trigger that in node.js?
I tried having my main function return 0 or 1, but it's not automatically setting the ERRORLEVEL.
What is the proper way to do this?
Check node.js documentation about process object .In your case you'll not need a callback function so you can simply use:
process.exit(666);
where you can set your desired exit code instead of 666
Related
I already googled and tried several solutions but without success...
My batch script looks the following, it opens four files but then cmd.exe window stays open and cannot be closed anymore. Even Task Manager asks for admin rights if I want to close. I am no admin, only normal user, so to force closing the cmd window I can only logoff and re-login to windows.
I already tried several options ("/b" option on start command, "exit" command at the end, also "exit 0" ... without any difference)
start "" /b "file1.xlsx"
#ping -n 3 localhost> nul
start "" /b "file2.xlsx"
#ping -n 1 localhost> nul
start "" /b "file3.xlsx"
#ping -n 1 localhost> nul
start "" /b "file4.xlsx"
I am using Windows 10 Enterprise 21H2.
At the bottom add:
:end
exit
Then most probably you can solve this 😊
#phuclv Thank you! I changed the pause from ping command to timeout command as you proposed: and this solved the problem for me.
Great! Thank you!
start "" /b "file1.xlsx"
timeout 2
start "" /b "file2.xlsx"
timeout 1
start "" /b "file3.xlsx"
timeout 1
start "" /b "file4.xlsx"
I have one single Batch file with 2 functions in it:
1.) MAIN
2.) ASYNC
Now i try to call the ASYNC function out of the MAIN function without waiting for the completion of the ASYNCfunction. Is that possible?
Here is my Batch file:
#echo off
:MAIN
setlocal
echo Here i am in the Main Thread
start /b :ASYNC
echo Here i am in the Main Thread again
endlocal
exit /b 0
:ASYNC
setlocal
echo I am in the ASYNC Thread
timeout /T 100
endlocal
exit /b 0
Is there any possibility to run a function in a single batchfile asynchron from another function in the same batchfile without separating the functions to multiple files?
Greets & Thank you :D
Yes, there is a possibility: use start to let the script restart itself, provide the label of the sub-routine as an argument and use goto rather than call to continue execution at that point:
#echo off
if not "%~1"=="" goto %~1
:MAIN
setlocal
echo Here I am in the MAIN thread
start "" /B cmd /D /C "%~f0" :ASYNC
echo Here I am in the MAIN thread again
endlocal
exit /b 0
:ASYNC
setlocal
echo Now I am in the ASYNC thread
timeout /T 10
endlocal
exit /b 0
When you run the script and want to first execute the main section, you must not provide an argument. If you do, it will be interpreted as a jump label. To avoid that, you could check whether the (first) argument begins with a : and skip the goto if not. Replace the line if not "%~1"=="" goto %~1 by the following to achieve that:
set "ARG1=%~1" & if not defined ARG1 shift /1 & goto :MAIN
if "%ARG1:~,1%"==":" goto %ARG1%
This is not absolutely safe against any odd arguments (like "&"&), but it will serve well in most situations. However, regard that the argument positions may not be as expected, because of the additional label argument (check out the argument string %*, which is even not going to be affected by shift).
Note, that the sub-routine then becomes executed in a new cmd.exe instance, so any environment changes will not reach the main instance.
There is even a better way to accomplish what you want, without affecting any potential arguments, namely to embed the label of the sub-routine in the script path itself when restarting the script (technique courtesy of user jeb, see his answer to How to pipe the final result of a windows batch for loop into another command):
#echo off
for /F "tokens=3 delims=:" %%Z in ("%~0") do goto :%%Z
:MAIN
setlocal
echo Here I am in the MAIN thread
start "" /B cmd /D /C "%~d0\:ASYNC:\..%~pnx0" %*
echo Here I am in the MAIN thread again
endlocal
exit /b 0
:ASYNC
setlocal
echo Now I am in the ASYNC thread
timeout /T 10
endlocal
exit /b 0
The key portion is the augmented path string %~d0\:ASYNC:\..%~pnx0, which is going to be resolved to %~d0%~pnx0 (that is the full script path %~0).
The sub-routine becomes again executed in a new cmd.exe instance.
This question already has answers here:
FTP commands in a batch script does not working properly
(2 answers)
Closed 5 years ago.
#echo off
set datetoday=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%
set /a batchdate=%datetoday%-1
cd c:\batchreports\DAYENDREPORTS\PREDAYEND\%batchdate%
ftp 192.168.18.188
username
password
cd autoemail
send DEALS_ENTERED_TODAY_ALL_2OM_UP_20170813.xls
bye
exit
When I try to run the code line by line in cmd prompt it works properly but if I run the bat file itself, it just prompts the login screen, which means my code stops working at the line of username.. what should I do so that the code will proceed?
To securely get yesterdays date you can't simply subtract one from todays date if this will swap the month as Compo already pointed out.
#echo off
For /f %%Y in (
'powershell -NoP -C "(get-date).AddDays(-1).ToString(\"yyyyMMdd\")"'
) Do Set Yesterday=%%Y
Set "Folder=c:\batchreports\DAYENDREPORTS\PREDAYEND\%yesterday%"
Pushd "%folder%"||(Echo Can't locate %folder% &Pause&Exit /B 1)
> ftpscript.ftp (
echo host 192.168.18.188
echo username
echo password
echo cd autoemail
echo send DEALS_ENTERED_TODAY_ALL_2OM_UP_%Yesterday%.xls
echo bye
)
(ftp -i -s:ftpscript.ftp 2>&1 >ftpscript.log) && (Del ftpscript.*) || (
Echo An error occured, view log:
more < ftpscript.log
pause
Exit /B 1
)
Exit /B 0
I want to search text in a log file.
If found show Error and Text.
Else show Not Found and Text.
set str2=Testing Failed in env
findstr "%str2%" SystemOut.log >nul
if not errorlevel 1 ( echo ERROR: Testing Failed in env)
if errorlevel 1 ( echo Not Found Testing Failed in env!)
Whenever in the log file it encounters Testing it says ERROR but it should not do that.
When I try to make changes by adding quotes or something Positive condition gets passed but it fails for negative condition.
Please help me with the script.
Thanks,
Machpatel
You need the /c: switch to include spaces in literal mode.
#echo off
set str2=Testing Failed in env
findstr /c:"%str2%" SystemOut.log >nul
if errorlevel 1 ( echo Not Found "%str2%")
if not errorlevel 1 ( echo found "%str2%")
I am trying to do a WHOIS for around 50 domains, i have searched on here and found a script that looks like it works but when i run it, it says:
"The Maximum setlocal recursion has been reached"
"The Process can not access the file, it is being used by another process"
Here is the code.
#echo off
setlocal
for /F "tokens=* EOL=# delims=" %%D in (e:\domains.txt) do call :reportit "%%~D"
endlocal
goto :eof
:reportit
setlocal
set "domain=%~1"
echo " Retrieving details for: %domain%"
echo " WHOIS: %domain%" >> e:\results.txt
echo "=============================================================" >> e:\results.txt
whois %domain% >> e:\results.txt
timeout 8
endlocal
exit /b
Any help will be much appreciated
Don't use WHOIS as the name of your batch.