finding file names which consist its name as variable - vb.net

i am using visual studio 2010
i need a expression to find a file name for eg *.xml is used for finding file name "test.xml" so what will be the expression to find a file name 4374573.xml
i have tried
File.ReadAllLines(TextBox1.Text & "*.xml") but it is only applicable for characters anything for variables.
any help will be appreciable

If you are looking how to find files on disk, you can use the GetFiles method as in how-to-use-directory-getfiles. For more details check:MSDN-GetFiles.

Not sure if this is what you are looking for
Dim path as String = "C:\YourFolder"
Dim fileCollection = My.Computer.FileSystem.GetFiles(path, FileIO.SearchOption.SearchTopLevelOnly, "*.XML")
It will search for every .XML file at the specified path and returns the file list in fileCollection

Related

How can I get the names a files inside a directory

Good day all
I have a test path name ""C:\test". There are the following folders in my directory:
importantStuff
UselessStuff
TopSecret
My question is this: How can I have Visual Basic return these exact names if I input the path "C:\test"? I have tried to use Directory.GetFiles(path), but it returns the path of those folders, and not their names. I'm stuck with this.
Just as a note: I am trying to port a program I wrote in python to vb.net. In python there is the function os.listdir(path). I essentialy want the vb.net equivalent of this
Try this
Dim direct As New DirectoryInfo(".\")
'Get the files based on .txt extension
Dim files As FileInfo() = direct.GetFiles("*.*")
'loop through each files and add it to Listbox control
For Each file As FileInfo In files
ComboBox.Items.Add(System.IO.Path.GetFileNameWithoutExtension(file.Name))
Next

VB.net FileSystem.rename - old path knowing only a substring

How can I rename a file that I don't know the full name, but I only know that it begin with a base string?
I must rename a file in a folder, that begins with a default string and then has extra unknow chars. I'm sure in that folder will be only one file beginning with that string.
It would be something like searching "string*.txt" and rename it with "string.txt", but FileSystem.rename doesn't accept oldPath with "*" as argument.
Dim _files as String() = IO.Directory.GetFiles("c:\temp\", "string*.txt")
IO.File.Move(_files(0), "c:\temp\newfilename.txt")
Still needs some checking if file found, etc. but this should work
You need to loop through all the files in your given directory, if one name matches then you know it's your file.
Structure of the code might look like this :
Function LookForName(Path As String) As String
'For Each File in your path
'If the name starts with "string" and ends with ".txt"
'You can return this filename
End Function
'You call LookForName with a given path
'You rename the returned file

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

Read File name and put it into Variable

I am trying to find a way to read a specific file name in a directory and then put the file name into a variable. In my program I have a batch file that zips a folder called logs and then changes the name of zip to username_date_time.zip.
So basically if the filename was jobs_03152015_1315.zip I would want the entire file name but not the path stored into a variable. The file starts off on the user's local machine. It is then uploaded into a network share.
The network path will be uploaded to a database for others to view. I want to just add the unique file name to the end of a pre set path. Here is the code I am using.
Dim filePath As String = "c:\temp\logs\"
If (System.IO.Directory.Exists(filePath)) Then
For Each file As String In filePath
If file.Contains(".zip") Then
Dim zip As String = file
testbox.Text = zip
Exit For
End If
Next
End If
You can enumerate all files using the GetFiles() method of the static (shared in VB) Directory class, see msdn.
Assuming you have more then 1 file I add the files to a listbox control. But you could change that if you wish. I get the filename using the Path class. You will find a lot other helpful functions in this class.
Dim searchPath = "C:\temp\logs"
Dim files = Directory.GetFiles(path, "*.zip")
For Each file In files
listbox1.Items.Add(path.GetFileName(file))
Next
This should do it.
Your loop doesn't read anything. The string variable filePath is just the name of the directory, not a list of the files in that directory. Calling For Each over a string simply enumerates each character contained in the string
To get a list of files contained in that folder you need Directory.EnumerateFiles() and pass the filePath variable and the extension required.
It seems that you are interested only to find if that folder contains at least one file with zip extension. If this is the case then you could remove the explicit loop and write simply
Dim file = Directory.EnumerateFiles(filePath, "*.zip").FirstOrDefault()
If file IsNot Nothing Then
testbox.Text = Path.GetFileName(file)
End If
Using Path.GetFileName will return just the file without the path part
Try this code :
Dim FilePath As String = "c:\temp\logs\"
If (System.IO.Directory.Exists(FilePath)) Then
For Each File As String In System.IO.Directory.EnumerateFiles(FilePath)
If File.Contains(".zip") Then
Dim info As New System.IO.FileInfo(File)
zip = info.Name
testbox.Text = zip
Exit For
End If
Next
End If

Value of type '1-dimensional array of Byte' cannot be converted to 'String'

I am trying to open a file from my hard drive, but then to copy that file to My Resources, incase of the file not working on someone else's computer, this is the coding that I am using, which is giving me the above error:
Process.Start("C:\Users\jmahone\AppData\Roaming\Local Libraries\Local Documents\Class List.xlsx")
FileCopy("C:\Users\jmahone\AppData\Roaming\Local Libraries\Local Documents\Class List.xlsx", My.Resources.Class_List)
And I am getting this error:
Value of type '1-dimensional array of Byte' cannot be converted to 'String'
If anyone could give me an alternative solution.
Thank You
J Mahone :-)
FileCopy is a leftover function for VB6 compatibility. It was used to copy a source file to a destination file.
It requires two parameters.
The first one is a string containing the name of the source file, the second one is another string with the name of the target destination file (with or without pathname specifier)
In your case the second parameter is not a string, but a reference to block of bytes stored in the resources of your application. Thus you get the error. You need to specify a string containing the destination file name. (Es. "C:\Users\jmahone\documents\MyResources\Class List.xlsx")
If you just want to open the XLSX file, then you need to install it on the user computer on a well know directory like the one listed on the Environment.SpecialFolder enum and then open the file from one of those folders that should exists also on the target machine.
For example:
Dim dataFolder = Environment.GetFolderPath(Environment.SpecialFolder.CommonAppData)
Dim myAppDataFolder = Path.Combine(dataFolder, "MyAppData")
Directory.CreateDirectory(myAppDataFolder)
Dim myExcelFile = Path.Combine(myAppDataFolder, "Class List.XLSX")
Process.Start(myExcelFile)
See docs on:
Directory.CreateDirectory
Path class
Environment.SpecialFolder