2

I have a task that runs a .exe , It is a simple VB program that reads from a txt file, in the code I have this:

 My.Computer.FileSystem.OpenTextFileReader("test.txt")

But when the scheduler runs the .exe , it throws a FileNotFound exception because it is searching in "C:\Windows\system32\test.txt".

I tried writing the path as ".\test.txt" I need the file to be in the same directory than the .exe.

How can I fix this? Is this a windows-7 task scheduler problem?

Note: when I run the program manually it doesn't throws the exception. update: the path must be like that because im going to deploy the .exe in other computers with other paths

5
  • 1
    Replace test.txt with c:\full\path\of\file\test.txt
    – DavidPostill
    Commented Oct 31, 2014 at 16:02
  • 1
    I should have wrote this, I need the path like that because I'm going to put the .exe in other computers and the path will change.
    – Wabonano
    Commented Oct 31, 2014 at 16:03
  • 1
    "I need the path like that because I'm going to put the .exe in other computers and the path will change" If the path is going to change to something unknown, then how would you expect Windows to know where to find it? Commented Oct 31, 2014 at 16:12
  • the path of the .exe might change, thats why I want to write "test.txt" so I can put the .txt in the same level as the .exe
    – Wabonano
    Commented Oct 31, 2014 at 16:18
  • Um, just to verify: What exactly are we talking about here? Visual Basic 6? Visual Basic .NET? Or VBScript? I’ll correct my answer accordingly.
    – Daniel B
    Commented Oct 31, 2014 at 16:25

1 Answer 1

4

The Task Scheduler doesn't load the user's profile to run things, so it starts the scripts in "C:\windows\system32\".

In the Action section of the Task's properties, you can set the "Start In" folder to specify what folder to change to before starting the program.

enter image description here

From Windows' help on the subject:

In the Start in (optional) text box, you can specify the working directory for the command line that executes the program or script. This should be either the path to the program or script file or the path to the files that are used by the executable file.

Otherwise, you'll need to specific the exact path of the file you want to access (test.txt), or ensure that test.txt exists in C:\windows\system32, or in a folder in the system's Path variable.

If the text file will be in the same folder as the EXE you are running in the task, then in the EXE itself, you could specify the path of the TXT files by reading the current path of the EXE.

In VB, you could use the App.Path command to get that info.

4
  • 2
    To extend this answer, if the file can be placed in the same folder in the user's profile then you could try using the system variables value for the user's profile %USERPROFILE%\folder...?
    – Kinnectus
    Commented Oct 31, 2014 at 16:19
  • @Ƭᴇcʜιᴇ007 ty it worked :D!
    – Wabonano
    Commented Oct 31, 2014 at 16:22
  • @BigChris I don't see why not in general (that would be specifying a path), but the OP seems to need/want to access the TXT from wherever the EXE (being run by the Task) is located, in this case. :) Commented Oct 31, 2014 at 16:22
  • @Ƭᴇcʜιᴇ007, the moment I wrote the comment Daniel B wrote an answer that would more suit as an answer as the OP would simply need to deploy the files to the same directory and it would work...
    – Kinnectus
    Commented Oct 31, 2014 at 16:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .