copy entire folder and sub folders
hi, i am making a program that i would like to copy a folder from one workstation/document server to my network server. how can i make an app copy a folder (E:\documents\ or //A/DOCS$/) to a server (//HOST/DOCS$ or C:\Documents\) to backup documents and overwrite the old files and do this every 10 or 15 mins. i am making this program incase one of my servers brakes down.
Thanks, dandono.
Re: copy entire folder and sub folders
Some APIs are involved so here is a sample:
VB Code:
Option Explicit
Private Type SHFILEOPSTRUCT
hwnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAnyOperationsAborted As Long
hNameMappings As Long
lpszProgressTitle As String ' only used if FOF_SIMPLEPROGRESS
End Type
Private Const FOF_MULTIDESTFILES = &H1
Private Const FOF_CONFIRMMOUSE = &H2
Private Const FOF_SILENT = &H4
Private Const FOF_RENAMEONCOLLISION = &H8
Private Const FOF_NOCONFIRMATION = &H10
Private Const FOF_WANTMAPPINGHANDLE = &H20
Private Const FOF_CREATEPROGRESSDLG = &H0
Private Const FOF_ALLOWUNDO = &H40
Private Const FOF_FILESONLY = &H80
Private Const FOF_SIMPLEPROGRESS = &H100
Private Const FOF_NOCONFIRMMKDIR = &H200
Private Const FO_MOVE = 1
Private Const FO_COPY = 2
Private Const FO_DELETE = 3
Private Const FO_RENAME = 4
Private Declare Function SHFileOperation Lib "shell32.dll" (lpFileOp As SHFILEOPSTRUCT) As Long
Public Function CopyFolder(ByVal strSource As String, ByVal strDest As String) As Boolean
'==========================================================================================
Dim varFOS As SHFILEOPSTRUCT
With varFOS
.fFlags = FOF_NOCONFIRMATION Or FOF_SILENT Or FOF_NOCONFIRMMKDIR
.wFunc = FO_COPY
.pFrom = strSource
.pTo = strDest
End With
Call SHFileOperation(varFOS)
CopyFolder = (varFOS.fAnyOperationsAborted = 0)
End Function
Private Sub Command1_Click()
CopyFolder "c:\temp\test1", "c:\temp\test2"
End Sub
Re: copy entire folder and sub folders
If the destination folder exists you can just do
VB Code:
Shell "xcopy.exe C:\temp C:\temp2 /E", 1
Re: copy entire folder and sub folders
Nice to see the old Dos commands still have their uses.
Re: copy entire folder and sub folders
i could not get the api version to work but does the shell one copy over network aswell?
Re: copy entire folder and sub folders
I believe so but I have no way to test it.
Re: copy entire folder and sub folders
It doesn't appear to. I even tried mapping the drive to \david on the remote machine, and then copying \test with \test2 in it to \test
xcopy y:\test c:\test
0 files copied.
Re: copy entire folder and sub folders
That sample I've posted works perfectly IF user has sufficient security access: read/write at least. Also you may need to check if source/destination folders exist prior to calling CopyFolder function ...
Re: copy entire folder and sub folders
sorry, i was referring to the xcopy command.
Re: copy entire folder and sub folders
... and I was just answering to this :) :
i could not get the api version to work ...
Re: copy entire folder and sub folders
Also, XCOPY might not recognize long names so you'll need to convert long path to short (the DOS way):
VB Code:
Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" _
(ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal lBuffer As Long) As Long
Public Function GetShortPath(strFileName As String) As String
'KPD-Team 1999
'URL: [url]http://www.allapi.net/[/url]
Dim lngRes As Long, strPath As String
'Create a buffer
strPath = String$(165, 0)
'retrieve the short pathname
lngRes = GetShortPathName(strFileName, strPath, 164)
'remove all unnecessary chr$(0)'s
GetShortPath = Left$(strPath, lngRes)
End Function
Private Sub Form_Load()
MsgBox GetShortPath("c:\Program Files\")
End Sub
Re: copy entire folder and sub folders
Nope, I was using \David\Test
Re: copy entire folder and sub folders
Wouldn't know what to tell you then as I like using more robust API solution(s) when aplicable and not those for DOS. Sorry.
Re: copy entire folder and sub folders
i'm using this code: Shell "xcopy " + txtloc.Text + " " + txthost.Text
it does not seem to work and i am on a user that is a net admin. does it work if it is copying to a windows server 2003 domain controler from a windows xp professional workstation?
Re: copy entire folder and sub folders
I would go with the API version. I fought for quite a while to try to get a batch/cmd file to work, and ran into one problem after another. I finally redid everything using an older API, and I guess that it hasn't been tested, yet.
Usie RhinoBulls example above. It should work.