Copying files back to a Specific Folder - vb.net

This program extracts files from a folder that has been modified today, and after the files are placed into another folder a batch file then deletes the rest of the non-modified files in that source folder.
The last thing my program is supposed to do is copy files from separate folder, and place them back into that source folder.
But my program only extracts the modified files, deletes the rest of the files in that folder, but when I run the program to also copy and place the new files into the source folder it just doesn't do it. Does anyone know why?
Imports System.IO
Public Class frmExtractionator
' Dim txtFiles1 As Control
Dim sourceDirectory As String = "F:\CopierFolderforTestDriveCapstone"
Dim archiveDirectory As String = "F:\FilesExtracted"
Dim originalDirectory As String = "F:\OriginalTestFiles"
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
Try
Dim txtFiles = Directory.EnumerateFiles(sourceDirectory)
If (Not System.IO.Directory.Exists(archiveDirectory)) Then
System.IO.Directory.CreateDirectory(archiveDirectory)
End If
For Each currentFileLoc As String In txtFiles
Dim fileName = currentFileLoc.Substring(sourceDirectory.Length + 1)
If (IO.File.GetLastWriteTime(currentFileLoc).ToString("MM/dd/yyyy") = DateTime.Now.ToString("MM/dd/yyyy")) Then
MessageBox.Show(currentFileLoc & " moved", "Moved Succesfully")
File.Move(currentFileLoc, Path.Combine(archiveDirectory, fileName))
End If
Next
Catch eT As Exception
Console.WriteLine(eT.Message)
End Try
System.Diagnostics.Process.Start("F:\poop.bat")
Try
Dim txtFiles2 = Directory.EnumerateFiles(originalDirectory)
For Each currentFileLoc2 As String In txtFiles2
Dim fileName = currentFileLoc2.Substring(originalDirectory.Length + 1)
File.Move(currentFileLoc2, Path.Combine(sourceDirectory, fileName))
Next
Catch eT As Exception
Console.WriteLine(eT.Message)
End Try
End Sub
End Class

Change
Dim fileName = currentFileLoc2.Substring(originalDirectory.Length + 1)
to
Dim FileName = IO.Path.GetFileName(currentFileLoc2)

Related

Get all the folders and sub-folders and files in a directory Vb.Net

how can i get all the folders and sub-folders and files in a specific path directory?,
example:
+ folder1
- exe1
+ folder2
- exe1
- exe2
+ folder3
- exe1
+ folder2
- exe1
+ folder3
+ folder4
im using right now on:
Sub GetDirectories(ByVal StartPath As String)
For Each Dir As String In IO.Directory.GetDirectories(StartPath)
ListBox1.Items.Add(Dir)
ListBox1.Items.AddRange(IO.Directory.GetFiles(StartPath))
ListBox1.Items.AddRange(IO.Directory.GetFiles(Dir))
Next
End Sub
and:
Dim files() As String = e.Data.GetData(DataFormats.FileDrop)
For Each path In files
For Each Dir As String In IO.Directory.GetDirectories(path)
GetDirectories(path)
Next
Next
but it not giving me all the files from the other sub-folders.
Edit:
using with listbox and i want see the full path when drop in the folder and after this giving all the sub folders and files
You can better use the TreeView to get more convenient results.
Simply get a TreeView from the ToolBox and use the following code for listing a specific directory and sub-directories (including files):
Private Enum ItemType
Drive
Folder
File
End Enum
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim node As TreeNode =
TreeView1.Nodes.Add("Hello") ' Specifying Folder Names
node.Tag = ItemType.Folder
node.Nodes.Add("FILLER")
End Sub
Private Sub file_view_tree_BeforeExpand(sender As Object, e As TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand
Dim currentNode As TreeNode = e.Node
currentNode.Nodes.Clear()
Try
'Now go get all the files and folders
Dim fullPathString As String = currentNode.FullPath
'Handle each folder
For Each folderString As String In
Directory.GetDirectories(fullPathString)
Dim newNode As TreeNode =
currentNode.Nodes.Add(Path.GetFileName(folderString))
Dim x As String = Path.GetFileName("")
newNode.Tag = ItemType.File
newNode.Nodes.Add("FILLER")
Next
'Handle each file
For Each fileString As String In
Directory.GetFiles(fullPathString)
'Get just the file name portion (without the path) :
Dim newNode As TreeNode =
currentNode.Nodes.Add(Path.GetFileName(fileString))
newNode.Tag = ItemType.File
Next
Catch ex As Exception
End Try
End Sub
Note: I've modified this thread of Stack Overflow and customized as per of your requirement.

Directory.GetFiles - SearchOption.AllDirectories Does not give files in subfolders (vb)

Directory.GetFiles - SearchOption.AllDirectories Does not give files that are located in subfolders. I'm trying to copy al .jpg files from a usb to the computer but when files are located in sub-folders, it does not copy them.
Private Sub ButtonStart_Click(sender As Object, e As EventArgs) Handles ButtonStart.Click
Dim sourceDir As String = ComboBoxUsbSelect.Text
Dim backupDir As String = FileDestination
Try
Dim picList As String() = Directory.GetFiles(sourceDir, "*.jpg", SearchOption.AllDirectories)
' Copy picture files.
For Each f As String In picList
'Remove path from the file name.
Dim fName As String = f.Substring(sourceDir.Length)
' Use the Path.Combine method to safely append the file name to the path.
' Will overwrite if the destination file already exists.
If delete = False Then
File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName), True)
Else
File.Move(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName))
End If
Next
Catch dirNotFound As DirectoryNotFoundException
Console.WriteLine(dirNotFound.Message)
End Try
MessageBox.Show("Done!")
End Sub

Files from sub directories

Can somebody help me to implement to add files from sub directories to my listbox ? I have looked for some information from google, but source code from those examples was really diffrent than my and I'm newbie in VB.NET. I think it may be the System.IO.SearchOption.AllDirectories but I have no clue how to implement it in my code.
Private Sub ButtonFolder_Click(sender As System.Object, e As System.EventArgs) Handles ButtonFolder.Click
FolderBrowserDialog1.ShowDialog()
FilePathLabel.Text = System.IO.Path.GetFileName(FolderBrowserDialog1.SelectedPath)
Dim folder As New IO.DirectoryInfo(System.IO.Path.GetFullPath(FolderBrowserDialog1.SelectedPath))
Dim arrfile() As IO.FileInfo
Dim file As IO.FileInfo
arrfile = folder.GetFiles("*.*")
dicPaths.Clear()
For Each file In arrfile
'ListBox1.Items.Add(file.FullName)
dicPaths.Add(file.Name, file.FullName)
Next file
For Each item As String In dicPaths.Keys
ListBox1.Items.Add(item)
Next item
Label1.Text = "Total Items : " + ListBox1.Items.Count.ToString
End Sub
Try This:
Public Function FindFiles(ByVal Path As String) As Boolean
Dim Directories As New IO.DirectoryInfo(Path)
Dim Directory As IO.DirectoryInfo
Dim File As IO.FileInfo
For Each Directory In Directories.GetDirectories
For Each File In Directory.GetFiles
ListBox1.Items.Add(File.Name)
Next
If Directory.GetDirectories.Length > 0 Then
FindFiles(Directory.FullName)
End If
Next
End Function
Usage: FindFiles(FolderBrowserDialog1.SelectedPath)

Save file in a new folder?

I'm looking to make a program that will open a file and upon saving it, the program will save it into a new folder, leaving the original file unchanged.
However, none of the codes I've tried using have worked.
How do I make a new folder that will go into the FilePath of the loaded file?
How do I then save into that folder?
I tried this and got the error.. "False was not found or could not be accessed"
Private Sub SaveChangesToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveChangesToolStripMenuItem1.Click
newFilePath = FilePath + "\" + newFolderName + "Folder"
Try
Dim Writer As New PackageIO.Writer(newFilePath = orgFilePath + "\" + newFolderName, "Folder 1", PackageIO.Endian.Big)
System.IO.Directory.CreateDirectory(newFilePath)
I tried this code here and it seemed to be the closest to working, but said the file path wasn't valid; I'm assuming because it's including the file they selected, not just the folders involved.
Private Sub SaveChangesToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveChangesToolStripMenuItem1.Click
newFilePath = FilePath + "\" + newFolderName + "Folder"
Try
Dim Writer As New PackageIO.Writer(newFilePath, PackageIO.Endian.Big)
System.IO.Directory.CreateDirectory(newFilePath)
Try something along these lines:
Dim orgFilePath as String
Dim newFolderName as String
Dim newFilePath as String
'you will need to set the path of the original file and give a name for the new folder
newFilePath = orgFilePath + "\" + newFolderName
'creates new directory (folder)
System.IO.Directory.CreateDirectory(newFilePath)
'then put the code to save your file to the newFilePath variable
Hope that helps

Causing a batch file to finish its operations, before continuing?

The following code will extract files modified today from the SourceDirectory and place them into the FilesExtracted folder, then the batch file will delete the rest of the files in the sourceDirectory. But after that is all done a brand new set of files will be copied from the OriginalTestFiles folder and put into the sourceDirectory, but its does not do it. Does anyone think that it could be because the batch files hasn't stopped its operations and is still deleting the files in the sourceDirectory, or is there another problem. Thank You all!
Imports System.IO
Public Class frmExtractionator
' Dim txtFiles1 As Control
Dim sourceDirectory As String = "F:\CopierFolderforTestDriveCapstone"
Dim archiveDirectory As String = "F:\FilesExtracted"
Dim originalDirectory As String = "F:\OriginalTestFiles"
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
Try
Dim txtFiles = Directory.EnumerateFiles(sourceDirectory)
If (Not System.IO.Directory.Exists(archiveDirectory)) Then
System.IO.Directory.CreateDirectory(archiveDirectory)
End If
For Each currentFileLoc As String In txtFiles
Dim fileName = currentFileLoc.Substring(sourceDirectory.Length + 1)
If (IO.File.GetLastWriteTime(currentFileLoc).ToString("MM/dd/yyyy") = DateTime.Now.ToString("MM/dd/yyyy")) Then
MessageBox.Show(currentFileLoc & " moved", "Moved Succesfully")
File.Move(currentFileLoc, Path.Combine(archiveDirectory, fileName))
End If
Next
Catch eT As Exception
Console.WriteLine(eT.Message)
End Try
System.Diagnostics.Process.Start("F:\poop.bat")
Try
Dim txtFiles2 = Directory.EnumerateFiles(originalDirectory)
For Each currentFileLoc2 As String In txtFiles2
Dim fileName = currentFileLoc2.Substring(originalDirectory.Length + 1)
File.Move(currentFileLoc2, Path.Combine(sourceDirectory, fileName))
Next
Catch eT As Exception
Console.WriteLine(eT.Message)
End Try
End Sub
End Class
You can get the process as a variable once you start it and wait for it to exit:
Dim theProcess As Process = Process.Start("F:\poop.bat")
theProcess.WaitForExit()
Try monitoring the Process for completion...
'Start the process.
Dim Proc = Process.Start("F:\poop.bat")
'Wait for the window to finish loading.
Proc.WaitForInputIdle()
'Wait for the process to end.
Proc.WaitForExit()
...