4

I have a little script to read my PATH and store in a file, which I would like to be scheduled to run daily.

path = os.getenv("PATH")
file_name = "C:\\temp.txt" 
file = io.open(file_name, "w")
file:write(path)
file:close()

If I run it from command line it works, but when I create batch file (I work on Windows XP) and double click it - the os.getenv("PATH") returns false. The batch file:

"C:\Program Files\Lua\5.1\lua" store_path.lua

I read in comments to this question that it "is not a process environment variable, it's provided by the shell, so it won't work". And indeed, some other env variables (like username) work fine.


The two questions I have are:

  1. Why the shell does not have access to the PATH? I thought it would
    make a copy of the environment (so only setting env variable would be a problem)?
  2. What would be the best way to read the PATH in such a way that I can add it to a scheduler?
Community
  • 1
  • 1
Ola M
  • 1,298
  • 3
  • 12
  • 27
  • I can't reproduce your error. Your script works fine for me on double-clicking bat-file. WinXP. – Egor Skriptunoff Apr 30 '13 at 15:09
  • If you are running a batch file any way why not just do "echo %PATH% > C:\temp.txt" in the batch file? – Jane T Apr 30 '13 at 15:17
  • What is the exact text of the batch file you're using? – ECrownofFire Apr 30 '13 at 17:56
  • @JaneT: I used Lua to do a bit more in the script, like use current date in the file name. Which surely could be done with the Windows shell too :). – Ola M May 01 '13 at 08:50
  • @EgorSkriptunoff: I'd be really interested in the reason it baheves differently on your machine... For me it only works after adding 'cmd /c' before the command - as suggested in the answer. – Ola M May 01 '13 at 09:20

1 Answers1

3

Have the batch file run it from a shell so that you get shell variables:

cmd /c C:\path\to\lua myfile.lua
daurnimator
  • 4,091
  • 18
  • 34