I want to run a ruby script, and end up in a different directory.
Possible?
All of the obvious approaches (%x, ``, system()) can change
directories, but only for their own process, once the script ends,
you’re right were you started.
For those who wonder ‘why?’… I have a Rake task that sets up new
projects by building a project directory tree, and a couple of stub
files. When I run it, I want to end up inside one of the project
dirs.
@rem = '–Ruby– @echo off
if “%TEMP%” == “” (
echo TEMP not set
goto exit 2>nul
)
SET ARG=%1
if “%1” == “” (
SET ARG=nil
)
set TMP_BAT_FILE=%TEMP%\TMPBAT_%RANDOM%.bat
write out Windows shell commands to the ARGV[0] filename
File.open(ARGV[0],“w”) { |f|
f.puts(‘cd \download’)
} END
:endofruby @echo off
if exist %TMP_BAT_FILE% call %TMP_BAT_FILE%
del %TMP_BAT_FILE% 2> nul
set TMP_BAT_FILE=
Unfortunately, no program running in any language can change the
directory in the shell it’s running in, after the program has finished
… assuming that the program is invoked as a process that is separate
from that of the shell (which is how ruby is invoked).
This is a function of how the operating system is designed. It’s true
for windows-based systems as well as unix-like systems.
I probably just don’t understand what you meant, but what about the
“cd” program? Is that somehow not separate from the shell?
I was hoping for a simpler solution, but that’ll work.
Thanks much.
I don’t know why this complex stuff was suggested. Dir.chdir does the
job - or am I missing something?
Dir.chdir doesn’t change your directory in the shell it’s running in
after the program has finished. That’s what he was after I think.
Unfortunately, no program running in any language can change the
directory in the shell it’s running in, after the program has finished
… assuming that the program is invoked as a process that is separate
from that of the shell (which is how ruby is invoked).
The shell maintains its own idea of the current working directory. When
a program is invoked, the current working directory can be changed in
that program’s environment, but when the program exits, you are back in
the shell’s environment, with its own idea of the current working
directory.
If you invoke a .bat or .cmd script, that is run by the winxp command
shell itself, and those programs can therefore change the shell’s idea
of the current working directory. But once you invoke a .exe, you are
in a separate program environment that has no way to change the
properties of the shell.
This is a function of how the operating system is designed. It’s true
for windows-based systems as well as unix-like systems.
I probably just don’t understand what you meant, but what about the
“cd” program? Is that somehow not separate from the shell?
“cd” is not a program. It’s a command that is implemented within the
shell. In other words, the shell itself interprets the “cd” (or
“chdir”, which is equivalent), and it causes the shell to change its
idea of the current working directory.
Dir.chdir doesn’t change your directory in the shell it’s running in
the shell’s environment, with its own idea of the current working
directory.
If you invoke a .bat or .cmd script, that is run by the winxp command
shell itself, and those programs can therefore change the shell’s idea
of the current working directory. But once you invoke a .exe, you are
in a separate program environment that has no way to change the
properties of the shell.
This is a function of how the operating system is designed. It’s true
for windows-based systems as well as unix-like systems.
Thanks for the detailed answer.
That was pretty much my thinking on it, but I was doing some wishful
thinking that there was some clever way to get around it. The batch
file approach will work, but it’s just not as elegant as I’d like.
Was also wondering if there was some Win32 call that might pull it
off.
I don’t know why this complex stuff was suggested. Dir.chdir does the
Well, back in the days of MS-DOS, an executable program could go through
your system’s memory and find the place where the shell stored its idea
of the current working directory, which the program could then change.
But with the OS’s that are built on top of the winnt architecture, those
days are gone, as far as I know.
Actually, here’s a sort-of clever way to get ruby to change your shell’s
idea of the current working directory. However, it requires a wrapper
script that needs to be sourced by your shell.
Create a file that contains the following commands:
Under unix-like systems, the file can have any name
(for example, “ruby-prog”), and it should contain this
line:
cd my-ruby-prog.rb
Under windows-based os’s, make this a .bat or .cmd file
(for example, “ruby-prog.cmd” or “ruby-prog.bat”), and
make it contain these two lines:
@echo off
for /f “usebackq” %%i (my-ruby-prog.rb) do cd %i
In your ruby program, when it is ready to exit, issue
these instructions:
puts cwd
exit(0)
… where the “cwd” variable contains the name of the
directory which ruby wants the shell to change to
after the ruby program exits.
for windows-based systems as well as unix-like systems.
Thanks for the detailed answer.
That was pretty much my thinking on it, but I was doing some wishful
thinking that there was some clever way to get around it. The batch
file approach will work, but it’s just not as elegant as I’d like. Was
also wondering if there was some Win32 call that might pull it
off.
Well, back in the days of MS-DOS, an executable program could go through
your system’s memory and find the place where the shell stored its idea
of the current working directory, which the program could then change.
But with the OS’s that are built on top of the winnt architecture, those
days are gone, as far as I know.
c:\work\active\apps>cdir.bat
(cdir.rb) was unexpected at this time.
Hmm … I wonder if this needs to run in a .cmd file instead of a .bat
file. Under winxp, it works for me in a .cmd file exactly as written.
Maybe if you rename your file to be cdir.cmd … ???
file. Under winxp, it works for me in a .cmd file exactly as written.
Maybe if you rename your file to be cdir.cmd … ???
Same results. Deleted the .bat file, and called it explicitly
(cdir.cmd) to make sure.
Actually, I had to do this:
\path\to\ruby.exe cdir.rb instead of just cdir.rb
I accidentaly copied and pasted the command line that I posted here from
an earlier version that didn’t work. I’m sorry for the confusion.
But if even this doesn’t work for you, then someone more familiar with
windows will have to jump in here. Unfortunately, I spend most of my
time in unix-based systems and I’m not sure where to go from here.
But I’m sure that this is do-able in windows, one way or the other.
I accidentaly copied and pasted the command line that I posted here from
an earlier version that didn’t work. I’m sorry for the confusion.
But if even this doesn’t work for you, then someone more familiar with
windows will have to jump in here. Unfortunately, I spend most of my
time in unix-based systems and I’m not sure where to go from here.
Nope, that didn’t help (yet). Not sure that ‘unfortunately’ applies
here btw
But I’m sure that this is do-able in windows, one way or the other.
I’m inclined to agree, and thanks much for trying. If nothing else,
you gave me food for thought, and a couple of search terms.
Already found some new toys, had never heard of pushd or popd, handy
to have around.
this will spawn a new command line with the inherited path.
Bill Stevens
Bill G. wrote:
I want to run a ruby script, and end up in a different directory.
Possible?
All of the obvious approaches (%x, ``, system()) can change
directories, but only for their own process, once the script ends,
you’re right were you started.
For those who wonder ‘why?’… I have a Rake task that sets up new
projects by building a project directory tree, and a couple of stub
files. When I run it, I want to end up inside one of the project
dirs.
c:\work\active\apps>cdir.bat
(cdir.cmd) to make sure.
But I’m sure that this is do-able in windows, one way or the other.
I’m inclined to agree, and thanks much for trying. If nothing else,
you gave me food for thought, and a couple of search terms.
a slightly less trickey way to do it (and apologies if this was already
proposed, I’ve been kinda skimming this thread up til now) would be to
call
your ruby program from a cmd file, and call another cmd file right
after.
then your ruby program could re-write the second cmd file.
like this:
doit.cmd contents:
ruby myprog.rb
followup.cmd
followup.cmd contents (written by myprog.rb):
cd c:\whatever\
…pretty sure this would work, if you can’t get the originally proposed
command line magic to work. It’d work as .bat files too, so you could
use
it in Win98 or DOS or whatever.