52 lines
1.5 KiB
Batchfile
52 lines
1.5 KiB
Batchfile
@echo off
|
|
setlocal
|
|
|
|
cd /d "%~dp0"
|
|
|
|
set "TASK_NAME=DocuTranslate"
|
|
set "PROJECT_DIR=%~dp0"
|
|
set "RUN_BAT=%PROJECT_DIR%run.bat"
|
|
set "RUNNER_BAT=%PROJECT_DIR%autostart_runner.bat"
|
|
set "LOG_DIR=%PROJECT_DIR%logs"
|
|
set "LOG_FILE=%LOG_DIR%\autostart.log"
|
|
|
|
if not exist "%RUN_BAT%" (
|
|
echo [ERROR] Cannot find "%RUN_BAT%".
|
|
pause
|
|
exit /b 1
|
|
)
|
|
|
|
if not exist "%RUNNER_BAT%" (
|
|
echo [ERROR] Cannot find "%RUNNER_BAT%".
|
|
pause
|
|
exit /b 1
|
|
)
|
|
|
|
if not exist "%LOG_DIR%" mkdir "%LOG_DIR%"
|
|
|
|
REM Create a startup task. Run this file as Administrator.
|
|
REM The task runs as SYSTEM, so it can start after reboot before desktop login.
|
|
powershell -NoProfile -ExecutionPolicy Bypass -Command ^
|
|
"$Action = New-ScheduledTaskAction -Execute 'cmd.exe' -Argument '/c ""%RUNNER_BAT%""';" ^
|
|
"$Trigger = New-ScheduledTaskTrigger -AtStartup;" ^
|
|
"$Principal = New-ScheduledTaskPrincipal -UserId 'SYSTEM' -RunLevel Highest;" ^
|
|
"$Settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -MultipleInstances IgnoreNew;" ^
|
|
"Register-ScheduledTask -TaskName '%TASK_NAME%' -Action $Action -Trigger $Trigger -Principal $Principal -Settings $Settings -Force | Out-Null"
|
|
|
|
if not "%ERRORLEVEL%"=="0" (
|
|
echo.
|
|
echo [ERROR] Failed to create scheduled task. Please run this file as Administrator.
|
|
pause
|
|
exit /b 1
|
|
)
|
|
|
|
echo.
|
|
echo [OK] Scheduled task "%TASK_NAME%" has been created.
|
|
echo [OK] It will run "%RUNNER_BAT%" at system startup.
|
|
echo [OK] Log file: "%LOG_FILE%"
|
|
echo.
|
|
echo You can start it now with:
|
|
echo schtasks /Run /TN "%TASK_NAME%"
|
|
echo.
|
|
pause
|