Directory Copying

Posted on 1998-08-20
Last Modified: 2010-04-30
What is the *simplest* way to copy an entire directory structure (and its files) from one drive to another?
Question by:IsleOfView
  • 3
  • 3
LVL 12

Expert Comment

ID: 1430076
SHELL("XCOPY " & dirspec & " " & target & " /S /E ", 1)


Author Comment

ID: 1430077
Maybe I was a little too blunt with the "Simplest" comment.  I want to have this accomplished in VB.
LVL 12

Expert Comment

ID: 1430078
SHELL command *IS* VB. No shame in using existing machine resource.

Are you coding for fun or $$$?

If you're coding in *MY* shop for $$$ then this is simplest, most reliable solution. Least cost to implement, little to debug. This produces the desired result, no? So why is not "correct" answer. No need to re-invent the wheel.

If you're coding for fun, then by all means, write a couple of pages of code.

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.


Author Comment

ID: 1430079
How can I make VB wait for the shell command to finish execution?  (and close the DOS window).  I would prefer this to happen from within VB, as this is going to a large customer base and the thought is that it would be more professional without DOS windows popping up all over the place...

Author Comment

ID: 1430080
I really need this to work through the API.
LVL 12

Accepted Solution

mark2150 earned 100 total points
ID: 1430081
Ahhh, making VB wait for a child task to complete requires some code but does work well. This was lifted wholesale from the M$ web site:

' These data type declarations are required to support the obscure windows
' function to spawn a child task and wait for it to finish before continuing
    cb As Long
    lpReserved As String
    lpDesktop As String
    lpTitle As String
    dwX As Long
    dwY As Long
    dwXSize As Long
    dwYSize As Long
    dwXCountChars As Long
    dwYCountChars As Long
    dwFillAttribute As Long
    dwFlags As Long
    wShowWindow As Integer
    cbReserved2 As Integer
    lpReserved2 As Long
    hStdInput As Long
    hStdOutput As Long
    hStdError As Long
End Type
    hProcess As Long
    hThread As Long
    dwProcessID As Long
    dwThreadID As Long
End Type
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, _
    ByVal dwMilliseconds As Long) As Long
Private Declare Function CreateProcessA Lib "kernel32" ( _
    ByVal lpApplicationName As Long, _
    ByVal lpCommandLine As String, _
    ByVal lpProcessAttributes As Long, _
    ByVal lpThreadAttributes As Long, _
    ByVal bInheritHandles As Long, _
    ByVal dwCreationFlags As Long, _
    ByVal lpEnvironment As Long, _
    ByVal lpCurrentDirectory As Long, _
    lpStartupInfo As STARTUPINFO, _
    lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Const INFINITE = -1&
' I don't really understand the above declares, but they do work so DON'T MESS WITH THEM!
Public Sub ExecCmd(cmdline$)
' This executes an external windows program and waits for it to complete
' Initialize the STARTUPINFO structure:
start.cb = Len(start)
' Start the shelled application
ret& = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
' Wait for the shelled application to finish:
    ret& = WaitForSingleObject(proc.hProcess, 5000)
    If ret& <> 0 Then GoTo holdhere
ret& = CloseHandle(proc.hProcess)
End Sub

Call the above with:

ExecCmd cmdstring

Where CMDSTRING is "XCOPY " & dirspec & " " & target & " /S /E "

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now