Get full path to .exe file if only a subfolder path to it is given - vb.net

I have to find the rscript.exe that exists in a folder called bin but I do not have the full reference to this folder.
Full Reference would be:
C:\Program Files\R\R-3.5.3\bin
but I do not know the version part beforehand. So I must go to
C:\Program Files\R and search the subfolders for the rscript.exe and recieve the full path to it.
I have already searched but all I could find was how to find out the path of the running application via reflections and tried out to rewrite the code without any solution. Could anyone help me here?

You can use the Directory.GetDirectories method with the searchPattern argument and loop throught the subdirectories:
Dim rootPath As String = "C:\Program Files\R"
If Directory.Exists(rootPath) Then
Dim currPath As String
For Each subFolder As String In IO.Directory.GetDirectories(rootPath, "R-*", SearchOption.TopDirectoryOnly)
currPath = IO.Path.Combine(subFolder, "bin", "rscript.exe")
If IO.File.Exists(currPath) Then
MessageBox.Show("File found at " & currPath)
End If
Next
End If

Related

Check if directory exists, but string is relative path

I am currently writing a program to check if hyperlinks in a file are broken. Some hyperlinks go directly to folders on a mapped drive.
I want to use dir() to check if the folder exists, but Excel shortens the string to the relative version "../../Random folder/Another random folder"
This string returns "" so my function thinks the folder does not exist, but it does and its link functions properly.
Any help is greatly appreciated.
Thanks!
I believe you need to use the overloaded function of Dir and specify vbDirectory as the additional argument. I believe it should look something like this:
If Dir("X:\random directory\other random directory", vbDirectory) = "" Then

vba dir function error 5174

I have been getting a runtime error 5174. In the directory there are .docx and .docm files. I have tried to add .doc files, as I read .docx are not supported by the dir function. Following adding the file, the code will go through all files in the directory like it should. If I run it again, it will fail with an error 5174. Any help would be greatly appreciated.
sMyDir = "C:\weekly\" & "*.doc?"
sDocName = Dir(sMyDir)
While sDocName <> ""
Documents.Open FileName:=sDocName, Visible:=False
' Does stuff
sDocName = Dir()
Error 5174 indicates file not found, which means it is possible that the directory is incorrect. Try using LIKE:
sMyDir LIKE "C:\weekly\" & "*.doc?"
Edit: Your sDocName does not provide with a full path as well
The result was the Dir function returns the file name only. So when I used the open method I added the path before the variable which stored the filename. I just needed to read the documentation on the Dir function.
Thanks

How do you call files without drive path

I don't know how to explain this right but I would like to ask you guys how do you call all the files in a folder without the "C:\" directory so wherever I move my project in another computer I don't have to edit the path directory in my project is that even possible? I'm using visual basic 2008.
I would advice to use AppDomain.CurrentDomain.BaseDirectory , this code will return the directory where the current application is located, lets say you want to get all the directories in the folder where the application is located, then you will get something like this.
Dim currentpath As String = AppDomain.CurrentDomain.BaseDirectory 'Get the directory where the application is located.
Dim Directories() As String = Directory.GetDirectories(currentpath) 'get all the directories what are in the current location of the application
'
Console.WriteLine("This application is located at:: {0}", currentpath)
'
If Directories.Length = 0 Then
Console.WriteLine("There aren't any folders found in the location of the application.")
Else
'
Console.WriteLine("The follow folder(s) are found.")
'
For Each folder In Directory.GetDirectories(currentpath)
Console.WriteLine(folder)
Next
'
End If
'
Console.ReadLine()
Output:
This application is located at :: C:Users\Kona\Desktop\
The follow folder(s) are found.
C:\Users\Kona\Desktop\C#
C:\Users\Kona\Desktop\VB
C:\Users\Kona\Desktop\Haskell
C:\Users\Kona\Desktop\Java
You can easily just use the:
Environment.CurrentDirectory
variable.
(use ..\ to go up a directory...)

VB.NET | Get current user profile folder

How can I use 'path' to go to the user's current profile?
For example, I have this code:
Dim fso, fldr
fso = CreateObject("Scripting.FilesystemObject")
fldr = fso.GetFolder("C:\Documents and Settings\%UserProfile%\Local Settings\TEST")
'delete subfolders
For Each subf In fldr.SubFolders
subf.Delete(True)
Next
'delete subfiles
For Each fsofile In fldr.Files
fsofile.Delete(True)
Next
I've tried this way and the path is unknown.
How can I make C:\Documents and Settings\???\Local Settings\TEST
to go to the current user's folder?
Use the 'userprofile' environment variable...
MsgBox(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile))
On my Windows 8.1, I cannot access Local Settings folder. It's right protected. As far as getting the right folder path is concerned, I think the answer is already posted above. Just append your custom folder path to the UserProfile Folder path returned by Environment of DotNet.
Something like:
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Local Settings\TEST"
Get the Local AppData folder:
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
And then concatenate it with your "TEST" folder using Path.Combine method.
See SpecialFolders and Combine msdn pages.
This worked for me, using VB6.0 Sp6
Dim myDocuPath As String
myDocuPath = Environ$("USERPROFILE") & "\My Documents"

How to create a folder and a file in my project root directory

I have a vb .net application which is stored in my D: drive. I need to create a folder and a file in my project root directory. I used the following code to create the folder as well as the file.
If (Not System.IO.Directory.Exists("\test\")) Then
System.IO.Directory.CreateDirectory("\test\")
If (Not System.IO.File.Exists("\test\output.txt")) Then
System.IO.File.Create("\test\output.txt")
End If
End If
But the folder and the file is created in C: drive.
I used Dim fullpath As String = Path.GetFullPath("\test\output.txt") to identify where the file and folder is created.
I want to create it in my project root directory. That means I need to create the folder using a relative path criteria.
If you want the directory of the executable or dll of your running application then:
Dim spath As String
spath = System.IO.Path.GetDirectoryName( _
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
If (Not System.IO.Directory.Exists(Path.Combine(spath, "test"))) Then
System.IO.Directory.CreateDirectory(Path.Combine(spath, "test"))
If (Not System.IO.File.Exists(Path.Combine(spath, "test\output.txt"))) Then
System.IO.File.Create(Path.Combine(spath, "test\output.txt"))
End If
End If
If you want the solution/project directory then:
Dim spath As String = Directory.GetCurrentDirectory
If (Not System.IO.Directory.Exists(Path.Combine(spath, "test"))) Then
System.IO.Directory.CreateDirectory(Path.Combine(spath, "test"))
If (Not System.IO.File.Exists(Path.Combine(spath, "test\output.txt"))) Then
System.IO.File.Create(Path.Combine(spath, "test\output.txt"))
End If
End If
You could use this path, and work from there...
MsgBox(Application.StartupPath)