How to make SVN log display in the window using batch file and Task Scheduler? - windows

I am trying to make a batch file that will check certain SVN repositories for updates each morning. I want to store local repository names in a file (SVN_check_list.txt) and have the console show me the list. My code, posted below, works when I just run the batch file:
#echo off
echo Checking for updates...& echo.
for /F %%A in (SVN_check_list.txt) do (
echo Checking '%%A'
svn status %%A -u )
pause
However, when I try to run it through Windows Task Scheduler (while I am logged in), it runs the code but does not display anything until the 'pause' at the end. When I turn echo on, it shows the commands (svn status -u) but not the output. How can I make this batch file display the outputs of the svn status command even when I run it with task scheduler?

Try passing cmd as the Program/Script to run in Scheduler with arguments /k "C:\My Batch File Folder\MyScript.bat"
This will launch the console.

I found this solution that seems to work: Run a batch file with Windows task scheduler
Basically:
Action: Start a program
Program/script: cmd
Add arguments: /k "C:\Users\tanderson\Documents\setup.bat"
Start in: C:\Users\tanderson\Documents (No quotes)

Related

How to run a .bat file continuously using task scheduler?

I have a simple batch script which is coping file from a folder and then move those files to a different folder. i set a up a task scheduler which working fine. but if another task run the cmd before previous task end, it will mixed up with the copy/move file job. is there anyway we can set the task scheduler that next task will run "only if previous task end"?
here is the screenshot i currently set
https://i.imgur.com/DTcKj9t.png
As mentioned in the comments by Ikegami, You could use a lock file to test if the batch file is running and let the same process delete the lock file when the script is done. You have to be sure though that the script cannot exit unexpectedly.
Another way is to add the following to the top of your batch-file
#echo off
tasklist /FI "WINDOWTITLE eq my_job_run" | findstr /i "cmd.exe"
if not errorlevel 1 exit
title my_job_run
This will check if a process by the name of cmd.exe exists with the window title of my_job_run if it does, exit and if it does not exist, continue and create the title, where the next run will detect it and not run the script again.

Execute bat file as a windows service

I have a windows batch file which i want to execute as a service. I have found app like alwaysRun but i want to use windows in-build app for this purpose. Can anyone please suggest.
My Use Case is : - I have a batch file which will be executing after every 10 secs. So i have created a normal batch file which calls this bat file and sleeps for 10 secs. So i want to make this second bat file as a service. So that it is called once and when the windows reboots.
This file should be called as a service.
#echo off
:begin
CALL dummyfile.bat
timeout /t 1
goto begin
Please suggest.
I would not do that. You could run the first batch file from a scheduled task.
OR
If you want it to run at startup,
As an example, on Windows 8 you could
Create a VBS file that will completely hide your batch file.
hideme.vbs
Set MyScript = CreateObject("WScript.Shell")
MyScript.Run "C:\wherever\script\is\batch.cmd", 0, False
It can be launched as cscript hideme.vbs
Then open Start / Run and type shell:startup and press enter. Paste a shortcut of the VBS file here.
This will let VBS file call the second batch file in hidden mode each time the PC starts up.
EDIT
In order to kill it, you need to create another batchfile which you can run to find the cmd.exe that is running in the background.
In your original batch file, create a title at the very beginning after #echo off
#echo off
title LOOP
:begin
CALL dummyfile.bat
timeout /t 1
goto begin
Now in your new batchfile, let's call it killLOOP.cmd you add this:
taskkill /F /FI "WindowTitle eq LOOP" /T
This will search for a process with Window title LOOP, then kill it. Just run it when you want to close it.
now in your
I executed batch file as a service, but it didn't work.
NSSM package resolved my problem. here are the steps to follow:
1- Download NSSM package
2- Add NSSM Directory path in Environment Variables Path
3- Execute command like:
nssm install Service_Name
3.1 After executing command mentioned in step 3, it will show a popup to provide
program file path. provide batch file path like "C:/Test/test.bat"
3.2 Click on install and now batch file will be executed as a window Service.
have you ever looked at the native Windows program called sc.exe ? It allows you to run a program as a service.
Here's some Microsoft documentation on it: https://support.microsoft.com/en-us/help/251192/how-to-create-a-windows-service-by-using-sc-exe
sc [Servername] Command Servicename [Optionname= Optionvalue...]
In particular, you could use the Create Command to create a new service:
Create
Creates a service (adds it to the registry).
So the simplest syntax would be something like:
sc Create myservice binPath=C:\some\path\to\myservice.bat

Read variable from external file not working on running as scheduled task

I would like to run a batch file after resuming from sleep state in Windows.
If I start the batch file on command line everything works as expected.
But the batch script does not run properly as scheduled task.
What I have done:
External config file AutoMountConf.bat contains set Pass = Test
Local script file scheduleTask.bat contains
rem AutoMountConf.bat is in my intranet.
call X:\AutoMountConf.bat
start "" "C:\Program Files\TrueCrypt\TrueCrypt.exe" /auto favorites /p %Pass% /q
On command line the TrueCrypt container is mounted.
If I run the script from scheduled task I get the login screen to type the password manually.
There are two or perhaps even three issues.
The first one is set Pass = Test instead of set "Pass=Test" as Stephan reported already. For more details on how to assign a value right to an environment variable see my answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line?
The second issue is caused by the fact that network drives once mapped by a user to a drive letter and remembered in registry by Windows are automatically disconnected by Windows on user logs off and are only reconnected if the same user logs on again.
For a scheduled task it is therefore very often necessary to use UNC paths for files and folders on a shared folder in network or connect the network drive and disconnect it in the batch file itself executed as scheduled task.
It is not possible to call a batch file with UNC path. Windows does not allow that. Therefore it is necessary to connect and disconnect to network share manually in the batch file. I offer 2 solutions for this problem.
The first one is using command net use:
%SystemRoot%\System32\net.exe use X: \\ComputerName\ShareName password /user:Domain\UserName /persistent:no
if not errorlevel 1 (
call X:\AutoMountConf.bat
%SystemRoot%\System32\net.exe use X: /delete
start "" /wait "C:\Program Files\TrueCrypt\TrueCrypt.exe" /auto favorites /p %Pass% /q
)
password and /user:Domain\UserName is necessary only if the scheduled task is not executed with a user account which has the permissions to access the batch file on the remote machine. In general it is much more secure to define the scheduled task with the right user account and safe the password also for this account together with the task. Windows stores the password for the task encrypted like it does it also for the user account itself.
Run in a command prompt windows net use /? for details on the required and optional options. /persistent:no is what avoids remembering the network share in Windows registry for automatic reconnect after login by same user.
The second one is using commands pushd and popd:
pushd \\ComputerName\ShareName
if not errorlevel 1 (
call AutoMountConf.bat
popd
start "" /wait "C:\Program Files\TrueCrypt\TrueCrypt.exe" /auto favorites /p %Pass% /q
)
Please execute in a command prompt window pushd /? and read the output help to understand why this works.
But this solution requires that the user account used for the scheduled task with correct password is one which has appropriate permissions on the share on the remote computer. Password and user name can't be specified with this solution in the batch file itself.
if not errorlevel 1 means if previous command exited NOT with a value greater or equal 1 meaning if exit code of previous command is 0 and therefore command execution was successful. It can always happen that the remote machine is currently not available on network and therefore it is always good to check success on connecting to share on remote machine.
There is perhaps one more reason why Pass is not defined after running AutoMountConf.bat.
AutoMountConf.bat contains setlocal and the variable Pass is defined after this command was executed and before endlocal is executed in same batch file or implicitly called by command processor on exiting AutoMountConf.bat.
setlocal results in creating always a copy of existing environment variables and all modifications on environment variables are done on this local copy. The previous environment variables are restored on execution of (matching) endlocal or when end of a batch file is reached in which case the command processor automatically restores previous environment.
Please execute in a command prompt window setlocal /? and read the output help.
For examples to understand environment management by commands setlocal and endlocal perhaps even better see answers on Echoing a URL in Batch and Why is my cd %myVar% being ignored?
set Pass = Test
sets a variable pass<space> with the Content <space>Test. So %pass% keeps empty.
use this Syntax:
set "Pass=Test"
to avoid any unintended spaces.

How to end a batch file with an open command window at a certain folder location

This seems like it should be ridiculously simple but I cannot find the answer online or in these forums.
I want to run a command from a batch file, leave the command window open and end at a particular file location.
I can't seem to get both to happen in the same window. This is for a script to run an automated task everytime and leave the window open to run a 2nd task that has a variable input.
start cmd /k c:\users\test\desktop\dmiwtool1.1\win64\dmiwtoolx64.exe & cd c:\users\test\desktop\dmiwtool1.1\win64\
If I run either by itself, they work (runs the exe, ends at /desktop prompt), but in this sequence only the first runs.
this works here:
#ECHO OFF
START /b "" "path\program.exe" "parameter"
CD %UserProfile%\Desktop
Do not use setlocal in your Batch or put endlocal in the line before the CD command.
This should work:
start cmd /k "c:\users\test\desktop\dmiwtool1.1\win64\dmiwtoolx64.exe & cd c:\users\test\desktop\dmiwtool1.1\win64\"
If you leave out the quote marks, the start command and the cd command are run separately.

Building a batch file to run exe files sequentially

I just start to learn how to build batch file. ( on the windows 7 environment)
I want to build the batch file which is able to run .exe files sequentially .
Run batch files sequentially
I was trying to apply above idea but I am not really sure how to apply it
For example, there are three file on the D:/
In "D:/" there are three .exe files.
MyDriver.exe
YouDriver.exe
Mysoftware.exe
And I would like to build batch file which is running three exe files sequentially
Possible scenario is..
Run batch file
Run MyDriver.exe
MyDriver file's install GUI pops up and then user start to install Mydriver
Done with MyDriver.exe
Run YouDriver.exe
YouDirver file's install GUI pops up and then user start to install YouDriver
Done with YouDriver.exe
Run MySoftware.exe
MySofrware install interface pops up and then user start to install MySoftware
Done exit batch file.
I am not really sure if batch files can do it or not...
if it is impossible , is there any other options to build it ???
thanks
You actually don't need to do anything special to make this happen; batch files are synchronous by default, so execution of the batch file will pause when an executable is launched, and resume when it exits. Something as simple as this should do:
#echo off
REM "#echo off" prevents each line from being printed before execution,
REM and is optional
REM "REM" introduces a comment line
D:\MyDriver.exe
D:\YouDriver.exe
D:\MySoftware.exe
Of course, if you're interested in checking the return values of the programs, to see whether they succeeded or failed to install (assuming the installer provides that information), then things become slightly more complicated; if that's what you need, mention it in a comment, and I'll expand my answer accordingly.
This will start each file and wait for it to complete and then launch the next one.
#echo off
start "" /w /b "d:\MyDriver.exe"
start "" /w /b "d:\YouDriver.exe"
start "" /w /b "d:\Mysoftware.exe"
start MyDriver.exe
start YouDriver.exe
start MySoftware.exe
If you want the batch file in a different dir you would have to do:
cd D:\
start MyDriver.exe
start YouDriver.exe
start MySoftware.exe
If you want a more flexible system:
echo Welcome to EXE starter!
set /p dir = DIR:
set /p exe = EXE1:
set /p exe1 = EXE2:
set /p exe 2 = EXE3:
cd DIR
start exe
start exe1
start exe2
There you go!
To do it squentially:
call YouDriver.exe
call MeDriver.exe
call Mysoftware.exe
call will halt the batch file until program has closed.
Try and put it in the same directory of the files you want to run. If you can't, use cd C:\Directory\Name to change it to the directory where the MyDriver.exe file is. Then just do MyDriver.exe- you don't need a call or start statement.
MyDriver.exe
YouDriver.exe
MySoftware.exe
use cd at the start if you neeed to.

Resources