Board index » Visual Studio » Move Folder question

Move Folder question

Visual Studio237
I'm trying to write a script to move folders & subfiles using



objFSO.MoveFolder "D:\SHARE\FOLDER\*" , "D:\Pending Print\FOLDER\"







It works fine, however it will only move the folder if there are no

files within the folder. Is there a parameter or something I am

missing to copy BOTH folders and sub-files?



thank you in advance,


-
 

Re:Move Folder question



"Rich" <coh.techsupport@gmail.com>wrote in message

Quote
I'm trying to write a script to move folders & subfiles using



objFSO.MoveFolder "D:\SHARE\FOLDER\*" , "D:\Pending Print\FOLDER\"







It works fine, however it will only move the folder if there are no

files within the folder. Is there a parameter or something I am

missing to copy BOTH folders and sub-files?



I can't tell what folder(s) you are trying to move. Maybe the system

can't tell either. Maybe you intend to move folder "D:\SHARE\FOLDER"

and all its files and subfolders. Or maybe you intend to move all

folders from within folder "D:\SHARE\FOLDER" and all their files and

subfolders, but not any files within within the root

"D:\SHARE\FOLDER".



The scripting help file says:

source

Required. The path to the folder or folders to be moved. The source

argument string can contain wildcard characters in the last path

component only.



Perhaps your wild card character is not in the path component you

think it is.



-Paul Randall





-

Re:Move Folder question





Essentially I want to move all subfolders of D:\SHARE\FOLDER\ , which

is why I was using the wildcard in the path "D:\SHARE\FOLDER\*" . This

folder is being populated with subdirectories randomly which I do not

know the name of but need to move. This is why I am trying to use the

wildcard.



It is strange the folders are moved without problems when the

subdirectories are empty. However when the subdirectories have files,

the script will not move the folders. Am I missing something?

-

Re:Move Folder question

Rich schrieb:

Quote
I'm trying to write a script to move folders & subfiles using



objFSO.MoveFolder "D:\SHARE\FOLDER\*" , "D:\Pending Print\FOLDER\"







It works fine, however it will only move the folder if there are no

files within the folder. Is there a parameter or something I am

missing to copy BOTH folders and sub-files?



That's not the default behaviour and probably a permission issue.

MoveFolder move folders with or without files inside.



Btw: The files in "D:\SHARE\FOLDER\" itself will of course NOT be

moved, use MoveFiles instead.



MfG,

Alex

-

Re:Move Folder question



"Rich" <coh.techsupport@gmail.com>wrote in message

Quote




Essentially I want to move all subfolders of D:\SHARE\FOLDER\ ,

which

is why I was using the wildcard in the path "D:\SHARE\FOLDER\*" .

This

folder is being populated with subdirectories randomly which I do

not

know the name of but need to move. This is why I am trying to use

the

wildcard.



It is strange the folders are moved without problems when the

subdirectories are empty. However when the subdirectories have

files,

the script will not move the folders. Am I missing something?



I think the following script demonstrates what you are trying to do.

To test it, place it in a .VBS file in an otherwise empty folder, and

run it. It works on my WXP SP2 system.



Option Explicit

Dim objFSO

Set objFSO = CreateObject("scripting.filesystemobject")

Dim objShell

Set objShell = CreateObject("WScript.Shell")

Dim sCurFolder



'Force the current directory to be the directory the script resides

in.

objShell.CurrentDirectory = _

objFSO.GetParentFolderName(WScript.ScriptFullName)

sCurFolder = objShell.CurrentDirectory



'Delete subfolder named "Main_Folder", if it exists

If objFSO.FolderExists(sCurFolder & "\Main_Folder") Then

objFSO.DeleteFolder sCurFolder & "\Main_Folder", True

End If



'Delete subfolder named "Main_Folder_Copy", if it exists

If objFSO.FolderExists(sCurFolder & "\Main_Folder_Copy") Then

objFSO.DeleteFolder sCurFolder & "\Main_Folder_Copy", True

End If



'Create a set of subfolders and files with the following structure:

'CurrentFolder

' Main_Folder

' SubFolder_1

' Text1.txt

' SubFolder_2

' Text2.txt

' TextMain.txt

objFSO.CreateFolder sCurFolder & "\Main_Folder"

objFSO.CreateFolder sCurFolder & "\Main_Folder\SubFolder_1"

objFSO.CreateFolder sCurFolder & "\Main_Folder\SubFolder_2"

fOverWrite sCurFolder & "\Main_Folder\TextMain.txt", "TextMain"

fOverWrite sCurFolder & "\Main_Folder\SubFolder_1\Text1.txt", "Text1"

fOverWrite sCurFolder & "\Main_Folder\SubFolder_2\Text2.txt", "Text2"

objFSO.CreateFolder sCurFolder & "\Main_Folder_Copy"

MsgBox "Verify that source folders and files exist, and that the " & _

"empty destination folder exists, then click OK to do the move"



'objFSO.MoveFolder "D:\SHARE\FOLDER\*" , "D:\Pending Print\FOLDER\"

objFSO.MoveFolder sCurFolder & "\Main_Folder\*", _

sCurFolder & "\Main_Folder_Copy\"

'The folder structure should now be:

'Current folder containing the script

' Main_Folder

' TextMain.txt

' Main_Folder_Copy

' SubFolder_1

' Text1.txt

' SubFolder_2

' Text2.txt

MsgBox "Done"



Sub fOverWrite(sFileName, sText)

'Write string sText to file sFileName.

'If sFileName already exists, overwrite it.

'First try writing as Ansi 8-bit characters. If an error

' occurs, try writing as Unicode 16-bit characters.

With CreateObject("Scripting.FileSystemObject")

On Error Resume Next

.CreateTextFile(sFileName, True, False).Write(sText)

If Err Then

On Error GoTo 0

.CreateTextFile(sFileName, True, True).Write(sText)

End If

On Error GoTo 0

End With

End Sub



-Paul Randall





-

Re:Move Folder question

Wow, I tested and this works great. Thank you for the assistance with

this.

-

Re:Move Folder question

hi Rich,



You seem to be happy with the code already offered, but

there is another method to keep in mind for future use:

i.e., the "Shell.Application" "MoveHere" method. Here

is a discussion:



msdn2.microsoft.com/en-us/library/bb787874(VS.85).aspx?wt.svl=overview">msdn2.microsoft.com/en-us/library/bb787874(VS.85).aspx?wt.svl=overview



And here is some sample (air) code:



--- <code>---

' script to demo shell.application (for IE4/5 in win98)

Option Explicit

Dim oSHApp ' as object

Dim sSrc, sDest ' as string

Const FOF_SIMPLEPROGRESS = 256 '(&H100)



Set oSHApp = CreateObject("Shell.Application")



sSrc = "c:\windows\temp\*.*"

sDest = "A:\"



' use ms Animated Move Applet, showing names and progressbar...

oSHApp.Namespace(sDest).MoveHere sSrc



' use ms Animated Move Applet, showing progressbar (but no names)...

' oSHApp.Namespace(sDest).MoveHere sSrc, FOF_SIMPLEPROGRESS



Set oSHApp = nothing ' clean up

WScript.Quit

--- </code>---



One advantage of using shell.app over fso is you get the

system's progress dialog -- which for lengthy moves will

entertain the user while the move is going on. Note: if

the move is a very quick one, you WON'T see the dialog.



cheers, jw

____________________________________________________________



You got questions? WE GOT ANSWERS!!! ..(but,

no guarantee the answers will be applicable to the questions)





Rich wrote:

Quote
I'm trying to write a script to move folders & subfiles using



objFSO.MoveFolder "D:\SHARE\FOLDER\*" , "D:\Pending Print\FOLDER\"



It works fine, however it will only move the folder if there are no

files within the folder. Is there a parameter or something I am

missing to copy BOTH folders and sub-files?



thank you in advance,

-