VB Script to move files when emigrating users from XP to Windows 7

by Tomas | Aug 24, 2011 | Scripting, VB Script

I writing a VB script that will move the user's document in the home directory to a "My Documents" subfolder that is re-direct folder in Windows 7 and will be available in offline mode. Is not an expert on this but it seems to work. Updated to new version.

Move files

Option Explicit

Dim objFSO
Dim StrDrive
Dim folderobj
Dim FromFolder
Dim ToFolder
Dim FromPath
Dim ToPath
Dim objSubfolder
Dim colSubFolders 
Dim dicSelFolders
Dim FolderToMoveFrom
Dim FolderToMoveTo
Dim Files
Dim OneFile
Dim FileInFromFolder
Dim Fileset
Dim SourceFileName
Dim DestFileName
Dim FileCount
Dim DestFile
Dim DocFolderExist

StrDrive = "F"
FromFolder = ""
ToFolder = "Mina dokument"

Strdrive=UCase(Left(StrDrive,1)) & ":\"

Set objFSO        = CreateObject( "Scripting.FileSystemObject" )
Set FromPath      = objFSO.GetFolder( StrDrive & FromFolder )
Set ToPath        = objFSO.GetFolder( StrDrive & ToFolder ) 
Set colSubFolders = FromPath.Subfolders
Set dicSelFolders = CreateObject( "Scripting.Dictionary" )

'Folders not to move
dicSelFolders.Add "$recycle.bin", ""
dicSelFolders.Add "mina dokument", ""
dicSelFolders.Add "program", ""

'if folder does not exist on destination drive,
' create it and continue

If Not objFSO.FolderExists(StrDrive & "\" & ToFolder) Then
   'WScript.Echo  "Normal run"
   objFSO.CreateFolder StrDrive & ToFolder
   MoveUserFolders()
   MoveUserFiles()
Else
   'WScript.Echo "Rename Mina dokument"
   objFSO.MoveFolder StrDrive & ToFolder, StrDrive & "Gamla " & ToFolder
   objFSO.CreateFolder StrDrive & ToFolder
   MoveUserFolders()
   MoveUserFiles()
End If

Wscript.Quit

Sub MoveUserFolders()

For Each objSubfolder in colSubfolders

If dicSelFolders.Exists( LCase( objSubfolder.Name ) ) Then
   'WScript.Echo "Leaving Folder", objSubfolder.Path
Else
   'WScript.Echo "Moving Folder", objSubfolder.Path
   If FromFolder = "" Then
      FolderToMoveFrom = FromPath & objSubfolder.Name
   Else
      FolderToMoveFrom = FromPath & "\" & objSubfolder.Name
   End If
   FolderToMoveTo = ToPath & "\" & objSubfolder.Name
   objFSO.MoveFolder FolderToMoveFrom, FolderToMoveTo
End If
Next

End Sub

Sub MoveUserFiles()

Set Fileset = FromPath.Files

'if no more subfolders, then go through files
For Each OneFile in Fileset
  SourceFileName = FromPath & "\" & OneFile.name
  DestFileName = ToPath & "\" & OneFile.name

  'if the destination file doesn't exist,
  'then copy the sourcefile to the destination folder
  If Not objFSO.FileExists(DestFileName) Then
    On Error Resume Next
    OneFile.Copy DestFileName
    OneFile.Delete
    On Error GoTo 0
    FileCount = FileCount + 1
  'if the destination file already exists
  Else
    Set DestFile=objFSO.GetFile(DestFileName)
    
    'then check to see if the source file's last modified date
    'is newer than the destination file. If it is,
    'then overwrite the destination file with the source file
    If onefile.DateLastModified > destfile.DateLastModified Then
      'turn all file attributes off to copy updated file over older one
      DestFile.Attributes = 0
      On Error Resume Next
      onefile.Copy DestFileName, True
      On Error GoTo 0
      OneFile.Delete
      FileCount = FileCount + 1
    End If
    Set destfile=Nothing
  End If
Next

End sub

Written by Tomas

I have a broad experience of most things in IT and still has a burning interest in learning new technologies and how to best use the technology.

Related Posts

Powershell GUI to encrypt string
Powershell GUI to encrypt string

I’ve often found myself with tasks eligible for automation. Automating is great with PowerShell until you need to pass credentials into a script. I have seen many administrators put passwords into the body of their script. For testing purposes, this could considered a...

read more
Hyper-V GUI in Powershell
Hyper-V GUI in Powershell

A small powershell script that i make to test powershell script with gui. In this gui a can see all my hyper-v guest i have. I can start/stop, connect and delete machines in this gui. <# Solution: Hyper-V Purpose: Hyper-V GUI Version: 2.0.1 Date: 08 Mars 2021...

read more

0 Comments