VB.NET Iteration and folder operations - vb.net

I am new to VB. Can anyone help on this, thanks. BTW, it's bloody pain to mess the formatting here :) How to do the indent?
parameters:
source file
target
backup folder
Pseudocode:
get all sub folders under target
for each folder under target
if exists source file
if exists backup folder
replace/copy source file to backup folder
else
create backup folder
replace/copy source file to backup folder
replace source file

Check this methods:
Directory.GetDirectories()
File.Exists()
Directory.Exists()
Directory.CreateDirectory()
File.Copy()
As I told you before on the other posts, just check the methods on System.IO. Its very well documented on MSDN, specially the classes File, Directory and Path.

For getting subfolders refer to this question
You then will want to put the sub folder locations into an array. From this array, loop through each object and so on.
Try using methods under 'File.' as this will include many functions for file processing.
This will require the 'Imports System.IO'.

Related

How to check for any files with FileExists(String) method

I'm trying to automate a document handling process, and I need to check if there are any files inside a certain folder. The process itself removes the files from the folder once it finishes, so I need it to loop back and check if there are any files left.
So far I've been using a sample file like this:
File.Exists("C:\Users\gcaor\Desktop\OC\150.pdf")
150.pdf is the sample file it's searching for, but is there a way to search for any file at all? So that it returns true if there is a file in the folder and false if there isn't
You can use Directory.EnumerateFiles + Any:
Dim anyFileExist = Directory.EnumerateFiles(path).Any()
This is using standard .NET methods and also stops at the first file found.

Showing the File Extensions inside the folder

Currently, I am working on a feature that will make the files inside the folder that will not hide the file extensions using this code.
Imports Microsoft.Win32
Sub SetNoDrives(value As Integer, path as string)
Dim RegPath As String = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced]"
Using Key As RegistryKey = Registry.LocalMachine.OpenSubKey(RegPath)
Key.SetValue("HideFileExt", value, RegistryValueKind.DWord)
End Using
End Sub
the problem is, I don't know where to place the string path (folder path) on the code. the path is the specific location where you will always show the file extensions of the files inside the folder.
Any help will be much appreciated.
The reason you don't know where to put the folder path is because there is nowhere to put the folder path. This is a user-wide option, i.e. either extensions are displayed in every folder or no folder for the current user. You don't get to choose on a folder by folder basis. At least, there is no option in Windows/File Explorer to do that and I've never seen mention of it being possible, even when specifically searching for it.

Copy a File From Resources to Temp Folder

I'm making a VB application and I want to copy a file called "L3nEncrypt.jar" which is in resources of the application to the temp folder.
Right now I have this:
My.Computer.FileSystem.SpecialDirectories.Temp
which returns the temp folder.
I found the solution
Imports System.IO
File.WriteAllBytes(My.Computer.FileSystem.SpecialDirectories.Temp & "/L3nEncrypt.jar", My.Resources.L3nEncrypt)
Use System.IO.File.Copy(SourceFileName,DestinationFileName)
The parameters are full file names of type string. Make sure that you're running the application with admin privileges so that the copying can be successful.

Delete directory on network shared folder

This question is bring from another forum which not having answer yet for my situation.
I have something to do on network shared folder. But when I search on Internet, it giving me a code to do in own computer only. Step that I want to do is:
Check destination (network shared folder) path is empty or not.
Delete folder content (not the main one) eg: "\USER-PC\File\"; the folder "File" no need to deleted, but the content inside is need to deleted.
Copy folder content from source to new destination.
No. 1 and 3 is OK. But No. 2 is not yet found. How to delete a content from directory on Network Shared Folder?
Delete directory code that I use but exception "Could not complete operation since directory is a root directory":
My.Computer.FileSystem.DeleteDirectory(strDestination, FileIO.DeleteDirectoryOption.DeleteAllContents)
Please assist
EDITED:
To delete all files inside the main directory:-
Dim directory As New DirectoryInfo(strDestination)
For Each file As FileInfo In directory.GetFiles()
file.Delete()
Next file
To delete all folders inside the main directory:-
For Each folder As DirectoryInfo In directory.GetDirectories()
folder.Delete(True)
Next folder
Use this instead (it's C#, you'll need to convert it to VB.NET):
DirectoryInfo directory = new DirectoryInfo("\\USER-PC\File");
foreach(FileInfo file in directory.GetFiles()) {
file.Delete();
}

Making folder and copying file

I want my application to look for a file in drive C's specific folder (say C:\myFolder\abc.mdb), if found just give a message if not, make the folder in drive C:\ and then copy the file.
How to do this?
Thanks
Furqan
You could use the File, Directory, and Path objects in the System.IO as shown below:
Imports System.IO
...
Dim path As String = "C:\myFolder\abc.mdb"
If File.Exists(path) Then
'TODO write code to create message'
Else
Dim folder As String = Path.GetDirectoryName(path)
If Not Directory.Exists(folder) then
Directory.CreateDirectory(folder)
End If
'TODO code to copy file from current location to the newly created directory path'
'i.e. File.Copy(FileToCopy, NewCopy)'
End If
I know this post is kind of old. I just wanted to update.
The quickest shortcut that will also create the Destination directory structure and in one line. Use Computer.FileSystem.CopyFile instead of System.IO.
My.Computer.FileSystem.CopyFile(sSourcefile, sDestinationfile)