Name directory with a variable

Hello again everyone.
This one should be a quick one for you. I am creating a directory
structure and i want to name some of the folders with a variable but
cannot figure out how. Any help would be great.

Example: FileUtils.makedirs (‘Backup\Test\[email protected]@[email protected]’)

Kind Regards

On Friday 20 January 2012 at 1:02 PM, Alex S. wrote:

Posted via http://www.ruby-forum.com/.
Hi,

You can interpolate variables into strings using #{}, for example,

var1 = ‘foo’
var2 = ‘bar’
“Backup\Test#{var1}#{var2}” #=> “Backup\Test\foo\bar”

So one way of accomplishing what you want to do is to pass a string into
Dir.mkdir[1], but you may also want to check out File.join[2] to see if
it is more relevant to what you are trying to do.

Vikhyat K.
http://vikhyat.net/

[1] http://ruby-doc.org/core-1.9.3/Dir.html#method-c-mkdir
[2] http://ruby-doc.org/core-1.9.3/File.html#method-c-join

On Jan 20, 2012 1:53 AM, “Vikhyat K.” [email protected] wrote:

“Backup\Test#{var1}#{var2}” #=> “Backup\Test\foo\bar”
Except that backslashes inside double quotes need to be either doubled,
or
converted into single slashes; e.g.

“Backup\Test\#{var1}\#{var2}”
“Backup/Test/#{var1}/#{var2}”

So one way of accomplishing what you want to do is to pass a string into
Dir.mkdir[1], but you may also want to check out File.join[2] to see if
it
is more relevant to what you are trying to do.

On Fri, Jan 20, 2012 at 10:05 AM, Eric C.
[email protected] wrote:

On Jan 20, 2012 1:53 AM, “Vikhyat K.” [email protected] wrote:

On Friday 20 January 2012 at 1:02 PM, Alex S. wrote:

Hello again everyone.
This one should be a quick one for you. I am creating a directory
structure and i want to name some of the folders with a variable but
cannot figure out how. Any help would be great.

Example: FileUtils.makedirs (‘Backup\Test\[email protected]@[email protected]’)

“Backup/Test/#{var1}/#{var2}”
To avoid these issues File.join has been invented:

File.join “Backup”, “Test”, var1, var2
File.join “Backup”, “Test”, var1, var2 + var3

Assuming var2 and var3 are Strings. Otherwise

File.join “Backup”, “Test”, var1, “#{var2}#{var3}”

Kind regards

robert

On 01/20/2012 04:40 PM, Alex S. wrote:

Hello Everyone

Thank you so much for the replies, it now works as it is supposed to :).
I ended up using “Backup\Test\#{var1}\#{var2}” as I find it makes the
code easier to read.

While it’s probably not important to you at this time, the path you
built above will only work on Windows systems, where the file separator
character is backslash. A path like that won’t work on Unix-like
systems such as Linux and OSX.

It’s a really good idea to use File.join or the pathname library to
build your paths rather than directly building path strings. If you do,
you don’t need to worry about the potential pitfalls if you ever decide
to take your script to another platform. The tools should manage the
differences for you.

Another option is to always use forward slashes, if you really must
build the path strings directly. For now at least, forward slashes
appear to work on every platform supported by Ruby, including Windows;
however, it’s possible (if unlikely) that there may one day be a system
where this is not the case. Using the tools mentioned above should give
you a better chance of being protected in such an event. :slight_smile:

-Jeremy

Hello Everyone

Thank you so much for the replies, it now works as it is supposed to :).
I ended up using “Backup\Test\#{var1}\#{var2}” as I find it makes the
code easier to read.

Kind Regards

Alex

Alex S. [email protected] wrote:

Example:
FileUtils.makedirs (“Backup\test\#{@yr}\#{@yr}#{@mont}”)
FileUtils.mv test_files, “Backup\test\#{@yr}\#{@yr}#{@mont}”

The files only go so far as Backup\test\

Can you provide some details about the problem? Are you getting errors,
and if so what are they? Have you tried enabling the verbose option on
FileUtils.mv? What is the content of test_files?

-Jeremy

Hello all again.

Thank you for that info Jeremy its definitely going to help in the
future.

Last question regarding this. Because I have created the folders with a
variable I am now unable to move files into them. The files move but
only into the last folder that has not been named with a variable.

Example:
FileUtils.makedirs (“Backup\test\#{@yr}\#{@yr}#{@mont}”)
FileUtils.mv test_files, “Backup\test\#{@yr}\#{@yr}#{@mont}”

The files only go so far as Backup\test\

Any ideas?

Kind Regards
Alex

On 01/20/2012 07:08 PM, Alex S. wrote:

When i move the files they end up in:
Backup\test
leaving 2012 and 201201 folders empty.

This really isn’t enough to go on, for me at least. My guess is that
you’re building the path slightly differently when you make the
directory structure than when you attempt to move files into it. Try
creating the path string just 1 time, storing that string into a
variable and then using that variable everywhere you need the path
string.

You can also try running the FileUtils.mv method in verbose mode by
passing :verbose => true as the last argument:

http://rubydoc.info/stdlib/fileutils/1.9.2/FileUtils:mv

Another thing to try is printing the arguments you’re passing to the
FileUtils.mv method and eyeballing the output to see if anything jumps
out at you as suspicious.

Finally, if all else fails, you can try calling FileUtils.mv with hard
coded strings that you believe to be equivalent to what you’re passing
in normally to see if the behavior changes.

-Jeremy

Hello Jeremy

I am not getting any errors, the script runs almost perfect but when i
check the folders the files are in the test folder. The contents of
test_files is an array of files that have been sorted. Its as if
FileUtils.mv cannot read the variables.

This is what is created successfully:
Backup\test\2012\201201

When i move the files they end up in:
Backup\test
leaving 2012 and 201201 folders empty.

Kind Regards
Alex

On Sat, Jan 21, 2012 at 9:08 AM, Alex S. [email protected]
wrote:

I am not getting any errors, the script runs almost perfect but when i
check the folders the files are in the test folder. The contents of
test_files is an array of files that have been sorted. Its as if
FileUtils.mv cannot read the variables.

note, you are invoking a mv (move), so make sure that every time you
run the script, the source files are present (that if you really want
something moved)…

kind regards -botp

On Sat, Jan 21, 2012 at 9:08 AM, Alex S. [email protected]
wrote:

I am not getting any errors, the script runs almost perfect but when i
check the folders the files are in the test folder. The contents of
test_files is an array of files that have been sorted. Its as if
FileUtils.mv cannot read the variables.

if you may be so kind to provide us the script then, so we can also
verify it on our end.

thanks and kind regards -botp

Hello All

What i have is:
FileUtils.makedirs (“Backup\test\#{@yr}\#{@yr}#{@mont}”)

That successfully makes the directory: Backup\test\2012\201201

Then I have some code that sorts all the files in the current directory
and puts them into an array called test_files. All working so far.

Next is:
FileUtils.mv test_files, “Backup\test\#{@yr}\#{@yr}#{@mont}”

What happens is it moves the contents of the array into:
Backup\test\

This leaves an empty folder called 2012 and 201201.

I want the files to go into \Backup\test\2012\201201

I hope this makes more sense :slight_smile:

Kind Regards

Hello everyone

Ok never mind i figured it out! I did it by making a variable to
Backup\test\#{@yr}\#{@yr}#{@mont} and then used:
FileUtils.mv test_files, x

Thank you all so much for you help and fast replies.

Kind Regards
Alex

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs