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)
Related
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.
I'm trying to copy a file from one directory to another. After the copy, I want to delete the original file. The expected result is that the source file no longer exists and the destination file does exist. The actual result is that the destination file exists and that an empty source file exists. Watching the directory during execution, the source file initially disappears then upon exiting the program it reappears with a length of zero.
Here's sample code:
Imports System.IO
Module Module1
Sub Main()
Dim sourceFileName As String = "c:\TestDir\source\TestFile.txt"
Dim destFileName As String = "c:\TestDir\destination\TestFile.txt"
System.IO.File.Copy(sourceFileName, destFileName)
System.IO.File.Delete(sourceFileName)
End Sub
End Module
If I were to remove the System.IO.Copy, the zero-length file does not appear. So it seems to have something to do with the combination of copying and deleting.
Are my expectations amiss? I realize I can delete the destination if it exists then Move the file, but I would like to understand why my sample does not work as I expect it. Thanks for any insight.
Try using the FileSystem.DeleteFile method from here:
https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.fileio.filesystem.deletefile?view=netframework-4.7.2
This should completely remove the file.
Has the Read-Only attribute been applied to your test file? If so, you can try something like this:
File.Copy(sourceFileName, destFileName)
File.SetAttributes(sourceFileName, FileAttributes.Normal);
File.Delete(sourceFileName)
Is there a reason you're not just using File.Move?
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.
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'.
I wrote myself a little downloading application so that I could easily grab a set of files from my server and put them all onto a new pc with a clean install of Windows, without actually going on the net. Unfortunately I'm having problems creating the folder I want to put them in and am unsure how to go about it.
I want my program to download the apps to program files\any name here\
So basically I need a function that checks if a folder exists, and if it doesn't it creates it.
If Not System.IO.Directory.Exists(YourPath) Then
System.IO.Directory.CreateDirectory(YourPath)
End If
Under System.IO, there is a class called Directory.
Do the following:
If Not Directory.Exists(path) Then
Directory.CreateDirectory(path)
End If
It will ensure that the directory is there.
Try the System.IO.DirectoryInfo class.
The sample from MSDN:
Imports System
Imports System.IO
Public Class Test
Public Shared Sub Main()
' Specify the directories you want to manipulate.
Dim di As DirectoryInfo = New DirectoryInfo("c:\MyDir")
Try
' Determine whether the directory exists.
If di.Exists Then
' Indicate that it already exists.
Console.WriteLine("That path exists already.")
Return
End If
' Try to create the directory.
di.Create()
Console.WriteLine("The directory was created successfully.")
' Delete the directory.
di.Delete()
Console.WriteLine("The directory was deleted successfully.")
Catch e As Exception
Console.WriteLine("The process failed: {0}", e.ToString())
End Try
End Sub
End Class
Since the question didn't specify .NET, this should work in VBScript or VB6.
Dim objFSO, strFolder
strFolder = "C:\Temp"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FolderExists(strFolder) Then
objFSO.CreateFolder strFolder
End If
Try this: Directory.Exists(TheFolderName) and Directory.CreateDirectory(TheFolderName)
(You may need: Imports System.IO)
VB.NET? System.IO.Directory.Exists(string path)
Directory.CreateDirectory() should do it.
http://msdn.microsoft.com/en-us/library/system.io.directory.createdirectory(VS.71).aspx
Also, in Vista, you probably cannot write into C: directly unless you run it as an admin, so you might just want to bypass that and create the dir you want in a sub-dir of C: (which i'd say is a good practice to be followed anyways. -- its unbelievable how many people just dump crap onto C:
Hope that helps.
(imports System.IO)
if Not Directory.Exists(Path) then
Directory.CreateDirectory(Path)
end if
If Not Directory.Exists(somePath) then
Directory.CreateDirectory(somePath)
End If
You should try using the File System Object or FSO. There are many methods belonging to this object that check if folders exist as well as creating new folders.
I see how this would work, what would be the process to create a dialog box that allows the user name the folder and place it where you want to.
Cheers
Just do this:
Dim sPath As String = "Folder path here"
If (My.Computer.FileSystem.DirectoryExists(sPath) = False) Then
My.Computer.FileSystem.CreateDirectory(sPath + "/<Folder name>")
Else
'Something else happens, because the folder exists
End If
I declared the folder path as a String (sPath) so that way if you do use it multiple times it can be changed easily but also it can be changed through the program itself.
Hope it helps!
-nfell2009