How To copy folder/file from A to B
Can anyone please teach me,
How can I copy a folder and all the sub folders and files in it, from a location A to a destination B. Replace the old content with this new one.
In another words, I trying to make a back up module for my data folder and files.
:)
Re: How To copy folder/file from A to B
make a new folder copy the folder or any of the sub folders, open your newly created folder then paste it there, if i am assuming that you do not do it in runtime..... :D
Re: How To copy folder/file from A to B
Look for examples of Recursive folder handling on planetsourcecode.com. Then, you can make folders by using the MkDir() command, and copy files by using the FileCopy() method.
Re: How To copy folder/file from A to B
Is the following a valid code, also please tell me how can I delete the whole folder and all the child folders and the files in that folder.
<V.B 6.0>
Code:
Public Shared Sub Copy ( _
sourceFileName As String, _
destFileName As String _
)
Private Function FileCopy()
Dim sourceFileName As String
Dim destFileName As String
MkDir("C:\temptest")
sourceFileName="C:/temp/text.txt"
destFileName="C:/temptest/text.txt"
File.Copy(sourceFileName, destFileName)
End Function
Re: How To copy folder/file from A to B
by using the harvested folder and file structure you got from the recursive search, you can copy then delete the files also.
to delete a file, use the Kill() method
Quote:
Is the following a valid code
On vb.net could be. On vb6 it isnt.
Re: How To copy folder/file from A to B
I prefer the API way...
Code:
'~~> Needs 1 Form, 2 textboxes and 1 Commandbutton
Option Explicit
Private Declare Function SHFileOperation _
Lib "shell32.dll" Alias "SHFileOperationA" _
(lpFileOp As SHFILEOPSTRUCT) As Long
Private Type SHFILEOPSTRUCT
hWnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAborted As Boolean
hNameMaps As Long
sProgress As String
End Type
Private Const FO_COPY = &H2
Private Sub Command1_Click()
'~~> Usage ~~> CopyFolder(From,To)
'~~> Example ~~> CopyFolder("C:\Temp\1","C:\Temp\2")
If CopyFolder(Text1.Text, Text2.Text) Then
MsgBox "Copied"
Else
MsgBox "Not copied"
End If
End Sub
Private Function CopyFolder(ByVal sFrom As String, _
ByVal sTo As String) As Boolean
Dim SHFileOp As SHFILEOPSTRUCT
On Error GoTo CF_Err
CopyFolder = False
With SHFileOp
.wFunc = FO_COPY
.pFrom = sFrom
.pTo = sTo
End With
SHFileOperation SHFileOp
CopyFolder = True
Exit Function
CF_Err:
MsgBox "Following error occurd while copying folder " & sFrom & vbCrLf & _
Err.Description, vbExclamation, "Error message"
End Function
Re: How To copy folder/file from A to B
@koolsid great, i didnt knew the api. :thumb:
Here are the details of the SHFILEOPSTRUCT: (for example you can also use the _MOVE to delete the source after copying)
http://msdn.microsoft.com/en-us/libr...95(VS.85).aspx
Some more details and remarks about the api:
http://msdn.microsoft.com/en-us/libr...64(VS.85).aspx
Re: How To copy folder/file from A to B
Great!
One last thing, I want to delete exhisting folder and its content before copying, else windows is prompting and asking if it can replace.
So if I delete before copying this will not happen right.
:) :)
Re: How To copy folder/file from A to B
you can use the scripting file system object (fso.deletefolder) to delete a specified folder and its contents...
reference from msdn:
http://msdn.microsoft.com/en-us/libr...xh(VS.85).aspx
Re: How To copy folder/file from A to B
Code:
'~~> Set reference to Microsoft Scripting Runtime Library
Private Sub Command1_Click()
'~~> Check if folder exists and if does then delete it
If FolderExists("C:\temp\2") Then Call DeleteFolder("C:\temp\2")
'~~> Rest of the Code to copy the folder + files as mentioned in POST#6
End Sub
'~~> Check If folder exists
Private Function FolderExists(ByVal strPath As String) As Boolean
Dim s As String
'~~> Strip final slash from path
If Right(strPath, 1) = "\" Then strPath = Left(strPath, Len(strPath) - 1)
'~~> Check if directory exists
s = Dir(strPath, vbDirectory)
If s <> "" Then FolderExists = True
End Function
'~~> Deletes Folder and files inside it
Sub DeleteFolder(ByVal strPath As String)
On Error Resume Next
Dim oFso As New Scripting.FileSystemObject
If Right(strPath, 1) = "\" Then strPath = _
Left(strPath, Len(strPath) - 1)
If oFso.FolderExists(strPath) Then
'~~> Deleting Read only files as well
oFso.DeleteFolder strPath, True
End If
End Sub
Re: How To copy folder/file from A to B
Thanks but
" Dim oFso As New Scripting.FileSystemObject
"
giving problem
Re: How To copy folder/file from A to B
Read the very first line in comments in the code that i posted above ;)
You need to set reference to Microsoft Scripting Runtime Library
Click On Menu project=>References and select the Microsoft Scripting Runtime Library
Re: How To copy folder/file from A to B
POWER it works.:) :) :wave: :) :)
But, may Know what is "'~~> Set reference to Microsoft Scripting Runtime Library"
I am unaware of this.
Re: How To copy folder/file from A to B
A reference is an external file(s), such as a DLL. To use the file system object you need to set a reference to scrrun.dll
If you have msdn installed in your pc, Search on the topic "Creating a Reference to an Object" It will explain it in detail....
Re: How To copy folder/file from A to B
What about the FO_DELETE flag on SHFILEOPSTRUCT? Will not delete the files and folders?
Re: How To copy folder/file from A to B
Quote:
Originally Posted by Jim Davis
What about the FO_DELETE flag on SHFILEOPSTRUCT? Will not delete the files and folders?
Yes you can use that as well... The reson why I gave the other code is because this will ask you to confirm the operation and I feel that the OP didn't want that....
Code:
Private Declare Function SHFileOperation Lib "shell32.dll" _
Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
Private Type SHFILEOPSTRUCT
hWnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAborted As Boolean
hNameMaps As Long
sProgress As String
End Type
Private Const FO_DELETE = &H3
Private Const FOF_ALLOWUNDO = &H40
Sub DeleteFolder()
Dim SHFileOp As SHFILEOPSTRUCT
With SHFileOp
'~~> Delete the folder
.wFunc = FO_DELETE
'~~> Select the folder
.pFrom = "C:\temp\2"
'~~> Move to Recycle Bin
'~~> If you want to permanently delete it then comment the below
.fFlags = FOF_ALLOWUNDO
End With
'~~> Perform Delete
SHFileOperation SHFileOp
End Sub
Re: How To copy folder/file from A to B
I see. So, there is no way to force the operation? For example, Total Commander also able to use the windows operations for file copy/deletions. You can set the "Force All" that there it will mass overwrite or/ delete the files without any further notification window. It also catches all widnows messages, then show its own UI window. I'm not sure how it does.
Edit:
Quote:
FOF_NOCONFIRMATION
Respond with Yes to All for any dialog box that is displayed.
This flag might help you to catch the window messages.
Quote:
FOF_WANTMAPPINGHANDLE
If FOF_RENAMEONCOLLISION is specified and any files were renamed, assign a name mapping object that contains their old and new names to the hNameMappings member. This object must be freed using SHFreeNameMappings when it is no longer needed.
These flags seems to me the best combo:
Quote:
FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR
I have found a post here that there you can find the declare of each 'extra' flags
http://www.vbforums.com/showpost.php...02&postcount=4
Re: How To copy folder/file from A to B
Hmmm let me check it out :)
Edit
Perfect! It works now
Code:
Private Declare Function SHFileOperation Lib "shell32.dll" _
Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
Private Type SHFILEOPSTRUCT
hWnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAborted As Boolean
hNameMaps As Long
sProgress As String
End Type
Private Const FO_DELETE = &H3
Private Const FOF_ALLOWUNDO = &H40
Private Const FOF_NOCONFIRMATION = &H10
Sub DeleteFolder()
Dim SHFileOp As SHFILEOPSTRUCT
With SHFileOp
'~~> Delete the folder
.wFunc = FO_DELETE
'~~> Select the folder
.pFrom = "C:\temp\2"
'~~> Delete Folder and it's contents without confirmation
.fFlags = FOF_NOCONFIRMATION
End With
'~~> Perform Delete
SHFileOperation SHFileOp
End Sub
Re: How To copy folder/file from A to B
The FOF_ALLOWUNDO will move the content to the Recycle Bin, am i right? (in case Recycle bin is available, but if it is just disabled by system settings, it will not move to there..)
Re: How To copy folder/file from A to B
No, In that case it won't... I believe. I could be wrong...