Results 1 to 29 of 29

Thread: Code to copy a folder

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2005
    Posts
    16

    Code to copy a folder

    Hello all,

    I am wanting to copy a folder from a server to a target PC and rename it, so far I can verify that the folder exists on the server and isn't already on the target PC but I cannot find any code that will copy and rename the folder.

    I am using VB6 an really need some help, if this is not possible in VB can you tell me haw to call a dos command through VB as I know the code for this in DOS.

    Thanks

  2. #2
    Addicted Member
    Join Date
    Nov 2004
    Posts
    171

    Re: Code to copy a folder

    I am not sure about the vb code for copying the folder, but just shell a batch file that has your code in it.
    Sherminator ~ I'll be back.

  3. #3
    Lively Member
    Join Date
    Oct 2001
    Posts
    125

    Re: Code to copy a folder

    VB Code:
    1. Shell "MyBatch.bat"

    Shell may return before the task is done (asynchronus!), so be sure all files are copied before you continue
    A cat always lands on its feet. A toast always lands on the butter side. A cat with a toast strapped on its back will hover in a quantum state of indecision.

  4. #4
    Addicted Member
    Join Date
    Nov 2004
    Posts
    171

    Re: Code to copy a folder

    Does anyone know of a function, or anything for that matter, that would allow you to know when the operation is completed? I have no specific need to know, but the code may become useful later...
    Sherminator ~ I'll be back.

  5. #5
    Lively Member
    Join Date
    Oct 2001
    Posts
    125

    Re: Code to copy a folder

    Brute force: before starting the batch, create a file somewhere. can be empty.
    towards the end of the batch, delete that file out of the batch.

    the VB app can occasionally look if the file is still there. Once it's gone, we know the batch is done. Along these lines, might be a good idea to pipe the echos from the batch into a file which can be verified by the vb app to make sure the batch performed as desired.

    one could also try to set an environment variable, but I believe VB can only read them, therefore we couldn't clean up unless another batch would do that...
    A cat always lands on its feet. A toast always lands on the butter side. A cat with a toast strapped on its back will hover in a quantum state of indecision.

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Code to copy a folder

    You could always use the CopyFolder method of the FileSystemObject within the Microsoft Scripting Runtime, and say something like
    VB Code:
    1. Private Sub cmdMakeItHappen_Click()
    2. Dim fso As FileSystemObject
    3. Dim target_folder As Folder    
    4. Set fso = New FileSystemObject
    5. fso.CopyFolder "H:\MyFolder\", "C:\MyFolder\"
    6. MsgBox "I am done"
    7. End Sub

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jan 2005
    Posts
    16

    Re: Code to copy a folder

    Thanks Hack that works a charm, I now i pushing it but how would I change the folder security to give users write and modify access to the folder?

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Code to copy a folder

    VB Code:
    1. SetAttr "c:\myfolder\", vbNormal

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jan 2005
    Posts
    16

    Re: Code to copy a folder

    I have tried the above but the user folder permissions are still read only [we only give users read access to the C:\ we setup folders with write and modify if it is needed] any help will be appriciated.

    In DOS you can change the CACLS on a folder:

    source = c:\myfolder

    cacls source /t /e /g users:c

    would using the shell command be the best way around this? and how would I do this [I am new to VB]

  10. #10
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Code to copy a folder

    SetAttr has always worked for me. How are you using it?

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Jan 2005
    Posts
    16

    Re: Code to copy a folder

    I just added it to the end of your code tike this:

    Private Sub cmdMakeItHappen_Click()
    Dim fso As FileSystemObject
    Dim target_folder As Folder
    Dim Source = c:\myfolder
    Set fso = New FileSystemObject
    fso.CopyFolder "H:\MyFolder\", Source
    MsgBox "I am done"
    SetAttr Source, vbNormal

    End Sub

  12. #12
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Code to copy a folder

    You are getting a little ahead of yourself.

    Modify your code ever so slightly as follows:
    VB Code:
    1. 'not
    2. Dim Source = c:\myfolder
    3. 'but
    4. Dim Source As String
    5. Source = "C:\myfolder"
    Then try it.

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Jan 2005
    Posts
    16

    Re: Code to copy a folder

    Its still not working, where does SetAttr Source, vbNormal get its permissons from? if it looks at the root directory in this case [C:\] then it will keep them as read only. I am not permitted to alter the permission of the directory as a whole only folders which need write and modify can be changed [the domain admins stop me from doing this]

  14. #14
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Code to copy a folder

    Just for the sake of having alternative method (and also because I am not a big fan of FSO) here is another approach that uses Windows API:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Type SHFILEOPSTRUCT
    4.         hwnd As Long
    5.         wFunc As Long
    6.         pFrom As String
    7.         pTo As String
    8.         fFlags As Integer
    9.         fAnyOperationsAborted As Long
    10.         hNameMappings As Long
    11.         lpszProgressTitle As String
    12. End Type
    13.  
    14. Private Const FOF_MULTIDESTFILES = &H1
    15. Private Const FOF_CONFIRMMOUSE = &H2
    16. Private Const FOF_SILENT = &H4
    17. Private Const FOF_RENAMEONCOLLISION = &H8
    18. Private Const FOF_NOCONFIRMATION = &H10
    19. Private Const FOF_WANTMAPPINGHANDLE = &H20
    20. Private Const FOF_CREATEPROGRESSDLG = &H0
    21. Private Const FOF_ALLOWUNDO = &H40
    22. Private Const FOF_FILESONLY = &H80
    23. Private Const FOF_SIMPLEPROGRESS = &H100
    24. Private Const FOF_NOCONFIRMMKDIR = &H200
    25.  
    26. Private Const FO_MOVE = 1
    27. Private Const FO_COPY = 2
    28. Private Const FO_DELETE = 3
    29. Private Const FO_RENAME = 4
    30.  
    31. Private Declare Function SHFileOperation Lib "shell32.dll" (lpFileOp As SHFILEOPSTRUCT) As Long
    32.    
    33. Public Function CopyFolder(ByVal strSource As String, ByVal strDest As String) As Boolean
    34. '==========================================================================================
    35. Dim varFOS As SHFILEOPSTRUCT
    36.     With varFOS
    37.         .fFlags = FOF_NOCONFIRMATION Or FOF_SILENT Or FOF_NOCONFIRMMKDIR
    38.         .wFunc = FO_COPY
    39.         .pFrom = strSource
    40.         .pTo = strDest
    41.     End With
    42.     Call SHFileOperation(varFOS)
    43.     CopyFolder = (varFOS.fAnyOperationsAborted = 0)
    44. End Function
    45.  
    46. Private Sub Command1_Click()
    47.     CopyFolder "c:\temp\test1", "c:\temp\test2"
    48. End Sub

  15. #15
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Code to copy a folder

    Quote Originally Posted by AndyW
    Its still not working, where does SetAttr Source, vbNormal get its permissons from? if it looks at the root directory in this case [C:\] then it will keep them as read only. I am not permitted to alter the permission of the directory as a whole only folders which need write and modify can be changed [the domain admins stop me from doing this]
    If you can modify the attributes from DOS, you should be able to modify the attributes from VB. Can you modify the attributes from DOS?

    (RhinoBull: I'm not a big fan of FSO either, however, it does have the one redeeming factor of being brief and easy to understand)

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Jan 2005
    Posts
    16

    Re: Code to copy a folder

    Yes it works fine when using dos

  17. #17
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Code to copy a folder

    Quote Originally Posted by Hack
    ... (RhinoBull: I'm not a big fan of FSO either, however, it does have the one redeeming factor of being brief and easy to understand)
    Indeed.

  18. #18
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Code to copy a folder

    Quote Originally Posted by AndyW
    Yes it works fine when using dos
    Well, I'm not sure what to tell you Andy. I have issue whatsoever using SetAttr on my PC to manipulate either folder or file attributes.

    I also have no idea what this means
    In DOS you can change the CACLS on a folder:

    source = c:\myfolder

    cacls source /t /e /g users:c
    In my old DOS days, I managed attributes using the DOS Attrib command, so maybe these are things that have been setup for you to do this kind of thing that VB can't get around.

  19. #19
    Lively Member
    Join Date
    Oct 2001
    Posts
    125

    Re: Code to copy a folder

    Originally Posted by Hack
    In my old DOS days, I managed attributes using the DOS Attrib command, so maybe these are things that have been setup for you to do this kind of thing that VB can't get around.
    Well, at least at that time I was quite confident I understood what was happening inside that gray box, to a fair degree anyways. To be honest, now it's not only a few wild guesses... well, that's advantage for you...

    Dinosaur's talk, I know, but to all you youngsters out there:
    There was an existing universum prior to Windows!!!
    A cat always lands on its feet. A toast always lands on the butter side. A cat with a toast strapped on its back will hover in a quantum state of indecision.

  20. #20

    Thread Starter
    Junior Member
    Join Date
    Jan 2005
    Posts
    16

    Re: Code to copy a folder

    In Dos I would have this set in a batch file

    set copyEnv=

    set /p copyEnv=Please enter the environment you want to copy

    cacls p:\%copyEnv% /t /e /g users:c

    this would then change the requested folder's permissions to give users modify access

    How would this be done in shell, when i try

    dim copyEnv as string

    copyEnv = "P:\" & textbox1.text

    shell "cacls copyEnv /t /e /g users:c

    dos opens and closes without running the command, what am i doing worng

  21. #21
    Lively Member
    Join Date
    Oct 2001
    Posts
    125

    Re: Code to copy a folder

    you have to manually / programmatically create the batch file like
    [Highlight=VB]

    Dim i as Long
    Dim Batchline(5) as String

    'writing the batch in memory
    Batchline(0)="REM This batch will perform the following: blah blah"
    Batchline(1)= 'first command of batch
    Batchline(2)= '2nd command of batch
    '...etc..

    'writing the batch to file
    Open "C:\bopybat.bat" for output as #1
    for i=0 to UBound(Batchline)
    Print #1, Batchline(i)
    Next
    Close #1

    'execute the batch
    Shell "C:\copybat.bat"
    A cat always lands on its feet. A toast always lands on the butter side. A cat with a toast strapped on its back will hover in a quantum state of indecision.

  22. #22

    Thread Starter
    Junior Member
    Join Date
    Jan 2005
    Posts
    16

    Re: Code to copy a folder

    So using you code how can I get the batchfile to pickup a variable from the VB code:

    Dim i as Long
    Dim Batchline(5) as String
    Dim copyEnv as string
    copyEnv = "program files"

    'writing the batch in memory
    Batchline(0)="REM This batch will perform the following: blah blah"
    Batchline(1)= "cacls p:\%copyEnv% /t /e /g users:c"

    'writing the batch to file
    Open "C:\copybat.bat" for output as #1
    for i=0 to UBound(Batchline)
    Print #1, Batchline(i)
    Next
    Close #1

    'execute the batch
    Shell "C:\copybat.bat"

    so in this case I want to give a user modify access to program files is it possible.

    Thanks for all your help so far guys

  23. #23
    Lively Member
    Join Date
    Oct 2001
    Posts
    125

    Re: Code to copy a folder

    not sure I understand what exactly you mean

    if the Evironment variable %CopyEnv% exists, you can read it
    VB Code:
    1. Dim SomeVar As String
    2. SomeVar=Environ("copyEnv")

    for the other parameters (i.e. /t /e /g users:c), put some checkboxes and/or option buttons on your form, where the user can choose their settings.

    When they hit "OK", you will have to build a string according to the selections.

    for instance, if you have a checkbox reading "include SubDirs":
    VB Code:
    1. If ChkSubDirs.Value=1 Then
    2.   Batchline(x) = BatchLine(x)+ "/T "
    3. End If

    Likely you will have to check that the parameters choosen make sense (not mutually exclusive etc).


    I couldn't be more specific, this cacls business is absolutely new for me.
    (as I mentioned earlier, I'm a dinosaur, in the good old days we had Attrib and even something from Novell Netware, I forgot...)
    A cat always lands on its feet. A toast always lands on the butter side. A cat with a toast strapped on its back will hover in a quantum state of indecision.

  24. #24
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Code to copy a folder & Attributes

    the vb FILECOPY command will copy a folder. but, as long as you are using the FSO, you can use it's Attributes property.

    use:


    fso.attributes = fso.attributes +/- [new attributes]
    fso.attributes = fso.attributes - 1

    0 Normal
    1 Read Only R/W
    2 Hidden R/W
    4 System R/W
    8 Volume Disk Drive Volume Label Read only
    16 Directory Folder or Directory Read only
    32 Archive Has changed since last backup R/W
    64 Alias Link or Shortcut Read only
    128 Compressed Read only


    Don't use batch files. They aren't necessary nowadays. I've had it drilled into my head recently.

  25. #25

    Thread Starter
    Junior Member
    Join Date
    Jan 2005
    Posts
    16

    Re: Code to copy a folder

    This is starting to go over my head [first time i have used vb]

    CACLS changes folder permissions so with the dos command /T says change the permissions on the folder and subfolders /E just edit the permissions [like you would by right clicking and choosing securty] /G grant it to the users setting, users:c give them write access

    all of these will not be optional [the guys using the program only need to enter the PC they want to copy to and the folder they want to copy]

    The problem is I have no idea how to get this working with VB, I have a batch file that does it but apparently "its not user friendly".

    How does this fso.attributes work with the code I have to copy the folder?

    Sorry for all the questions but i am a noooooooooob

  26. #26
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Code to copy a folder

    you should be able to use the SHELL command to use CACLS if it runs.

    just use SHELL "c:\windows\cacls ..."

    the way that you normally would.

    after you pick an object in fso, you can then set its attributes. I don't think that is what you want though. You want the permissions.
    Here is an overview:

    Displays or modifies access control lists (ACLs) of files



    CACLS filename [/T] [/E] [/C] [/G user:perm] [/R user [...]]

    [/P user:perm [...]] [/D user [...]]

    filename Displays ACLs.

    /T Changes ACLs of specified files in

    the current directory and all subdirectories.

    /E Edit ACL instead of replacing it.

    /C Continue on access denied errors.

    /G user:perm Grant specified user access rights.

    Perm can be: R Read

    W Write

    C Change (write)

    F Full control

    /R user Revoke specified user's access rights (only valid with /E).

    /P user:perm Replace specified user's access rights.

    Perm can be: N None

    R Read

    W Write

    C Change (write)

    F Full control

    /D user Deny specified user access.

    Wildcards can be used to specify more that one file in a command.

    You can specify more than one user in a command.



    Abbreviations:

    CI - Container Inherit.

    The ACE will be inherited by directories.

    OI - Object Inherit.

    The ACE will be inherited by files.

    IO - Inherit Only.

    The ACE does not apply to the current file/directory.

  27. #27

    Thread Starter
    Junior Member
    Join Date
    Jan 2005
    Posts
    16

    Re: Code to copy a folder

    As I could't work out how to pass a VB variable though shell I sorted this another way. I created this batch file:

    p:
    for /d %%f in ("ski*") do cacls %%f /t /e /g users:c
    for /d %%f in ("bur*") do cacls %%f /t /e /g users:c
    for /d %%f in ("hml*") do cacls %%f /t /e /g users:c
    for /d %%f in ("sml*") do cacls %%f /t /e /g users:c

    As the folders that can be copied will start with either of the above 3 letters I just make it alter the permissions on all folders it finds meeting the criteria.

    Thanks for all you time and help

  28. #28
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Code to copy a folder

    Out of curiosity: did you try that sample I posted in reply #14?

  29. #29

    Thread Starter
    Junior Member
    Join Date
    Jan 2005
    Posts
    16

    Re: Code to copy a folder

    Sorry I didn't only because I don't understand it [dont see the point in just copying code if you cannot follow what it is doing] I can follow the function and sub but nearly everything before that is greek to me from what i see it will copy a folder from source to destination. can you get to to set folder permissions aswell??

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width