Basic copy in vbs using variables

Discussion in 'Software' started by red death68, Sep 8, 2011.

  1. red death68

    red death68 Command Sergeant Major

    ok so iv started to combine my codes together into a single hta and im stuck on how i can copy files from a folder that gets unzipped(self extracting) to a directory whos location is found via the registry

    the registry key is saved as the variable rv and the folder name to copy from will vary from archive to archive but for now I would like to use the one called s

    if this is possible please post a code using the variables also to determine if its the folder to be copied i used a variable called chck# (# is the number of the check)(yes there is no e)

    also the value of each is dependent of the check so please just use chck1 for the sound for now i can adapt the code from there

    thank you very much in advance
     
  2. red death68

    red death68 Command Sergeant Major

    i forgot to mention that i would like it to skip the confirmation of copy step as that will be handled by a detailed msgbox also forgot to mention that i would like it to display progress as it copies
     
  3. GermanOne

    GermanOne Guest

    It's complicated without you current source code.
    Untested:
    Code:
    chck1 = True
    strFileToCopy1 = "C:\somewhere\test.txt"
    strWhereToCopy = "C:\test"
    
    Set objShellApp = CreateObject("Shell.Application")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    If chck1 Then
      If objFSO.FileExists(objFSO.BuildPath(strWhereToCopy, objFSO.GetFileName(strFileToCopy1))) Then
        objFSO.DeleteFile(objFSO.BuildPath(strWhereToCopy, objFSO.GetFileName(strFileToCopy1)))
      End If
      Set objFolder = objShellApp.NameSpace(strWhereToCopy)
      objFolder.CopyHere strFileToCopy1
      Set objFolder = Nothing
    End If
    
     
  4. red death68

    red death68 Command Sergeant Major

    german i went to implement your code now that i have free time but i noticed it is for a single file is there a differnce between copying a single file or the contents of a directory to another directory
     
  5. GermanOne

    GermanOne Guest

    Indeed it is different. If you want to copy multiple files you have to loop over the directory and copy file by file since you can't use wildcards in VBS.
    But I don't understand your requirement because I thought you would like to copy a single file depending on the checkbox :confused

    Regards
    GermanOne
     
  6. red death68

    red death68 Command Sergeant Major

    no sorry if i wasnt clear im making an uninstaller for game modifications, and the modifications normally affect the files in at least one entire folder although i do have a rare case when i need to copy a single file to a differnt directory so i can still implement your original code.
     
  7. GermanOne

    GermanOne Guest

    I see. But you should tell me if you want to copy the entire folder or only it's content. In case of content, I need to know if there are subfolders inside.

    Regards
    GermanOne
     
  8. red death68

    red death68 Command Sergeant Major

    there a few few with sub directories that shouldnt need altering but in case i code that replaces the files in each folder will be enough so long as it preserves any sub folders

    also if i didnt mention it i would like it to display the copying dialoug but not the confirmation of copy dialouge
     
  9. GermanOne

    GermanOne Guest

    I have to think about it. It's quite difficult because you need the damn copying dialogue.

    Regards
    GermanOne
     
  10. red death68

    red death68 Command Sergeant Major

    i had this one from awhile ago theres was something i didnt like about it but i cant remember what especially since i just woke up

    Code:
    Option Explicit
    
    'Declare variables
    Dim objFSO, objReadFile, contents, objShell, objFolder, strFolder, objFSOFolder, objFile
    
    'Set Objects
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objReadFile = objFSO.OpenTextFile("path.txt", 1, False)
    
    'Read file contents
    contents = objReadFile.ReadAll
    
    'Close file
    objReadFile.close
    
    'Cleanup objects
    Set objFSO = Nothing
    Set objReadFile = Nothing
    
    CreateObject("WScript.Shell").Run "sounds.exe"
    
    strFolder = "contentsrez\Snd2"
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    ' Delete folder if exists
    If objFSO.FolderExists(strFolder) Then objFSO.DeleteFolder strFolder, True
    
    ' Regardless create it new for further usage
    objFSO.CreateFolder strFolder
    
    Set objShell = CreateObject("Shell.Application")
     
    Const FOF_CREATEPROGRESSDLG = &H0&
    
    'directory files are to be copied to
    Set objFolder = objShell.NameSpace(contentsrez\Snd2) 
     
    'directory files are to be copied from
    objFolder.CopyHere "s", FOF_CREATEPROGRESSDLG
    
    'Quit script
    WScript.Quit
     
  11. GermanOne

    GermanOne Guest

    Try something like that. I can't test it since all the constants seem not to have any effect on my Win7 machine.
    Code:
    strFolder1 = "C:\source"
    strFolder2 = "C:\dest"
    
    Const FOF_CREATEPROGRESSDLG = &H0&
    Const FOF_NOCONFIRMATION = &H10&
    Const FOF_NOCONFIRMMKDIR = &H200&
    Const FOF_NOERRORUI = &H400&
    
    Set objFSO = Createobject("Scripting.FileSystemObject")
    If Not objFSO.FolderExists(strFolder2) Then objFSO.CreateFolder(strFolder2)
    
    Set objShell = CreateObject("Shell.Application")
    Set objFolder = objShell.NameSpace(strFolder2) 
    objFolder.CopyHere strFolder1, FOF_CREATEPROGRESSDLG + FOF_NOCONFIRMATION + FOF_NOCONFIRMMKDIR + FOF_NOERRORUI
    
    Regards
    GermanOne
     
  12. red death68

    red death68 Command Sergeant Major

    that could be a problem this was gonna be distributed amoung multiple systems including win7 and vista as well as xp
     
  13. GermanOne

    GermanOne Guest

    Right. And for that reason I called it "damn" before :mad
    There are fabulous possibilities using the CopyFile and CopyFolder methods of the FileSystemObject, but unfortunately without any progress dialogue.
    Believe me, I try to avoid the CopyHere method of the shell when ever I can. A progress bar is not worth it to use CopyHere. You could always use a messagebox or maybe better a self-closing shell popup to tell the user if a process has been finished.

    Regards
    GermanOne
     
  14. red death68

    red death68 Command Sergeant Major

    a demonstration of either of these alternatives would be much apreciated german = D
     
  15. GermanOne

    GermanOne Guest

    These methods are well explained in the MSDN (as always).
    CopyFile Method
    CopyFolder Method
    Poup Method

    You need a small sub routine if you want to preserve files and folders of your destination folder which do not exist in the source folder.
    Code:
    strSource = "C:\SourceFolder"
    strDest = "C:\DestinationFolder"
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    CopyContent strSource, strDest
    
    Set objWSH = CreateObject("WScript.Shell")
    objWSH.Popup "Copying Finished", 5, "Info", vbInformation + vbSystemModal
    
    
    Sub CopyContent(ByVal strSource, ByVal strDest)
      If Not objFSO.FolderExists(strDest) Then objFSO.CreateFolder strDest
      Set objFolder = objFSO.GetFolder(strSource)
      For Each objSubFolder In objFolder.SubFolders
        CopyContent objFSO.BuildPath(strSource, objSubFolder.Name), objFSO.BuildPath(strDest, objSubFolder.Name)
      Next
      For Each objFile In objFolder.Files
        objFSO.CopyFile objFSO.BuildPath(strSource, objFile.Name), objFSO.BuildPath(strDest, objFile.Name), True
      Next
    End Sub
    
    As you can see the subroutine will be called for each found subfolder recursively. For that reason it's iterating over the entire depth of the folder structure.

    Regards
    GermanOne
     
  16. red death68

    red death68 Command Sergeant Major

    iv been reviewing the contents of the sub folders and it appears that i may just be best replacing the contents is it possible to copy a folder and all contents in one shot?

    as the subfolder files seem to be directly connected to the contents in the first folder
     
  17. GermanOne

    GermanOne Guest

    If only you read my last post ...
    Have a look at the linked CopyFolder Method.

    Regards
    GermanOne
     
  18. red death68

    red death68 Command Sergeant Major

    a little but iv been extremely busy sorry for my negligence
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds