locked
Running .bat file using C# RRS feed

  • Question

  • Hi i've created a code that will run a .bat file on schedule test here's the code

    string taskBat = @"c:\bla\bla1\test.bat
            
            TaskService ts = new TaskService();
    
            TimeTrigger tt = new TimeTrigger();
            tt.StartBoundary = DateTime.Now;
    
            tt.Enabled = true;
            tt.Repetition.Duration = TimeSpan.FromMinutes(5);
            tt.Repetition.Interval = TimeSpan.FromMinutes(1);
        
             // Create a new task definition and assign properties
            TaskDefinition td = ts.NewTask();
          
            td.Triggers.Add(tt);
            
            td.Actions.Add(new ExecAction(taskBat, null, null));
         
            string taskName = "taskName";
          
            ts.RootFolder.RegisterTaskDefinition(taskName, td,TaskCreation.Create,null, null,TaskLogonType.InteractiveToken,null);
    

    But what happen is that , it opens the the test bat on the right time but it suddenly fails

    Is there something wrong?I tried schtasks.exe and still the same ?I'm using windows server 2003  would this have cause it?

    Monday, April 18, 2011 2:03 PM

Answers

  • Hi All,

     

    I was able to solve the issue by inserting the working directory like so

    string dir = d:\foldera\folderb\folderc\foldere 
    td.Actions.Add(new ExecAction(taskBat, null, dir));
    
    

    The task scheduler API from codeplex support this and my program is running fine

    Thanks for all your help, really appreciate it

     

     

    Wednesday, April 20, 2011 10:41 AM

All replies

  • hi,

    it "suddenly fails" is pretty vague.

    Where does it fail, do you get an exception? What message do you get?


    Microsoft MVP Office Access
    https://mvp.support.microsoft.com/profile/Stefan.Hoffmann
    Monday, April 18, 2011 2:09 PM
  • It has a dependency on a xml file in the same directory but says that it can't find it.I've tested it manually and there seems to be no problem at all with the xml file or the test bat. I'm worried that the schedule task ran fast and it's unable to find the xml file(first time using task scheduler api). Is this the only way for me to use Task Schedule?
    Monday, April 18, 2011 2:22 PM
  • hi,

    sounds like your batch uses a relative path to the working directory, which may be/is different when run as a scheduled task.


    Microsoft MVP Office Access
    https://mvp.support.microsoft.com/profile/Stefan.Hoffmann
    Monday, April 18, 2011 2:26 PM
  • What i understand from creating Task Schedule is it will create a .job  file , now since the task is created in the Windows\Tasks(just checked my task scheduler) it will never find the .xml file oui.What's the best workaround for this?Am i understanding this correctly?
    Monday, April 18, 2011 2:39 PM
  • hi,

    create a simply test batch in C:\Temp\TestCD.cmd containing

    @Echo Off
    Cd >> C:\Test\TextCD.Result.txt

    What output do you get when you run it via your job?

    btw, you should post your batch, at least the relevant parts.


    Microsoft MVP Office Access
    https://mvp.support.microsoft.com/profile/Stefan.Hoffmann
    Monday, April 18, 2011 3:07 PM
  • Hi Stefan,

     

    I've replace the string in my batch file and it running now, but entered another roadblock.

    The.bat file also needs to get a .config(which is a xml) file,the error is "Cannot find in C:\WINDOW\system32" yada yada this file.Now when i paste the config file into SYSTEM32 it works.

    Is getting the .config file from System32 is part of Task Scheduler.I got my Task Scheduler API from Codeplex.

     

    @@echo off
    
    @rem "Filter Syntax : /f:include Type:Automation and 
    
    gallio.echo /working-directory:. /rt:MonsterReport D:\aaa\bbb\ccc\something.gallio
    
    @@echo on
    

    Do i need to do something inside the .bat file so that it can find the .config file

    Tuesday, April 19, 2011 11:03 AM
  • hi,

    The.bat file also needs to get a .config(which is a xml) file,the error is "Cannot find in C:\WINDOW\system32" yada yada this file.Now when i paste the config file into SYSTEM32 it works.

    This is what I told you: The batch is running using another working directory. The easiest solution would be the use of an hard-coded working directory.

    Do i need to do something inside the .bat file so that it can find the .config file

    Post your failing batch...


    Microsoft MVP Office Access
    https://mvp.support.microsoft.com/profile/Stefan.Hoffmann
    Tuesday, April 19, 2011 11:17 AM
  • Set the ExecAction's WorkingDirectory to the bat's directory.
    Tuesday, April 19, 2011 12:07 PM
  • Hi All,

     

    I was able to solve the issue by inserting the working directory like so

    string dir = d:\foldera\folderb\folderc\foldere 
    td.Actions.Add(new ExecAction(taskBat, null, dir));
    
    

    The task scheduler API from codeplex support this and my program is running fine

    Thanks for all your help, really appreciate it

     

     

    Wednesday, April 20, 2011 10:41 AM