# move folders using VBS



## ~DC:UK~ (Sep 15, 2004)

Hi all,

I have created a script that will move folders on our server from one location to another:

_on Error Resume Next

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

fso.CreateFolder "E:\VXBD\shared\backup\" & Day(Now) & "_" & Month(Now) & "_" & Year(Now)

fso.MoveFolder "E:\VXBD\shared\projects\A*", "E:\VXBD\shared\backup\" & Day(Now) & "_" & Month(Now) & "_" & Year(Now)_

This works fine but if there is a directory that already exists, the script will not overwrite it, just stops with an error (which is why I have used the on error resume next).

Can anyone suggest a script that will move folders from one location to another even if folders with the same name exist in the destination directory?

thanks.


----------



## Glaswegian (Dec 5, 2004)

Hello

You can use Name to move files between folders, like this

```
Name "E:\VXBD\shared\backup\oldfile" As "E\VXBD\shared\projects\newfile"
```
rather than moving folders.

HTH

Regards


----------



## ~DC:UK~ (Sep 15, 2004)

My script has to move a bunch of folders beginning with 'A' because an application on our server keeps looking for these folders instead of looking in the default location, but it always checks this location first.

I just need a bunch of folders from our "\shared\projects\" directory to be moved to "\shared\backup\".


----------



## Fredledingue (Aug 22, 2003)

This script will delete the folder if it already exists before creating and moving the new one.

'------Check if folder exists--------------
afolder = "E:\VXBD\shared\backup\" & Day(Now) & "_" & Month(Now) & "_" & Year(Now)
Set Fso = CreateObject("Scripting.FileSystemObject")

If Fso.FolderExists(afolder) Then
afolder.Delete
fso.CreateFolder "E:\VXBD\shared\backup\" & Day(Now) & "_" & Month(Now) & "_" & Year(Now)
fso.MoveFolder "E:\VXBD\shared\projects\A*", "E:\VXBD\shared\backup\" & Day(Now) & "_" & Month(Now) & "_" & Year

Else

fso.CreateFolder "E:\VXBD\shared\backup\" & Day(Now) & "_" & Month(Now) & "_" & Year(Now)
fso.MoveFolder "E:\VXBD\shared\projects\A*", "E:\VXBD\shared\backup\" & Day(Now) & "_" & Month(Now) & "_" & Year

End If


----------



## Fredledingue (Aug 22, 2003)

Maybe adding ,True or ,False at the end of the string could change the way it behave with existing folders...


----------



## ~DC:UK~ (Sep 15, 2004)

Thanks guys, I have managed to create a script that suits our needs now.

The only thing that puzzles me is the CopyFolder object. I thought it has a OverWriteFiles argument but I could not get that to work. This is what I have decided to go with:

_
on Error Resume Next

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

thismonth = Month(Now)
projectfiles="E:\VXBD\shared\projects\"
backupfiles="E:\VXBD\shared\backup\"
createmonths=Day(Now) & "_" & Month(Now) & "_" & Year(Now)
filetypetomove="A*"

If thismonth = 1 Then thismonth = "January"
If thismonth = 2 Then thismonth = "February"
If thismonth = 3 Then thismonth = "March"
If thismonth = 4 Then thismonth = "April"
If thismonth = 5 Then thismonth = "May"
If thismonth = 6 Then thismonth = "June"
If thismonth = 7 Then thismonth = "July"
If thismonth = 8 Then thismonth = "August"
If thismonth = 9 Then thismonth = "September"
If thismonth = 10 Then thismonth = "October"
If thismonth = 11 Then thismonth = "November"
If thismonth = 12 Then thismonth = "December"

If Not fso.FolderExists(backupfiles & thismonth) then
fso.CreateFolder backupfiles & thismonth
end if

fso.CreateFolder backupfiles & thismonth & "\" & createmonths

fso.MoveFolder projectfiles & filetypetomove, backupfiles & thismonth & "\" & createmonths
_

So this will take all folders beginning with "A" and move them into a directory named with the current date separated by underscores. Each of these are placed into a directory named with the current month.

This stops our bespoke application checking for the folders beginning with "A" and forces it to load from the proper location.

Thanks for the help everyone, much appriciated.


----------

