File.dirname windows path returns - ruby-on-rails

I want to extract the directory name from a windows path. The windows path is a string, something like the following:
"c:\\some\path\name"
when I do the following:
File.dirname("c:\\some\\path\\name")
The result is
"."
If I run this on the unix path it works fine
File.dirname("/some/path/name") => "/some/path"
Do I need to somehow set the FILE::ALT_SEPARATOR? I have tried different variations of the path to no avail.

One solution I found is to replace all the backslashes with a forward slash. This works decently well. However there still must be a better solution.
File.dirname("c:\\some\\path\\name".gsub('\\', '/')).gsub('/', '\\')
=> "c:\\some\\path"
I sub the backslashes back in after the dirname call in order to keep the representation consistent.

The recommended way is to always use unix-type forward slash for path separators in Ruby code. Even if you use it on Windows OS, they will be correctly mapped internally to its backslash path separators.
If the backslash comes from the user input, then you need to detect whether the OS is such that allows a backslash in a file name (e.g., Windows does not, Unix does). Then if the backslash is not allowed, then you should convert them to forward slash during validation. In the Ruby code, keep all separators as forward slash. So, you should not be caring about backslashes when using commands such as File.dirname.

Related

Electron command line replaces single backslash with double

I'm building an app in Electron that requires a file path to be passed to it via the command line. I'm able to retrieve the command line arguments and values fine, but the backslash ('\') character keeps getting replaced with two backslashes ('\\') regardless of how I format the file path in the arguments.
For example, this is the test command: npx electron . --path='test/path\\hello\foo\/baz'
In main.js I use getSwitchValue('path') to get the value and immediately print it out:
'test/path\\\\hello\\foo\\/baz'
Notice that the forward slash stays the same, while all the backslashes are doubled.
The real problem is trying to replace the backslashes with a single backslashes or a forward slash using the string.replace method to no avail.
Obviously, I can instruct users to use forward slashes only, but when copying a file path from Windows file explorer it always uses backslashes and this the behavior expected from users.
Does anyone know why this happens and how to stop it or at least circumvent it?

Revit Ironpython Shell - Parsing a list of filenames with a number after a backslash in the path

I want to read in a list of files (inc path) from either a spreadsheet or a text file for some downstream processing. The list has been generated as a log from another process and the path includes a 2 digit year folder followed by a project number folder as follows:
\\servername\projects\19\1901001\project files\filetobeprocessed.abc
The problem is as soon as the above string is read in, it is interpreted as
\\servername\\projects\x019\x01901001\\project files\x0ciletobeprocessed.abc
Which then means that I cannot use the path to access the file.
Assigning the path string to a variable, I have tried:
thePath = repr(pathreadfromfile)
After assigning the path string I have tried fixing the string using
thePath.replace('\x0','\\')
thePath.replace('\\x0','\\')
thePath.replace(r'\x0','\\')
Nothing seems to fix the path so that it can be used to open the file.
I can't find anything in either python or Ironpython that suggests a fix for this programatically. I know that you can fix this is the path is known within the code by using r'' to use raw text to create the path.
Any help appreciated
Obviously, the backslash \ is interpreted as an escape character.
For a really simple solution, hopefully the simplest, I would suggest using forward slash / for all your path separators instead of backslash.
If you really need the backslash somewhere further down the road, you can replace them back again.

Julia - Platform specific file paths

Whats the most clever (or preferably the right way) to handle different file system paths?
i.e
Windows
cd("Folder\\\\file.jl") #this becomes "\\"
Unix
cd("Folder/file.jl")
The only solution that comes to mind is declairing a global variable at runtime
#windows_only global slash = "\\"
#linux_only global slash = "/"
but that looks awfully dodgy
joinpath("Folder","file.jl") should do the trick.
From the REPL ?joinpath gives:
joinpath(parts...) -> AbstractString
Join path components into a full path. If some argument is an absolute
path, then prior components are dropped.
So, if needed more than two path parts can be joined as in:
joinpath("dir1","dir2","file1") == "dir1/dir2/file1" (on a Linux machine)

urlreq.pathname2url not returning enough slashes?

Windows 7, Python 3.3. I'm using the following method to generate URLs to files and folders on our shared drive:
import urllib.request as urlreq
...
urlreq.urljoin('file:', urlreq.pathname2url(path))
If path starts with a drive letter, then the above adds three slashes to the front and returns:
file:///Z:/foo
Which is exactly what I need. But if path starts with our network path "//WDSHARESPACE" (Correction: "\WDSHARESPACE") then I'm getting
file://WDSHARESPACE/Public/foo
Which works with IE, but not with Firefox. (Firefox wants the three slashes, plus the original two), so:
file://///WDSHARESPACE/Public/foo
Is there an elegant way to accomplish this, or do I need to test for the different cases? I'm not real strong in HTML coding, so would prefer not to.
From the docs:
Convert the pathname path from the local syntax for a path to the form used in the path component of a URL
The 'local syntax' on windows uses backslashes, not forward slashes. So if you pass //WDSHARESPACE, the forward slashes are not treated specially in any way.
Just have a look at the implementation to see what's really going on. If the path doesn't start with a drive letters or two backslashes, the function just converts backslashes to forward slathes and quotes the rest.
Also note this part of the docstring:
not recommended for general use

Why won't applications in Program Files run using os.execute in lua?

I'm trying to run an executable using Lua's os.execute() function. If I do something like the following it does not work:
os.execute("C:\\\Program Files\\\Movie Maker\\\moviemk.exe")
However, if I put my lua file in the same path that moviemk.exe is in then it can call it.
Any ideas why this may be?
P.S. I'm using Windows XP SP3
This is a classic problem with the command shell. It isn't really a Windows specific problem, except that on *nix, people never really got into the habit of putting spaces in file names, and Windows puts spaces in several default system places such as C:\Program Files.
What is happening is that os.execute(str) is implemented in terms of the ANSI C function system(str), which on Windows tries to duplicate the effect of typing "cmd /C "..str to the command prompt. (On *nix, it uses /bin/sh -c instead of cmd /C.)
The classic problem is that this has to split the complete command string at whitespace to decide what program to run, and what its arguments are.
Your original example: os.execute("C:\\Program Files\\Movie Maker\\moviemk.exe") effectively became cmd /c c:\program files\movie maker\moviemk.exe, which after splitting it up on whitespace, CMD tried to find a program named c:\program to execute with arguments named files\movie and maker\moviemk.exe. This isn't what you intended.
The solution is to be much more defensive about quoting.
I would write this as:
os.execute [["C:\Program Files\Movie Maker\Moviemk.exe"]]
If there were additional command line arguments to be supplied, I would use double-quotes around each, and a single space between arguments. Using the long string syntax [[...]] has the advantage that backslash isn't a special character, so you don't need extra leaning toothpicks making it harder to read the string literal.
Using double quotes around each argument should work on both Windows and *nix, although it is harder to find commands that are the same on both platforms, of course.
One other detail to be aware of is that \Programs Files might not be on C:. There might not even be a disk named C:. (My work PC boots from E: and I find more buggy programs that way.) The easiest way to learn the correct pathname is to just use the environment variable ProgramFiles. There are many other ways.
Try:
os.execute("C:\\Program Files\\Movie Maker\\moviemk.exe")
or:
os.execute("C:/Program Files/Movie Maker/moviemk.exe")
The '\' character is used for escape characters in Lua.

Resources