none
Issue with copying folders using FileSystemObject in VBScript

    Question

  • Hi,

    I'm working on a backup script in VBScript and I am having issues getting folders to copy properly. What is happening is I am moving to the folder I want to backup and then searching all the subfolders for specific folders and then another subfolder within those subfolders. I then want it to copy over the top level folder and the two sets of subfolders over to the backup location. The script I'm using right now is just moving over the second subfolder.                       

    ' Move to hostedFolder


    ' Open log file


    Set objFile = oFSO.OpenTextFile(logFileLocation & "backup_log_" & strdate & ".txt", 2)


    activeFolder = hostedFolder


    Set objRootFolder = oFSO.GetFolder(activeFolder)
    Set colSubfolders = objRootFolder.SubFolders


    For Each objFolder in colSubfolders


    If oFSO.FolderExists(objFolder & "\" & "Accounting" & "\" & "data") then


    oFSO.CopyFolder objFolder & "\" & "Accounting" & "\" & "data", backupFolder & strdate & "\"


    objFile.WriteLine objFolder & " was copied to " & backupFolder & strdate

    End If   

    Next

    So basically what is happening is it is only copying over the data folder and not the Accounting and objFolder folder. objFolder is the top folder so for instance test\Accounting\Data. I'd like the script to copy test\Accounting\Data.

    I may not have explained myself clearly and it is a bit confusing to describe. Please let me know if I can clarify anything.

    Thanks.



    • Edited by jn865 Friday, February 17, 2012 2:14 PM
    Friday, February 17, 2012 2:07 PM

Answers

  • Hi Tom et all,

    Let me try and clear things up a bit about what I'm attempting to do with the script.

    These are the variables that define the locations the script is going to copy and paste to.

    hostedFolder = "E:\backuptest\"
    backupFolder = "\\ipaddress\networkshare$\backups\"

    Inside backuptest there are multiple folders. I need the script to look at each folder inside backuptest and check for the existance of specific subfolders IE Accounting. Inside the Accounting subfolder is a subfolder called data. If the folder contains the Accounting subfolder I need the script to copy the folder with only the Accounting subfolder and data subfolder inside. Right now the script just copies the data folder and leaves out the Accounting folder and the top folder. I need it to copy the top level folder, the Accounting subfolder, and the data subfolder only. There are multiple folders in backuptest that could have Accounting\data and if the script just copies the data folder from each folder with no other folder structure intact then it will overwrite the previously copied folder. 

    Please let me know if I need to elaborate further.



    To copy the accounting folder yu need to specidy it and not just the subfolder.

    You logic is a bit thin and seems arbitrary.  Try stating the logic as a set of steps.  This wil help you to see why it is an issue.

    To copy the Accounting folder:

    Set fldr = oFSO.GetFolder objFolder & "\" & "Accounting"
    target = backupFolder & "\" & strdate
    fldr.Copy target

    You are trying to specify all folders in one object. This cannot be done.


    ¯\_(ツ)_/¯

    • Marked as answer by jn865 Sunday, February 19, 2012 1:16 AM
    Friday, February 17, 2012 3:59 PM

All replies

  • I cannot see how your script can run. In the statement
    If oFSO.FolderExists(objFolder & "\" & "Accounting" & "\" & "data") then

    "objFolder" is an object. Objects are not strings and you cannot append a string to an object. Did you perhaps mean "objFolder.path"?

    The same problem exists in this line:
    objFile.WriteLine objFolder & " was copied to " & backupFolder & strdate

    The following line
    Set objFile = oFSO.OpenTextFile(logFileLocation & "backup_log_" & strdate & ".txt", 2)

    should probably read
    Set objFile = oFSO.CreateTextFile(logFileLocation & "backup_log_" & strdate & ".txt", true)

    and this line
    If oFSO.FolderExists(objFolder & "\" & "Accounting" & "\" & "data") then

    should be
    If oFSO.FolderExists(objFolder.path & "\" & "Accounting\data") then

    I recommend you clean up your code, then post it again if you still have a problem. Try not to insert so many blank lines - it makes your code hard to read!

    Friday, February 17, 2012 2:32 PM
  • Hi Pegasus,

    Most of the time when I post my scripts here I usually leave out quite a bit of things such as where I'm defining oFSO as the FileSystemObject and creating the log files, etc. Would it be beneficial for my future posts to post the whole script? I will give your suggestions a try, clean up the code a bit, and see what my results are.

    Thanks for your help.

    Friday, February 17, 2012 2:36 PM
  • You need to include the relevant bits in your code. Since all respondents are fully familiar with the File System Object, omitting it from your post won't matter.
    Friday, February 17, 2012 2:46 PM
  • Just an aside: The original script functions (after a fashion) because the Path property is the default property for the Folder object.  That is, by default objFolder will return the path when typecasting requires a string.  it returns a pointer to the object when typecasting requires the object.  That is one of the advantages (disadvantages?) of script - it's not strongly typed.  Rather, the script engine does its best at 'guessing' what is intended. 

    Using the explicitly named Path property is, of course, the way to go, but it has nothing to do with the OPs problem.  Rather, it is a simple problem of logic.  The script is doing what it is told, not what the OP wants it to do.  Since, from the description given, I have no idea what is intended, either, I'm not surprised the script fails to do what is desired.  I keep re-reading the original post and I can't figure out which folders/subfolders need to be copied.


    Tom Lavedas

    Friday, February 17, 2012 3:01 PM
  • I cannot see how your script can run. In the statement
    If oFSO.FolderExists(objFolder & "\" & "Accounting" & "\" & "data") then

    "objFolder" is an object. Objects are not strings and you cannot append a string to an object. Did you perhaps mean "objFolder.path"?

    Pegasus.  You are not really correct about that.

    Try this:
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set oFolder = fso.GetFolder("c:\")
    MsgBox oFolder

    You willnotice that ActiveX objects usually have a default property.  In the case of the folder object the default property is teh path so we do not need to use it.

    In the early days of object-ness it was all the rage t ohave default properties.  Today teh wise obes who decide these things have decided to drop default properties.  Man of the newest COM objects may not have a default.  Net classes do not really have default properties although at time they may act like they do.

    While not the recommenfed syntax by modern standards it is perfectly legitimate for legacy systems like VBScript and WSH.

    I do agree that a bit better formatting would make it easier to discover the problem.


    ¯\_(ツ)_/¯

    Friday, February 17, 2012 3:02 PM
  • Hi,

    I'm working on a backup script in VBScript and I am having issues getting folders to copy properly. What is happening is I am moving to the folder I want to backup and then searching all the subfolders for specific folders and then another subfolder within those subfolders. I then want it to copy over the top level folder and the two sets of subfolders over to the backup location. The script I'm using right now is just moving over the second subfolder.                       

    ' Move to hostedFolder


    ' Open log file


    Set objFile = oFSO.OpenTextFile(logFileLocation & "backup_log_" & strdate & ".txt", 2)


    activeFolder = hostedFolder


    Set objRootFolder = oFSO.GetFolder(activeFolder)
    Set colSubfolders = objRootFolder.SubFolders


    For Each objFolder in colSubfolders


    If oFSO.FolderExists(objFolder & "\" & "Accounting" & "\" & "data") then


    oFSO.CopyFolder objFolder & "\" & "Accounting" & "\" & "data", backupFolder & strdate & "\"


    objFile.WriteLine objFolder & " was copied to " & backupFolder & strdate

    End If   

    Next

    So basically what is happening is it is only copying over the data folder and not the Accounting and objFolder folder. objFolder is the top folder so for instance test\Accounting\Data. I'd like the script to copy test\Accounting\Data.

    I may not have explained myself clearly and it is a bit confusing to describe. Please let me know if I can clarify anything.

    Thanks.



    Your question is somewhat vugue.   The code is copying exactly what you told it to copy.

    You did not tell it to copy any other folders.

    You need to be clearer about your intended result.  Do you want to copy accounting and all subfolders?


    ¯\_(ツ)_/¯

    Friday, February 17, 2012 3:08 PM
  • Hi Tom et all,

    Let me try and clear things up a bit about what I'm attempting to do with the script.

    These are the variables that define the locations the script is going to copy and paste to.

    hostedFolder = "E:\backuptest\"
    backupFolder = "\\ipaddress\networkshare$\backups\"

    Inside backuptest there are multiple folders. I need the script to look at each folder inside backuptest and check for the existance of specific subfolders IE Accounting. Inside the Accounting subfolder is a subfolder called data. If the folder contains the Accounting subfolder I need the script to copy the folder with only the Accounting subfolder and data subfolder inside. Right now the script just copies the data folder and leaves out the Accounting folder and the top folder. I need it to copy the top level folder, the Accounting subfolder, and the data subfolder only. There are multiple folders in backuptest that could have Accounting\data and if the script just copies the data folder from each folder with no other folder structure intact then it will overwrite the previously copied folder. 

    Please let me know if I need to elaborate further.



    • Edited by jn865 Friday, February 17, 2012 3:26 PM
    Friday, February 17, 2012 3:24 PM
  • Hi,

    Why implement in VBScript when you can just use Robocopy? It's quite robust.

    Bill

    Friday, February 17, 2012 3:53 PM
  • Hi Tom et all,

    Let me try and clear things up a bit about what I'm attempting to do with the script.

    These are the variables that define the locations the script is going to copy and paste to.

    hostedFolder = "E:\backuptest\"
    backupFolder = "\\ipaddress\networkshare$\backups\"

    Inside backuptest there are multiple folders. I need the script to look at each folder inside backuptest and check for the existance of specific subfolders IE Accounting. Inside the Accounting subfolder is a subfolder called data. If the folder contains the Accounting subfolder I need the script to copy the folder with only the Accounting subfolder and data subfolder inside. Right now the script just copies the data folder and leaves out the Accounting folder and the top folder. I need it to copy the top level folder, the Accounting subfolder, and the data subfolder only. There are multiple folders in backuptest that could have Accounting\data and if the script just copies the data folder from each folder with no other folder structure intact then it will overwrite the previously copied folder. 

    Please let me know if I need to elaborate further.



    To copy the accounting folder yu need to specidy it and not just the subfolder.

    You logic is a bit thin and seems arbitrary.  Try stating the logic as a set of steps.  This wil help you to see why it is an issue.

    To copy the Accounting folder:

    Set fldr = oFSO.GetFolder objFolder & "\" & "Accounting"
    target = backupFolder & "\" & strdate
    fldr.Copy target

    You are trying to specify all folders in one object. This cannot be done.


    ¯\_(ツ)_/¯

    • Marked as answer by jn865 Sunday, February 19, 2012 1:16 AM
    Friday, February 17, 2012 3:59 PM
  • Hi,

    Why implement in VBScript when you can just use Robocopy? It's quite robust.

    Bill

    Friday, February 17, 2012 4:31 PM
  • After reworking through the logic and cleaning up the code I was able to get what I needed. Here is the final code I used to achieve the results I was looking for:

    Set selectedFolder = oFSO.GetFolder(hostedFolder)
    Set colSubfolders = selectedFolder.SubFolders

    For Each objSubfolder In colSubfolders

    ' Check for Accounting data

    If oFSO.FolderExists(objSubfolder & "\Accounting\data") then
    oFSO.CreateFolder(backupFolder & currentdate & "\" & objSubfolder.Name)
    oFSO.CreateFolder(backupFolder & currentdate & "\" & objSubfolder.Name & "\" & "Accounting data")
    oFSO.CopyFolder objSubfolder & "\Accounting\data", backupFolder & currentdate & "\" & objSubfolder.Name & "\" & "Accounting data"

    Thanks for all your help and suggestions.

    Sunday, February 19, 2012 1:19 AM