Announcement

Collapse
No announcement yet.

Moving folders to/from UNC Paths (VBS)

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Moving folders to/from UNC Paths (VBS)

    Can anyone see issues with this?

    if I try with c:\public it works fine.
    if I try with \\computer\public it doesn't
    if I try with \\computer\c$\public it doesn't
    if I try with the current code using the strComputer it also doesn't
    all the above (with the exception of works fine) it gives permission errors...
    All permissions are fine though - I have full access to this machine - my only guess is is doesn't like UNC paths... but how do I get around that?

    Code:
    strComputer = "\\computer"
    strArchDate = "archive_" & FormatDateTime(date(),vblongdate)
    
      Dim objFSO        : Set objFSO        = CreateObject("Scripting.FileSystemObject" )
      Dim objFolder     : Set objFolder     = objFSO.GetFolder( "c:\testfolder" )
      Dim colSubFolders : Set colSubFolders = objFolder.Subfolders
      Dim dicSelFolders : Set dicSelFolders = CreateObject("Scripting.Dictionary" )
    
    dim Movesys
    set Movesys = CreateObject("Scripting.FileSystemObject")
    
      dicSelFolders.Add "admin", ""
      dicSelFolders.Add "local"  , ""
      dicSelFolders.Add strArchDate  , ""
    
      Dim objSubfolder
      For Each objSubfolder in colSubfolders
    '     On Error Resume Next
          If dicSelFolders.Exists( LCase( objSubfolder.Name ) ) Then
             WScript.Echo "Protected", objSubfolder.Path
          Else
             WScript.Echo "Moving", objSubfolder.Path
             Movesys.MoveFolder objSubfolder.Path, strComputer & "\public\_test_\_Archive_" & FormatDateTime(date(),vblongdate) & "\"
          End If
      Next

    Aside from that the archive one which will have today's date will not get processed properly... any ideas there?

    this is because of the spaces in it - - any ideas if its possible - if not i'll have to find another method of excluding folders
    Last edited by Lan; 14th August 2007, 18:04.
    This is my signature.

  • #2
    Re: Moving folders to/from UNC Paths (VBS)

    Just a thought:
    Check both the NTFS and SHARE permissions, since all the failing options are accessing a share
    Tom Jones
    MCT, MCSE (2000:Security & 2003), MCSA:Security & Messaging, MCDBA, MCDST, MCITP(EA, EMA, SA, EDA, ES, CS), MCTS, MCP, Sec+
    PhD, MSc, FIAP, MIITT
    IT Trainer / Consultant
    Ossian Ltd
    Scotland

    ** Remember to give credit where credit is due and leave reputation points where appropriate **

    Comment


    • #3
      Re: Moving folders to/from UNC Paths (VBS)

      I'm an administrator which has full control on the machine, logged onto the machine, and have full control to the share too... I don't think it's "actually" permissions problem - that just seems to be what it's reporting... when I change it from the computername+share to c:\folder it works fine - so it shouldn't be security permissions - at most it'd be just the share...

      I've not had any other problems other scripts doing things using these paths other than the moving folders...

      Perhaps I need to find another way of doing this
      This is my signature.

      Comment


      • #4
        Re: Moving folders to/from UNC Paths (VBS)

        Originally posted by Lan View Post
        ... I don't think it's "actually" permissions problem - that just seems to be what it's reporting...
        Perhaps I need to find another way of doing this
        You are right, it is not about permissions here, it is the "Scripting.FileSystemObject" method.

        The FileSystemObject is designed to work locally (to an actual local storage device on a computer system running Windows).
        Although, the FileSystemObject is capable of reading UNC paths, you cannot use the object against remote machines for all tasks.

        The script you use must be run on the local computer, although you know that the unc-path Is local, but it is not for the FileSystemObject.

        For your case you must use WMI.
        However, The Win32_Directory class does not provide a one-step method for moving folders (when moving between drives).
        - Moving a folder generally involves two steps:
        1. Copying the folder to its new location
        2. Deleting the original folder (be sure copying was successfull!!)

        (The one exception to this two-step process involves moving a folder to a new location on the same drive. As long as the current location and the new location are on the same drive, you can move the folder by simply calling the 'Rename method' and passing the new location as the method parameter. This approach effectively allows you to move the folder in a single step http://www.microsoft.com/technet/scr....mspx?mfr=true)


        \Rems

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment


        • #5
          Re: Moving folders to/from UNC Paths (VBS)

          Ah thanks.

          It will always be moving things from a network location to another folder on the same network location. Just ran from another machine...
          If I am copying and deleting the files any idea if it will it be just as quick as the move?

          yea, in my test copy wasn't successful and it deleted it all - good job it was a test directory first...
          Last edited by Lan; 15th August 2007, 12:37.
          This is my signature.

          Comment


          • #6
            Re: Moving folders to/from UNC Paths (VBS)

            You could add a little function to check if the new folder exists before deleting the old:


            Code:
            strTargetComputer = "computername"
            strTargetFolder = "c:\test scripts"
            
            Set objWMIService = GetObject("winmgmts:" _
                & "{impersonationLevel=Impersonate}!\\" _
                & strTargetComputer & "\root\cimv2")
            
            '<script here>
            
            'set condition for deleting the source folder:
            
            If FolderExist(objWMIService, strTargetFolder) Then 
               Wscript.Echo "Target Folder Exists, you can delete the source"
            ElseIf Not FolderExist(objWMIService, strTargetFolder) Then
               Wscript.Echo "OEPS, better not delete the source"
            End If
            
            wscript.quit
            
            
            Function FolderExist(ByVal oWMI, ByVal sTgtFldr)
               sTgtFldr = Replace(sTgtFldr, "\", "\\")  '<--only if needed(?)
             'http://www.microsoft.com/technet/scriptcenter/resources/qanda/aug04/hey0819.mspx
               FolderExist = cbool(oWMI.ExecQuery _
                ("Select * From Win32_Directory Where " & _
                    "Name = '" & sTgtFldr & "'").Count)
            End Function
            \Rems
            Last edited by Rems; 15th August 2007, 16:03. Reason: modified the function

            This posting is provided "AS IS" with no warranties, and confers no rights.

            __________________

            ** Remember to give credit where credit's due **
            and leave Reputation Points for meaningful posts

            Comment


            • #7
              Re: Moving folders to/from UNC Paths (VBS)

              Ah the general idea of the whole script - http://forums.petri.com/showthread.php?t=17636 - was to create an archive folder on a remote machine, copy all the contents apart from the couple of folders I've specified into the new (archive) folder...

              at the moment I have to create an archive folder with the date, selecting all - deselecting the few I don't want moved, then move the contents into the archive folder just incase... I was hoping to reduce this task to a few clicks

              I got it creating a new folder with the date - making a log file (but not putting anything in it) then got stuck on moving all the folders apart from the few specified folders... I got most of it working but as mentioned only locally...

              So it'd just be moving all the folders (bar a couple) into an empty folder - but then I just thought... I was going to say it wouldn't exist in the target folder - but this is copying then deleting so it would exist..

              I've confused myself with all this


              if it helps the idea of the thing was;
              *Create log file on local machine
              *create archive folder on remote machine (archive date')
              *EXCLUDE 'admin'/'local file' and 'archive date' folders; but copy entire contents (folders) into archive date folder.
              log the folders moved into the log file...
              Last edited by Lan; 15th August 2007, 16:02.
              This is my signature.

              Comment


              • #8
                Re: Moving folders to/from UNC Paths (VBS)

                Don't know if there was a question in your last post, but here I have
                3 examples how to create a new folder on a remote machine using WMI.
                (You can use the third one I think)

                1.
                - This script connects only to the WMI-service on the remote computer.
                - It is able to browse all the folders on the remote computer, it is not depending on shares.
                Code:
                ' name: Create remote folder 1 (WMI method).vbs
                ' by  : Remco Simons [nl] 2007
                
                'http://forums.petri.com/showthread.php?t=17752
                'This script only connects to the WMI-service on the destination computer.
                'it is capable to use any local path that exists on the destination computer, it is not depending on shares.
                
                '(In addition;
                'this script can also create folders locally on this computer, therefore enter "." instead of the "computername")
                
                
                DESTINATION_COMPUTER = "Computername"
                DESTINATION_PATH     = "C:\test1 folder\"
                TARGET_FOLDER        = "archive_1"
                
                strNewPath = DESTINATION_PATH & TARGET_FOLDER
                
                Set objWMIService = GetObject("winmgmts:" _
                        & "{impersonationLevel=Impersonate}!\\" _
                        & DESTINATION_COMPUTER & "\root\cimv2")
                
                call CreateFolder(objWMIService, strNewPath)
                
                'done..
                
                Wscript.Quit
                
                
                Sub CreateFolder(ByVal oWMI, ByVal sTrgtFldr)
                 'http://www.microsoft.com/technet/scriptcenter/resources/qanda/sept04/hey0901.mspx
                  sTrgtFldr = Replace(sTrgtFldr,chr(34), "")
                  errReturn = oWMI.Get("win32_process").Create _
                     ("cmd.exe /c md " & chr(34) & sTrgtFldr & chr(34), Null, Null, intProcessID)
                  wscript.sleep 111 'update
                End Sub

                2.
                - This script connects temporaly to the remote computer in a subroutine.
                - This script is also able to browse all the folders on the remote computer, it is not depending on shares.
                - With this script you could also create in its main part a WMI-object that connects to the WMI service of the local computer (".")
                - this script is able to compare folder/file content between the two computers
                Code:
                ' name: Create remote folder 2 (WMI method).vbs
                ' by  : Remco Simons [nl] 2007
                
                'http://forums.petri.com/showthread.php?t=17752
                'This script connects in the Subroutine to the WMI-service on the destination computer.
                'It is capable to use any local path that exists on the destination computer, it is not depending on shares.
                'It is possible to create in the main part of this script an other WMI-object that connects to the WMI-service on this computer.
                
                '(In addition;
                'the CreateFolder Subroutine in this script can also create folders locally on this computer, therefore enter "." instead of the "computername")
                
                
                DESTINATION_COMPUTER = "Computername"
                DESTINATION_PATH     = "C:\test2 folder\"
                TARGET_FOLDER        = "archive_2"
                
                strNewPath = DESTINATION_PATH & TARGET_FOLDER
                
                call CreateFolder(DESTINATION_COMPUTER, strNewPath)
                
                'done..
                
                Wscript.Quit
                
                
                Sub CreateFolder(ByVal TargetComputer, ByVal sTrgtFldr)
                 'http://www.microsoft.com/technet/scriptcenter/resources/qanda/sept04/hey0901.mspx
                  sTrgtFldr = Replace(sTrgtFldr,chr(34), "")
                  errReturn = GetObject("winmgmts:" _
                         & "{impersonationLevel=Impersonate}!\\" _
                         & TargetComputer & "\root\cimv2:win32_process").Create _
                         ("cmd.exe /c md " & chr(34) & sTrgtFldr & chr(34), Null, Null, intProcessID)
                        wscript.sleep 111    
                End Sub

                3.
                - This script connect only to the host computer (".")
                - It uses a unc-path to reach files and folders on the shared computer
                - You must use this method if you not only want to compare, but also want to transfer files between the two computers!
                Code:
                ' name: Create remote folder 3 (WMI method).vbs
                ' by  : Remco Simons [nl] 2007
                
                'http://forums.petri.com/showthread.php?t=17752
                'This script connects to the WMI-Service only of this local computer
                'it must use an UNC-path to create a new path on a shared computer
                
                '(In addition;
                'this script can also create folders locally on this computer, therefore instead of a UNC-path enter a driveletter that exists on this computer)
                
                
                DESTINATION_ROOT = "\\Computername\c$"  
                DESTINATION_PATH = "\test3 folder\"
                TARGET_FOLDER    = "archive_3"
                
                strNewPath = DESTINATION_ROOT & DESTINATION_PATH & TARGET_FOLDER
                
                
                strComputer = "."
                Set objWMIService = GetObject("winmgmts:" _
                        & "{impersonationLevel=Impersonate}!\\" _
                        & strComputer & "\root\cimv2")
                
                call CreateFolder(objWMIService, strNewPath)
                
                'done..
                
                
                'optional - create a temporary drivemapping to the target folder.
                'the mapping can be used for copying subfolders to it.
                
                Set objNetwork = CreateObject("WScript.network")
                tmpDrive = FreeDrive()
                objNetwork.MapNetworkDrive tmpDrive, strNewPath, False
                
                wscript.echo "tmpDrive ", tmpDrive, "  <== ", strNewPath
                
                objNetwork.RemoveNetworkDrive tmpDrive, True, True
                wscript.sleep 111 'update
                Set objNetwork = Nothing
                Set objWMIService = Nothing
                
                Wscript.Quit
                
                
                Function FreeDrive() 
                  Dim i
                  With CreateObject("Scripting.FileSystemObject") 
                    For i = Asc("D") To Asc("Z") 
                      If Not .DriveExists(Chr(i)) Then 
                        FreeDrive = Chr(i) & ":" 
                        Exit Function 
                      End If 
                    Next
                  End With 
                End Function 
                
                
                Sub CreateFolder(ByVal oWMI, ByVal sTrgtFldr)
                 'http://msdn2.microsoft.com/en-us/library/aa394599.aspx
                  Const HIDDEN_WINDOW = 0
                  sTrgtFldr = Replace(sTrgtFldr,chr(34), "")
                  With oWMI.Get("Win32_ProcessStartup")
                   Set objConfig = .SpawnInstance_
                   objConfig.ShowWindow = HIDDEN_WINDOW
                  End With
                  With GetObject( _
                    "winmgmts:root\cimv2:Win32_Process")
                   errReturn = .Create( _
                     "cmd.exe /c md " & chr(34) & sTrgtFldr & chr(34), _
                     Null, objConfig, intProcessID )
                  End With
                  Set objConfig = Nothing
                  wscript.sleep 111 'update
                End Sub
                (if you look close you see they are all using the dos command: MD to create the Folder. It is also able to create a complete new path at once).

                The Subroutine CreateFolder() in all three examples are about the same, They all use a WMIservice-object. In the scripts 2 and 3 these subroutines are infact the same, the only difference between them that the last one run the command hidden.

                In script 3, I added also a temporary drive mapping, that makes it possibly more easier to move the folders from the local computer to the new archive folder on the shared computer.

                Copying folders using WMI can be done with just a little bit more and this line:
                errResults = objFolder.CopyEx(objFolder.Name,,,True)
                'CopyEx' will copy the folder and all its content all subfolders included to the new destination.

                But deleting the folders on the other hand, takes more code, to cycle throug all the possible subfolders and delete them recursively one by one, since ServicePack 2 for Windows XP it does not allow errResults = objFolder.Delete any more to delete folders if they still contain subfolders. You can find examples in the internet how to deal with this.
                http://www.microsoft.com/technet/scr...5/hey0405.mspx
                http://www.microsoft.com/technet/scr...7/hey0126.mspx
                But for copying and for the deletion of folders you still can use the "Scripting.FileSystemObject" (both of them support the use of unc paths or mapped drives).
                The FileSystemObject.DeleteFolder method automaticaly deletes all subfolders too.

                keep in mind that,
                - Open files cannot be deleted!


                \Rems

                This posting is provided "AS IS" with no warranties, and confers no rights.

                __________________

                ** Remember to give credit where credit's due **
                and leave Reputation Points for meaningful posts

                Comment


                • #9
                  Re: Moving folders to/from UNC Paths (VBS)

                  hm - for creating a folder I was just using here;

                  Code:
                  dim filesys, ArchFol, ArchPath
                  ArchPath = strComputer & "\c$\_test_\_Archive_" & FormatDateTime(date(),vblongdate)
                  set filesys=CreateObject("Scripting.FileSystemObject")
                  If Not filesys.FolderExists(ArchPath) Then
                  Set ArchFol = filesys.CreateFolder(ArchPath)
                  End If
                  which seemed to work --- the main problem was I couldn't move folders to this new created folder also excluding specific folders (which also included spaces) so I was looking for a way to exclude and move on a remote machine...


                  I wouldn't need to transfer files between the local machine and the remote - just remote -> another folder in remote. The only reason for not running this on the local machine was that I am too lazy to log into it - I usually do it through windows explorer and c$
                  This is my signature.

                  Comment


                  • #10
                    Re: Moving folders to/from UNC Paths (VBS)

                    I haven't read the complete post but isn't it easier to use robocopy instead.
                    Marcel
                    Technical Consultant
                    Netherlands
                    http://www.phetios.com
                    http://blog.nessus.nl

                    MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
                    "No matter how secure, there is always the human factor."

                    "Enjoy life today, tomorrow may never come."
                    "If you're going through hell, keep going. ~Winston Churchill"

                    Comment


                    • #11
                      Re: Moving folders to/from UNC Paths (VBS)

                      Can you do all that with robocopy?
                      This is my signature.

                      Comment

                      Working...
                      X