WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Sample code
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Directory Move

 Keywords: DirMove Folder Directory Move 

Question:

I am interested in moving a directory rather than copying it. I noticed there isn't a DirMove functions. How can these be accomplished in WinBatch?

Answer:

Here are the three methods that you can use in WinBatch to move folders:

Method 1: Windows Shell COM Object

FOF_CREATEPROGRESSDLG = 0 ;Create Progress dialog
sourcedir = 'C:\Scripts'
targetdir = 'D:\Archive'
oShell = ObjectCreate("Shell.Application")
oFolder = oShell.NameSpace(targetdir)
oFolder.MoveHere( sourcedir, FOF_CREATEPROGRESSDLG )
oShell = 0
Method 2: WMI
strComputer = '.'
sourcedir = 'C:\Scripts'
targetdir = 'D:\Archive'
objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" : strComputer : "\root\cimv2")
colFolders = objWMIService.ExecQuery ("Select * from Win32_Directory where name = '" : sourcedir : "'")
ForEach objFolder In colFolders
    errResults = objFolder.Rename( targetdir )
Next
colFolders = 0
objWMIService = 0
Method 3: FileSystem Object
sourcedir = 'C:\Scripts'
targetdir = 'D:\Archive'
objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFolder( sourcedir , targetdir )
objFSO = 0

Article ID:   W17464
File Created: 2008:04:10:15:10:56
Last Updated: 2008:04:10:15:10:56