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.
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 Friday 20 January 2012 at 1:02 PM, Alex Sweps 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.
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.
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.
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.
> 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\Test2\@var\@var2@var3')
>
> Kind Regards
>
> --
> Posted via http://www.ruby-forum.com/\.
Hi,
You can interpolate variables into strings using #{}, for example,
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.
-Jeremy
···
On 01/20/2012 04:40 PM, Alex Sweps 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.
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
···
Alex Sweps <alexszepes@gmail.com> wrote:
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.
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:
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
···
On 01/20/2012 07:08 PM, Alex Sweps wrote:
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.
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
···
On Sat, Jan 21, 2012 at 9:08 AM, Alex Sweps <alexszepes@gmail.com> 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 Sweps <alexszepes@gmail.com> 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.
On Fri, Jan 20, 2012 at 10:05 AM, Eric Christopherson <echristopherson@gmail.com> wrote:
On Jan 20, 2012 1:53 AM, "Vikhyat Korrapati" <c@vikhyat.net> wrote:
On Friday 20 January 2012 at 1:02 PM, Alex Sweps 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\Test2\@var\@var2@var3')
You can interpolate variables into strings using #{}, for example,