2

I have a program I am trying to run on logon which is dependent on files that are in the same directory that it resides. By manually running the program from the command line, which I have cd'd to, everything runs as it should. However, if I schedule a task to run the program I get an error from my program that complains about not finding a file it needs. So my question(s) is/are: 1.) Is the working directory of a scheduled task the location of the program? 2.) If not, how would I set the working directory to the location of the program?

Here is the code I am using the schedule my task:

SCHTASKS /Create /TN "Test" /TR $MyLocation\Test.exe /sc onlogon /RL Highest

Where "$MyLocation" is a powershell variable that I set to reference the directory of my program.

arynhard
  • 443
  • 1
  • 7
  • 26
  • This other post looks like it should answer your questions: http://stackoverflow.com/questions/447774/specifying-the-running-directory-for-scheduled-tasks-using-schtasks-exe – Chris N Jun 04 '12 at 16:01
  • I solved this by creating cmd file for the scheduled task in which I first set the working directory as the one that I wanted it to be and then ran the script. – Gisli Jun 04 '12 at 16:51
  • @ChrisN: I have seen that post but it does not provide a way that I want my program to perform. The /V1 switch does not let you operate under system account. I have tried that code but did not work. I can't provide you with an error I received, although I can try it again and post later. – arynhard Jun 04 '12 at 19:23
  • @Gisli: So you scheduled a batch file which changed directory to the program and from there executed the program? – arynhard Jun 04 '12 at 19:24
  • @arynhard: Yes, I couldn't find any easier way to do it. It's easy and it works. – Gisli Jun 04 '12 at 22:18
  • I like the idea but unfortunately I don't think it would work with the way my program is designed. In an attempt to make it work across multiple computers I designed it so that it could be run from any folder. Writing a batch file would require knowing exactly where the user would place the program. I would like to have it run independent of its location. – arynhard Jun 04 '12 at 22:30
  • If your program is a powershell script, it is possible to get the working directory at run-time and use Push-Location to set that as the working directory. I might also be possible from a C++/C# application but I have never done it. – Gisli Jun 05 '12 at 08:53

2 Answers2

0

I've got around this in the past by using a batch file. There's a neat trick you can use in a batch file which will give you the directory that tha batch file is located in. So, you can very easily run the app you want. Create a batch file with the following contents and place it in the directory next to your app, then set it's path as the value for the /TR argument.

%~dp0\Test.exe

Strictly speaking, the backslash isn't required as %~dp0 includes a trailing backslash anyway. I think it makes the batch file a bit more readable though.

Damian Powell
  • 8,259
  • 5
  • 45
  • 58
  • " pwd " in Powershell will list out the current directory. – rud3y Feb 05 '13 at 18:46
  • 1
    @rud3y Yes, but the current directory may not be the directory in which the script is running. For example, you could `cd C:\Foo` then `. C:\Foo\Bar\Baz\Quux.ps1`. In this case, `%~dp0` would be "C:\Foo\Bar\Baz" and `$PWD` would be "C:\Foo". – Damian Powell Feb 06 '13 at 11:07
0

If you run a script and send $MyLocation in as a parameter something like this would set up the task o:

 $cmdFile = "$MyLocation\Test.cmd"
 New-Item $cmdFile -type file
 "cd $MyLocation" >> $cmdFile
 "powershell -command '& {$($MyLocation)\Test.exe}'" >> $cmdFile
 SCHTASKS /Create /TN "Test" /TR $cmdFile /sc onlogon /RL Highest

Gísli

Gisli
  • 694
  • 2
  • 11
  • 28