Link to home
Start Free TrialLog in
Avatar of georgedschneider
georgedschneiderFlag for United States of America

asked on

VBScript to copy folder contents

Is there a way to create a vbscript to copy the contents of the folder to a specified location and then delete the source.  I know how to copy a folder but can't see to figure out how to copy the contents.
Avatar of RobSampson
RobSampson
Flag of Australia image

Hi, what about the MoveFiles method:
http://msdn.microsoft.com/en-us/library/2wcf3ba6(VS.85).aspx

although it's not recursive......

Regards,

Rob.
Set objFSO = CreateObject("Scripting.FileSystemObject")
strSource = "C:\Temp"
strDest = "C:\Temp2"
If Right(strSource, 1) <> "\" Then strSource = strSource & "\"
If Right(strDest, 1) <> "\" Then strDest = strDest & "\"
objFSO.MoveFile strSource & "*", strDest

Open in new window

Avatar of georgedschneider

ASKER

what do you mean by recursive?
Hi,

Non-recursive means if you have files in
C:\Temp
they will get moved, but files in
C:\Temp\Another Folder
will not, as far as I am aware. I can't remember....you could always test it....

Regards,

Rob.
How can I use the date or year function to extract the year in two digit format such as 08 insterad of 2008?
I'm using the following scrip and I keep geting an error
Set objFSO = CreateObject("Scripting.FileSystemObject")
MyDate = Date 
strMonth = Right("0" & Month(MyDate), 2)
strDay = Right("0" & Day(MyDate), 2)
strYear = Right(Year(MyDate),2)
MyFile = strmonth & strday & stryear& "imagic"
objFSO.MoveFile "C:\Test\" & "*", "C:\imagic\"
WScript.Echo "Files Moved"

Open in new window

I needed to use C:\Test\*.* I beleive.  I'll try modying this and see what happens.
Hi, maybe you do need *.*, I can't quite remember.  I notice you have
MyFile = strmonth & strday & stryear& "imagic"

which will give you something like
092908imagic

but you're not explicitly moving it, so it's probably not needed....unless you want to do something with it after it has been moved to the imagic folder...

Regards,

Rob.
I having a little trouble with the syntext of my below script getting  it so it copies the contents of the file named files10072008 for example to the location C:\Test.
Set objFSO = CreateObject("Scripting.FileSystemObject")
MyDate = Date - 1
Wkday = Weekdayname(weekday(Date))
strMonth = Right("0" & Month(MyDate), 2)
strDay = Right("0" & Day(MyDate), 2)
strYear = Right(Year(MyDate),2)
MYFile = "\\server\share\folder\folder name\files" & strmonth & strday & stryear
FileobjFSO.CopyFile MyFile\*.*, "C:\Test"

Open in new window

Hi, try changing line 8 from this:
FileobjFSO.CopyFile MyFile\*.*, "C:\Test"

to this
FileobjFSO.CopyFile MyFile & "\*.*", "C:\Test"

Regards,

Rob.
Actually, no, that should be this
FileobjFSO.CopyFile MyFile & "\*.*", "C:\Test\"

Regards,

Rob.
I got the script to work with the belwo code.  Here's what I'm wonder if its possible to accoumplish.  Each file is named for the date the contents where created for example file100808, file100708, etc.  Is there a way to create something where it would run the folowing for files over a four day span since for eaxample on mOnday the files are created for Friday, Tuesday for Saturday and Monday.  The Saturday files are actual generated late in the day monday.

The problem I'm seeing is the file name creation function is based on today's date - 1 and uses this to create the filename convention.


Set objFSO = CreateObject("Scripting.FileSystemObject")
MyDate = Date - 1
Wkday = Weekdayname(weekday(Date))
strMonth = Right("0" & Month(MyDate), 2)
strDay = Right("0" & Day(MyDate), 2)
strYear = Right(Year(MyDate),2)
MyFile = "C:\filename" & strmonth & strday & stryear
objFSO.CopyFile MyFile & "\*.*","C:\Test\"
objFSO.DeleteFolder MyFile

Open in new window

If you want to go back four days, change this
MyDate = Date - 1

to
MyDate = DateAdd("d", -4, Date)

Regards,

Rob.
Would something like the following work as well?
Set objFSO = CreateObject("Scripting.FileSystemObject")
Wkday = Weekdayname(weekday(Date))
For i=1 to 4
MyDate = Date - i
strMonth = Right("0" & Month(MyDate), 2)
strDay = Right("0" & Day(MyDate), 2)
strYear = Right(Year(MyDate),2)
MyFile = "C:\filename" & strmonth & strday & stryear
If objFSO.FolderExists(Myfile) = True Then 
objFSO.CopyFile MyFile & "\*.*","C:\Test\"
WScript.Echo MyFile
End If
Next

Open in new window

The other think I need to incorporate is the ability to launch a program as well.  I wrote a simple code to do this bellow:

The prblem I'm having is the program I trying to lauch relies on an ini file to run correctly.  In the vendor provided  scheduler program it is written as path to exe nameofinifile.  I thought if I put this in it would work but with no luck.  Have you ever seen somwething like this before.
Set objApp = CreateObject("WScript.Shell") 
objApp.Run "Path to exe"

Open in new window

Yes, that should work with the
For i = 1 To 4

bit.  Also, I have added code to launch an Exe, passing the path of an Ini file.

Regards,

Rob.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Wkday = Weekdayname(weekday(Date))
For i=1 to 4
	MyDate = Date - i
	strMonth = Right("0" & Month(MyDate), 2)
	strDay = Right("0" & Day(MyDate), 2)
	strYear = Right(Year(MyDate),2)
	MyFile = "C:\filename" & strmonth & strday & stryear
	WScript.Echo myfile
	If objFSO.FolderExists(Myfile) = True Then 
		objFSO.CopyFile MyFile & "\*.*","C:\Test\"
		WScript.Echo MyFile
	End If
Next
 
Set objShell = CreateObject("WScript.Shell") 
Set objFSO = CreateObject("Scripting.FileSystemObject")
strExe = "C:\Program Files\MyProgram\Start.exe"
strIni = "C:\Files\INI Files\MyProgramIni.ini"
strExe = objFSO.GetFile(strExe).ShortPath
strIni = objFSO.GetFile(strIni).ShortPath
objShell.Run strExe & " " & strIni, 1, False

Open in new window

What does the second strINI and STrEXE statements do with the .shortpath command?
They just return the shortpath of the file name, to simplify the command line.....saves on having so many quotes....

For example, say a file had a "long path" of:
C:\Program Files\Microsoft Office\Office\WinWord.exe

it's equivalent "short path" is:
C:\Progra~1\Micros~1\Office\WinWord.exe

so there are no spaces in the shortpath, meaning you don't have to put quotes around it when you use it on the command line.

Regards,

Rob.
What does the last line   objShell.Run strExe & " " & strIni, 1, False perform?  What are the settings 1 and False for?  


Hi, see here for the syntax of the objShell.Run command:
http://msdn.microsoft.com/en-us/library/d5fk67ky(VS.85).aspx

as you can see, the 1 and False are the
[intWindowStyle] and [bWaitOnReturn]
parameters, respectively.

So the 1 means that the Window style parameter "Activates and displays a window".

The False means that the script will *not* wait for the program executed by Run to finish, before executing more code.

As you do not need the script to wait, we make that false.  You can test how that works by doing something like this

Set objShell = CreateObject("WScript.Shell")
MsgBox "Notepad will now open, and more code will execute when it closes."
objShell.Run "notepad", 1, True
MsgBox "Notepad just closed."

so that's with True, and this is with False

Set objShell = CreateObject("WScript.Shell")
MsgBox "Notepad will now open, and more code will execute immediately."
objShell.Run "notepad", 1, False
MsgBox "Notepad just started."


Regards,

Rob.
The below is a batch script that will run and execute the program passing the ini file.  How can I get it to do the same thing in the vbscript.
c:
cd\folder\subfolder
executablename.exe --conf inifilename.ini

Open in new window

Hi, this would do that.....

Regards,

Rob.
Set objShell = CreateObject("WScript.Shell") 
Set objFSO = CreateObject("Scripting.FileSystemObject")
strExe = "C:\folder\subfolder\executablename.exe"
strIni = "C:\folder\subfolder\inifilename.ini"
strExe = objFSO.GetFile(strExe).ShortPath
strIni = objFSO.GetFile(strIni).ShortPath
objShell.Run strExe & " " & strIni, 1, False

Open in new window

How do I determine what the short path is?
Do I need these two lines for the script to run?

strExe = objFSO.GetFile(strExe).ShortPath
strIni = objFSO.GetFile(strIni).ShortPath
What I get when I have the below code is C:\folderlocationofscript\pathto the ini (INI) file isi missing.
Set objShell = CreateObject("WScript.Shell") 
Set objFSO = CreateObject("Scripting.FileSystemObject")
strExe = "C:\folder\subfolder\executablename.exe"
strIni = "C:\folder\subfolder\inifilename.ini"
strExe = objFSO.GetFile(strExe).ShortPath
strIni = objFSO.GetFile(strIni).ShortPath
objShell.Run strExe & " " & strIni, 1, False

Open in new window

OK, you can try it without the ShortPaths....you don't need to know what that is, the system works that out....

Regards,

Rob.
Set objShell = CreateObject("WScript.Shell") 
Set objFSO = CreateObject("Scripting.FileSystemObject")
strExe = "C:\folder\subfolder\executablename.exe"
strIni = "C:\folder\subfolder\inifilename.ini"
objShell.Run """" & strExe & """ """ & strIni & """", 1, False

Open in new window

I'm receiving the same error messages that I recevied previosuly with this new script as well.
OK, so we'll go back to the code that uses the ShortPath's.  This code will now present you with an InputBox that will have the command in it.  This you will be able to copy and paste into a Start --> Run box, and see if it works.

Basically, what it *should* run at the moment is just
C:\folder\subfol~1\execut~1.exe C:\folder\subfol~1\inifil~1.ini

Regards,

Rob.
Set objShell = CreateObject("WScript.Shell") 
Set objFSO = CreateObject("Scripting.FileSystemObject")
strExe = "C:\folder\subfolder\executablename.exe"
strIni = "C:\folder\subfolder\inifilename.ini"
strExe = objFSO.GetFile(strExe).ShortPath
strIni = objFSO.GetFile(strIni).ShortPath
strCommand = strExe & " " & strIni
strCommand = InputBox("Prompt", "Title", strCommand)
objShell.Run strCommand, 1, False

Open in new window

The same error message appears.  
If you copy and paste the line from the InputBox into Start --> Run, does it work?

If not, what command written into that Start --> Run box does work?  We can make the script emualate that.

Regards,

Rob.
I can't get anyhting to run from start --> Run.  The only thing that works is the if I run the follwoing batch script:

c:
cd\folder\subfolder
executablename.exe --conf inifilename.ini

Is there someway to convert this to a vb script?
Ah, now it makes sense.  So we can run that command using the following code.

Regards,

Rob.
Set objShell = CreateObject("WScript.Shell") 
Set objFSO = CreateObject("Scripting.FileSystemObject")
strExe = "C:\folder\subfolder\executablename.exe"
strIni = "C:\folder\subfolder\inifilename.ini"
strExe = objFSO.GetFile(strExe).ShortPath
strIni = objFSO.GetFile(strIni).ShortPath
strCommand = strExe & " --conf " & strIni
strCommand = InputBox("Prompt", "Title", strCommand)
objShell.Run strCommand, 1, False

Open in new window

Same result.  I tried it also without the shortpath as well.
What I was able to do is get the program to run by creating a batch file with the followling:

c:
cd\folder\subfolder
executablename.exe --conf inifilename.ini

I then called the batch file form within the script to run the progem I need to run.  The progem is the script appears to be running the program four times.  What I was trying to accoumplish is that the copy procedure occurrs, the batch script is called which runs the import program kicks off and the process is repeated after the program compltes.  Is this even possible?  Below is my vb script.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Wkday = Weekdayname(weekday(Date))
For i=1 to 4
MyDate = Date - i
strMonth = Right("0" & Month(MyDate), 2)
strDay = Right("0" & Day(MyDate), 2)
strYear = Right(Year(MyDate),2)
MyFile = "\\server\share\subfolder\foldername" & strmonth & strday & stryear
If objFSO.FolderExists(Myfile) = True Then 
objFSO.CopyFile MyFile & "\*.*","\\server\share\subfolder\"
Set oShell = CreateObject("Wscript.Shell")
oShell.Run("""c:\foldername\batchfile.bat""")
End If
Next

Open in new window

Ideally I want to run the program from with the script without having to call a batch file to launch it.
OK, so maybe the program only works in a DOS prompt. This will now launch a DOS prompt to run it in.

If that works, you can make the DOS prompt go away automatically by changing this:
      strCommand = "cmd /k " & strExe & " --conf " & strIni

to this
      strCommand = "cmd /c " & strExe & " --conf " & strIni

and then you can also make it hidden by changing this
      objShell.Run strCommand, 1, True

to this
      objShell.Run strCommand, 0, True

Regards,

Rob.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Wkday = Weekdayname(weekday(Date))
For i=1 to 4
MyDate = Date - i
strMonth = Right("0" & Month(MyDate), 2)
strDay = Right("0" & Day(MyDate), 2)
strYear = Right(Year(MyDate),2)
MyFile = "\\server\share\subfolder\foldername" & strmonth & strday & stryear
If objFSO.FolderExists(Myfile) = True Then 
	objFSO.CopyFile MyFile & "\*.*","\\server\share\subfolder\"
	Set oShell = CreateObject("Wscript.Shell")
	strExe = "C:\folder\subfolder\executablename.exe"
	strIni = "C:\folder\subfolder\inifilename.ini"
	strExe = objFSO.GetFile(strExe).ShortPath
	strIni = objFSO.GetFile(strIni).ShortPath
	strCommand = "cmd /k " & strExe & " --conf " & strIni
	objShell.Run strCommand, 1, True
End If
Next

Open in new window

I get an error on line 17, char 2 object requiqure: 'objshell'
Oh, forgot to add the WScript.Shell object....

Regards,

Rob.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Wkday = Weekdayname(weekday(Date))
For i=1 to 4
MyDate = Date - i
strMonth = Right("0" & Month(MyDate), 2)
strDay = Right("0" & Day(MyDate), 2)
strYear = Right(Year(MyDate),2)
MyFile = "\\server\share\subfolder\foldername" & strmonth & strday & stryear
If objFSO.FolderExists(Myfile) = True Then 
	objFSO.CopyFile MyFile & "\*.*","\\server\share\subfolder\"
	Set oShell = CreateObject("Wscript.Shell")
	strExe = "C:\folder\subfolder\executablename.exe"
	strIni = "C:\folder\subfolder\inifilename.ini"
	strExe = objFSO.GetFile(strExe).ShortPath
	strIni = objFSO.GetFile(strIni).ShortPath
	strCommand = "cmd /k " & strExe & " --conf " & strIni
	objShell.Run strCommand, 1, True
End If
Next

Open in new window

I'm still getting a misisng ini file message.  The command promt shows he directory where the script is run from.  How do I cahnge to the directory where the exe and ini file are located if the directory is C:\Files\Subdirectory?
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I used the following script and everything worked fine.  I then decided to change it slightly to test the moving of a folder verses all files within it for testing purposes before I deploy it.  I can copy the folder to a new location but can't move it I get access denied.  The error is at line 10 char. 3.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Wkday = Weekdayname(weekday(Date))
For i=1 to 4
	MyDate = Date - i
	strMonth = Right("0" & Month(MyDate), 2)
	strDay = Right("0" & Day(MyDate), 2)
	strYear = Right(Year(MyDate),2)
	MyFile = "\\server\share\subfolder\foldername" & strmonth & strday & stryear
	If objFSO.FolderExists(Myfile) = True Then 
		objFSO.MoveFolder MyFile, "\\server\share\subfolder\"
		Set oShell = CreateObject("Wscript.Shell")
		strFolder = "C:\Temp\Scripts"
		strExe = "executablename.exe"
		strIni = "inifilename.ini"
		objShell.CurrentDirectory = strFolder
		strCommand = "cmd /c " & strExe & " --conf " & strIni
		objShell.Run strCommand, 0, True
	End If
Next

Open in new window

Hi, I would suspect that moving the folder gives you access denied when it, or a file in it, is being accessed by another process.  Copying would work fine, because it is able to take a copy, but moving it cannot be done if something else has it open....

Regards,

Rob.
I tried running a simple test on my local workstation using the following script.  If I change it to use UNC path instaead it fails with the permission denied error which makes me think its something else other than a perssions or in use issue,
Set objFSO = CreateObject("Scripting.FileSystemObject")
MyFiles = "C:\Test2"
objFSO.MoveFolder MyFiles,"C:\Test3\"

Open in new window

Hmmmm....it's hard to say why it's doing that....but UNC paths usually are being accessed by other resources.....
What if you use a UNC path to your own local machine, where you replace <MYPCNAME> with your host name...

I do believe that the target folder (being C:\Test3) must already exist or this will fail.
Set objFSO = CreateObject("Scripting.FileSystemObject")
strServer = "<MYPCNAME>"
MyFiles = "\\" & strServer & "\C$\Test2"
If objFSO.FolderExists("\\" & strServer & "\C$\Test3\") = False Then objFSO.CreateFolder "\\" & strServer & "\C$\Test3"
objFSO.MoveFolder MyFiles,"\\" & strServer & "\C$\Test3\"

Open in new window

Here's the strange thing I've noticed using both the script you provided and the one I wrote below.  Both work if the files are copied to and from the same location for example a folder on the root of C.  If I change the Test3 to a shared folder modify the script to the followoing utilizing the sharename I get a permission denied error similar to what is happening when moving folders across servers:
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFolder "\\computername\C$\Test","\\computername\Test3\"



Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFolder "\\computername\C$\Test","\\computername\C$\Test3\"

Open in new window

Hi, that's really odd.  This worked for me.

Things that I noticed....
1. If strTargetFolder has a backslash on the end, that folder must exist first, and the source folder will be moved to *inside* that folder.
2. If strTargetFolder has a backslash on the end, and that folder does not exist already, you will get an error
3. If strTargetFolder does not have a backslash on the end, and that folder already exists, you will get an error.
4. If strTargetFolder does not have a backslash on the end, and that folder does not exist, the source folder will be renamed to the name of the target folder.

Also, because in this example, you are accessing the C$ administrative share, you must be a member of the local Administrators group of the server you're connecting to.  If you are not, you will get permission denied.

Similarly, for a different "shared" folder, you need to make sure that you have permissions at both the share level, and the NTFS level.

Usually, in the Sharing tab of the folder, the permissions get set to "Authenticated Users" at Full Control, and then on the Security tab, the permissions are tightened.  The most restrictive permissions take effect. This way, you only ever need to worry about changing one set of permissions on a shared folder.

Check out the sharing permissions on your share and see what you can find out.

Regards,

Rob.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = CreateObject("WScript.Network")
strServer = objNetwork.ComputerName
strSourceFolder = "\\" & strServer & "\C$\Test"
strTargetFolder = "\\" & strServer & "\C$\Test3\"
objFSO.MoveFolder strSourceFolder, strTargetFolder

Open in new window

Your script as wel as the script below works as long as I'm using the fully UNC path including the administrative share.  However if I attempt to use a sharename for the destination folder and modify the line to:
objFSO.MOVEFolder "\\lcomputername\C$\Test2", "\\computername\Test3\"
The script fails.  I have a similar result by modify your script as well.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = CreateObject("WScript.Network")
objFSO.MOVEFolder "\\computername\C$\Test2", "\\computername\C$\Test3\" 

Open in new window

Well that would occur because you can't move a shared folder.

You would need to map to your source folders parent directory, by sharing the parent directory. Say that on the server itself, the local path to the folder you want to move is
D:\Programs\MyFolder\SourceFolder

Then you need to share "MyFolder", because you'll be moving SourceFolder out of it.  Then your code would be:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = CreateObject("WScript.Network")
objFSO.MOVEFolder "\\server\MyFolder\SourceFolder", "\\server\MyFolder\TargetFolder\"

Does that help?  Are you sharing the folder that you're actually trying to move?

Regards,

Rob.
Only the destination folder is shared.  In the code above test2 is a folder off of C while test3 is also a folder off the root of C but is shared.  I'm getting an error when moving test2 into the shared folder test3 if I reference the share name.  If I use the UNC patch with C$ instead I have no problem.  Why wouldn't I be allowed to move a folder into a shared folder.  I checked permission just to make sure and everythign seems fine especialy since a copy folder will work but not a move.
I really can't say....that's quite odd....if you right click the destination shared folder "test3", click Sharing, then click Permissions....does that have Authenticated Users at Full Control?  If it's anything less that Full Control, make it full control, and see if that helps.

Regards,

Rob.
The share leve is everyone full control as well the the ntfs permissions as well.
OK, what happens if you go to a DOS prompt and type
move /Y "\\lcomputername\C$\Test2" "\\computername\Test3\"

Does that give you any error about what it cannot move?

Rob.
Same issue.  Access is denied which oesn't make sense if everyone has full control and on top of that I'm running it as a domain admin.
I alterted the script to do a copy folder and then delete the orignal folder form the source which works.  I'm not sure why the move folder method will not work if i attempt to move it to a different folder other than off the root of the drive.  




Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFolder "\\computername\C$\Test2", "\\computername\Test3\" 
objFSO.DeleteFolder("\\lpbo2ub60700YS\C$\Test2")

Open in new window

Hmmm, to be honest....neither do I....perhaps it's just best to use Copy then Delete instead...

Regards,

Rob.
Thanks for the grade.

Regards,

Rob.