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

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

Related

Zip multiple files to one folder

So I am having trouble trying to reconcile this concept as every change I do doesn't seem to fix the issue.
I have multiline textbox and can enter multiple values separated by commas and here are the details:
Each value represents a folder
And each folder has multiple documents/other folders inside
All of these values are in one main directory (lets call it folder path)
For example say I enter in my textbox "65635,65636" each of those represent a folder in the directory i.e. "\folderpath\65635" and "\folderpath\65636", I am trying to zip these whole folders via DotNetZiplib, I know how to do this if I specifically reference one folder but is there anywhere to loop through the textbox to get the names of the folders and have the files added to one zipped folder?
Using zip As New ZipFile = new ZipFile
Dim files() As String = Directory.GetFiles(folderpath & textboxvalue)
For each textboxvalue in directory.getfiles
zip.Addfile(textboxvalue)
The zipfile function I have would know to loop through these as opposed to assuming it's one big file.
You must split textbox values in array first to get numbers separated by comma. Next you will need Combine your folder with this splited text values, creating path correctly.
Check if folder exists in system if Yes then get all files from Directory and for each filename zip it.
Something like this:
Using zip As New ZipFile("your zip filename")
For Each str As String In textboxvalue.Split(",")
Dim path as String = System.IO.Path.Combine(folderpath, str)
If System.IO.Directory.Exists(path) = False Then
Continue For
End If
Dim files() As String = Directory.GetFiles(path)
For Each fileName As String In files
zip.Addfile(fileName)
Next
Next
End Using

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

finding file names which consist its name as variable

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

copy all files with certain extension

Hello I want to copy all files with and a specific extension. I've tried a few things but it isn't working. During my debug I get an exception "Illegal characters used in path" I'm guessing it doesn't like *.xls any suggestions?
First try
My.Computer.FileSystem.CopyFile("C:\test\test\mxw\*.xls\", "C:\workorder1-23\workorder1-23\mxw\", True)
second try
For Each f In Directory.GetFiles("C:\test\test\mxw\*.xls\", CStr(SearchOption.AllDirectories))
If My.Computer.FileSystem.FileExists(f.ToString) Then
File.Copy("C:\test\test\mxw\*.xls\", "C:\workorder1-23\workorder1-23\mxw\", True)
End If
Next
CopyFile copies just one file.
You cannot use it with wildcards to copy a group of files. (The invalid character is probably the wildcard)
And you should not append a backslash at the end of the file.
So let me try to replace your code with this
For Each f In Directory.GetFiles("C:\test\test\mxw", "*.xls", SearchOption.AllDirectories)
If File.Exists(f) Then
File.Copy(f, Path.Combine("C:\workorder1-23\workorder1-23\mxw", Path.GetFileName(f)), True)
End If
Next
Also Directory.GetFiles has three parameters, a path, a pattern and a flag to read subfolders
Since File.Copy does not create directories the provided answer will only work if the target directories exist. Add a Create.Directory(f) before the File.Copy if they don't.

How to specify a path with spaces for StreamWriter

In VB.Net, how do I provide the StreamWriter constructor with a path that includes spaces? StreamWriter("""C:\Users\Public\Public Users\file.txt""") does not work.
Here is a working code example:
Dim fs As New System.IO.StreamWriter("e:\test 123.txt")
fs.Write("hello")
fs.Close()
UPDATE:
The new example for folder with space(s):
'this is your filename
Dim Filename As String = "e:\folder with space\test 123.txt"
'this is your folder
Dim Folderpath As String = System.IO.Path.GetDirectoryName(Filename)
'now do checking if the folder exists, if not create the folder
If System.IO.Directory.Exists(Folderpath) = False Then
System.IO.Directory.CreateDirectory(Folderpath)
End If
'now create the file as usual
Dim fs As New System.IO.StreamWriter("e:\folder with space\test 123.txt")
fs.Write("hello")
fs.Close()
The reason for your code didn't compile because you have not create the folder before creating the file, ie that folder must be existed before you can create your file.
You don't put quotes around the string you pass to the StreamReader constructor. Quotes are only used when you use, say, the command line. Or anything else that uses spaces as separators between arguments. The program requires those double quotes to recognize an argument with an embedded space.
Not necessary here, there's no ambiguity since the argument only takes the path to a single file. The only exception to that rule that I know of is the ProcessStartInfo.Arguments property.
So, just put single double quotes around the string, the syntax that the compiler requires. Your real problem is the name of the folder. Windows Explorer shows a different name for the folders in c:\users\public. For example c:\users\public\videos is displayed in the Explorer window as "Public Videos". It's trying to be helpful by expanding the abbreviated name. Your program however has to use the real folder name. Which is probably "users", not "Public Users". To find out for sure, use the command line (cmd.exe). Use cd \users\public and dir /a.
Last but not least, that folder has a different name on different versions of Windows. You should use Environment.GetFolderPath(). "Public Users" isn't a standard folder name however, not sure why you are using it.