Purvesh Kulkarni
Purvesh Kulkarni

Reputation: 9

Run a windows batch file with argument from Windows Scheduler

I want to run a batch file, which will accept a Date argument and perform some tasks. This activity has to be scheduled to run at a specified schedule. If it is required, we should also be able to run the task manually by providing the Date argument via a command.

Hence, I am trying to create a windows scheduled task. However, I'm stuck of how to provide the batch file with an argument. This will take care of the scheduled execution of the script.

To cater the requirement of manually running the task, at will, I shall be using schtasks command. I also want to understand, if it is possible to provide the arguments to the schtasks while executing?

Thus, to summarize my issue:

  1. How to pass date argument to batch script in Windows schedule task?
  2. How to dynamically pass the date argument to batch script when running scheduled task from command line?

I appreciate your suggestion, if there is a better way of implementing this.

Limitation:

The script can only be executed by a particular user of the server. While the task should be executed by any other user on the server with relevant privileges.

Server: Windows 2012

Edit:

The Batch file to process the date variable is working fine. My question is pertaining to scheduling of this script. I have tried using Windows task scheduler, which invokes this script, however, I am unable to pass the date as an argument to the batch script. How can an argument (Current Date) be passed from Windows task to batch script? I am aware that I can use %%DATE and %%TIME in batch script, however, it does not solve my requirement to run the script for some other date.

Upvotes: 0

Views: 11580

Answers (1)

Magoo
Magoo

Reputation: 80173

The more information you reveal about your application, the better able we are to help.

this batch should indicate a way:

@echo off
setlocal
set "rundate=%1"
if not defined rundate (
 echo date was not supplied
 echo system date is %date%
 set "rundate=%date:~u,v%%date:~w,x%%date:~y,z%"
)
echo running with date %rundate%
pause

The point is that we don't know the format of the date that you want to supply, nor of the date-format that would be used by your scheduler, so I can't advise which values to supply for u to z. The format is start position,length and the "first" character of the date string is "character 0".

date is a "magic variable" set up by the system to the current system date. It is shown in the format that the user is set up to use, so it could contain daynames or not and it may be in dd/mm/yy or mm/dd/yy format, have leading zeroes or not, used different separators or perhaps use 4-digit years.

But - the above batch should display either the parameter supplied to the batch, ie

thisbatch 20160304

would display 20160304 This would be used for the manual run

and

thisbatch

with no parameters would display a string constructed from the date variable and depending on u..z which should be usable for the scheduling application.

Upvotes: 0

Related Questions