Skip to content

Instantly share code, notes, and snippets.

@anicholson
Created July 20, 2012 04:56
Show Gist options
  • Save anicholson/3148778 to your computer and use it in GitHub Desktop.
Save anicholson/3148778 to your computer and use it in GitHub Desktop.
Gotcha: DOS maintains CWD for each drive

Given this scenario:

K:\>dir
...
some_dir
job_1
...
K:\>cd some_dir
K:\some_dir> C:
C:\> cd script
C:\script> ruby processor.rb

You would expect this output...

In job job_1

Instead, we receive no output at all! Why?

Gotcha(s):

  1. Changing drives in DOS/Windows is a separate action from changing the CWD for a drive.
  2. Changing the working path to a different drive requires 2 steps.
  3. When Dir.chdiring to a non-root path on Windows, Ruby combines these steps to be more Unix-like. When passed a root path, however, it defers to whatever DOS' CWD is stored. Unfortunately, when you change drives in a DOS/Windows environment, your CWD for that drive is remembered!

As a result, in the above scenario, changing dir to ROOT_PATH (which we know to be K:) actually changes dir to K:\some_dir!

Solutions: Add a trailing / to the ROOT_PATH variable.

ROOT_PATH = 'K:'
Dir.chdir(ROOT_PATH) do
Dir.glob('job_*').each do |job|
puts "In job #{job}"
#... perform work within each job
end
end
ROOT_PATH = 'K:'
Dir.chdir(ROOT_PATH + '/') do
Dir.glob('job_*').each do |job|
puts "In job #{job}"
#... perform work within each job
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment