I move a file to the temp folder (Path.GetTempPath). Later, the user wishes to restore the file to its original location using an application option. I can get the file name without any trouble. How do I get the original file path? In the file properties there is a Path item but it is always the current path. Since I am already setting an EXIF tag, I thought of using some other EXIF tag with the original path and file name. EXIF seems counter productive and there must be another way. I have looked at similar questions but none seem to solve this issue.
Why not just put another file in the temp folder with the same name, different extension and inside it put the original location?
Dim toMove = "c:\myfolder\some.txt"
Dim temp = Path.GetTempFileName() 'string of eg c:\user\appdata\temp\file.tmp..
Dim loc = Path.ChangeExtension(temp, "loc") 'string of eg c:\user\appdata\temp\file.loc..
File.Move(toMove, temp) 'move c:\myfolder\some.txt -> c:\user\appdata\temp\file.tmp
File.WriteAllText(loc, toMove) 'write "c:\myfolder\some.txt" string into file at c:\user\appdata\temp\file.loc
And then to move it back
Dim toMoveBack = "..some tmp file path.." 'e.g. c:\user\appdata\temp\file.tmp
Dim loc = Path.ChangeExtension(toMoveBack, "loc") 'e.g. c:\user\appdata\temp\file.loc
Dim origLoc = File.ReadAllText(loc) 'origLoc is now a value of c:\myfolder\some.txt
File.Move(toMoveBack, origLoc) 'move c:\user\appdata\temp\file.tmp -> c:\myfolder\some.txt
File.Delete(loc) 'remove c:\user\appdata\temp\file.loc
Related
How can I add the file creation date filter for files created today and tomorrow?
This part don't work and I need to add files created today and tomorrow:
.Where(Function(file) New FileInfo(file).CreationTime.Date = DateTime.Today.Date)
Dim pdfList As String() = _
Directory.GetFiles(sourceDir, "*.PDF").Where(Function(file) New
FileInfo(file).CreationTime.Date = _
DateTime.Today.Date)
For Each f As String In pdfList
'Remove path from the file name.
Dim fName As String = f.Substring(sourceDir.Length + 1)
' Use the Path.Combine method to safely append the file name to the path.
' Will overwrite if the destination file already exists.
File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, _
fName), True)
Next
Dim folder As New DirectoryInfo(sourceFolderPath)
Dim files = folder.EnumerateFiles("*.pdf")
.Where(Function(fi) fi.CreationTime.Date = Date.Today)
For Each file In files
file.CopyTo(Path.Combine(backupFolderPath, file.Name))
Next
There are a few things to note here:
We are creating a DirectoryInfo and using it to get FileInfo objects. Given that you were creating a FileInfo for each file anyway, it's silly not to do this. That FileInfo object gives you the creation time, file name without the folder path and a method to copy the file.
We are calling EnumerateFiles rather than GetFiles. The latter returns an array containing all files, but we don't want all files in this case. If you're going to filter then enumeration is better, because it gets each file one by one, processes a file and discards it before getting the next. That's more efficient. In this case, we don't even call ToArray or ToList, so the files aren't actually retrieved until the loop and then they are retrieved and processed one by one.
The is no point using Date.Today.Date. Today has already had the time zeroed. Internally, Date.Today uses Date.Now.Date.
I want to list all files names exist under folder in hard drive with vb.net , and i don't know how.First ,i choose a folder with folderbrowser component, next,i list all files
Here is my code (only for choose a folder)
dossier_disque.ShowDialog()
txt_folder.Text = dossier_disque.SelectedPath
for list all files , i tried to use for each , but it's not correct
my code when i tried to list file
Dim files() As String = Directory.GetFiles(txt_folder.Text)
For Each a In CStr(files.Count)
folder_hard.Rows.Add(Directory.GetFiles(txt_folder.Text))
Next
folder_hard is a grid name
txt_folder is a name of a folder path
With this code , the result , i can see only the first file twice in grid
There is a problem with your for each loop:
CStr() converts values into strings.
So your for loop is looping through each char in the string of the number of files in the files array.
So change it to:
For Each a In files
Then a will be each file name in the files array.
If you want to add each to your grid you need to change that line to :
folder_hard.Rows.Add(a)
So this should work:
Dim files() As String = Directory.GetFiles(txt_folder.Text)
For Each a In files
folder_hard.Rows.Add(a)
Next
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
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
Ok, here is my story:
I am building a fileviewer, and i am trying to delete the selected file in the listview.
when i try to delete it, it gave me an error saying the file wasnt found. I looked at my desktop and the file was there. here is the original code:
dim f as string = lv1.focuseditem.text
my.computer.filesystem.deletfile(f)
lv1.update()
this gave me that error. My updated code is supposed to show me where the computer thinks my file is:
Dim file As String = lv1.FocusedItem.Text
Dim testFile As System.IO.FileInfo
testFile = My.Computer.FileSystem.GetFileInfo(file)
Dim folderPath As String = testFile.DirectoryName
MsgBox(folderPath)
this shows a messagebox that shows the path of:
C:\Users\tgs266\Desktop\SIOS\SIOS\SIOS\obj\Debug\test.txt
but the real file location is:
C:\Users\tgs266\Desktop\test.txt
please help
How are you getting the filenames for the ListView? Is it just the filename and no path?
If, for example, lv1.FocusedItem.Text is "test.txt", and that is the value you use (without the path), by default the program will look in the directory it's executing in. This is most likely why you're seeing C:\Users\tgs266\Desktop\SIOS\SIOS\SIOS\obj\Debug\test.txt as the location, instead of what you expected.
If all the files are on your desktop, you can use Environment.GetFolderPath in conjunction with the Environment.SpecialFolder Enumeration to get the file, like this:
Dim file As String = lv1.FocusedItem.Text
Dim testFile As System.IO.FileInfo
testFile = My.Computer.FileSystem.GetFileInfo(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\" + file)
Dim folderPath As String = testFile.DirectoryName
MsgBox(folderPath)
However, if you're going to have files scattered throughout your system, you'd be better off storing the full path as #Plutonix indicates in his comment.
It looks like your code is looking in your applications path on the server while you want to look at the users desktop location.