# External prog get file from main dir, want file in diff dir

**URL:** <https://www.ruby-forum.com/t/external-prog-get-file-from-main-dir-want-file-in-diff-dir/171102>\
**Category:** Ruby\
**Created:** [June 26, 2009, 1:36pm UTC](https://www.ruby-forum.com/t/external-prog-get-file-from-main-dir-want-file-in-diff-dir/171102 "2009-06-26T13:36:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kga85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/90ced4/32.png) [@kga85](https://www.ruby-forum.com/u/kga85)\
**Post date:** [June 26, 2009, 1:36pm UTC](https://www.ruby-forum.com/t/external-prog-get-file-from-main-dir-want-file-in-diff-dir/171102/1 "2009-06-26T13:36:21Z")

</div>

I am running a external program(2) from another ruby program (1). This  
external program requre a local file (the file must lay in the prog 1  
dir) . It uses some kind of custom get comand to get this file.

Say my dir is calld TestCase

Then this fle must be in TesCase/file.txt

But i want to store the file in TestCase/CaseX/file.txt (or in some  
other folder)

The problem is that i cant change the extern program, and it asks for  
the file to be in TestCase folder.

Some code almost similar to the code in the extern program (I cant  
change it):  
up = some proxy method to connect to user  
input up.getfile(“file.txt”)

How do i redirect the extern programs file request to another folder.  
And i cant change the externprogram.

Is ther som way to do it, or must i give up and just keep all files  
cluttered in the main prog directory?

---

<div class="post-metadata">

**Author:** ![kga85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/90ced4/32.png) [@kga85](https://www.ruby-forum.com/u/kga85)\
**Post date:** [June 26, 2009, 3:09pm UTC](https://www.ruby-forum.com/t/external-prog-get-file-from-main-dir-want-file-in-diff-dir/171102/2 "2009-06-26T15:09:48Z")

</div>

Dir.chdir() should do what you want, e.g.:

system(“pwd”)  
Dir.chdir(“CaseX”) { system(“pwd”) }

Within the block, the working directory is the argument to Dir.chdir.  
When the block finishes, the working directory is automatically reset  
to what it was before.

- Jamis

---

<div class="post-metadata">

**Author:** ![kga85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/90ced4/32.png) [@kga85](https://www.ruby-forum.com/u/kga85)\
**Post date:** [June 29, 2009, 4:09pm UTC](https://www.ruby-forum.com/t/external-prog-get-file-from-main-dir-want-file-in-diff-dir/171102/3 "2009-06-29T16:09:02Z")

</div>

Jamis B. wrote:

> Dir.chdir() should do what you want, e.g.:
> 
> system(“pwd”)  
> Dir.chdir(“CaseX”) { system(“pwd”) }
> 
> Within the block, the working directory is the argument to Dir.chdir.  
> When the block finishes, the working directory is automatically reset  
> to what it was before.
> 
> - Jamis

A million thanks. It worked.
