Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Visual Basic ( VB ) (http://www.go4expert.com/forums/visual-basic/)
-   -   VBScript to copy file from 1 path to another path (http://www.go4expert.com/forums/vbscript-copy-file-1-path-path-t28752/)

tyme 19Jul2012 15:40

VBScript to copy file from 1 path to another path
 
Dear all,

I want to copy files from a folder. Basically i i want to copy files if the folder exist. If it does not exist then check another folder. I have 3 folders to look at. The folder name is in date format yymmdd. But i just want to look at for 1st, 2nd or 3rd day of the month only. Basically the flow is like this but i don't know how to create a complete vbscript.

If folder yymm01 exist at path: \\servername\sample\ then copy files in it to \\servername\prod\ then exit.
else
check if folder yymm02 exist at path: \\servername\sample\ then copy files in it to \\servername\prod\ then exit.
else
check if folder yymm03 exist at path: \\servername\sample\ then copy files in it to \\servername\prod\ then exit.


Thanks in advance for any help.

Scripting 20Jul2012 01:11

Re: VBScript to copy file from 1 path to another path
 
Maybe you can try using CopyFile function. So something like this My.Computer.FileSystem.CopyFile(sourceFileName ,destinationFileName)
Im not VB expert, but I hope you will find some more on google :)

tyme 20Jul2012 14:43

Re: VBScript to copy file from 1 path to another path
 
Dear all,

I manage to create the script to copy the files but i got error: Object required:" at the highlighted line, char 6. Does anyone know what is wrong with my script?

Code:


Const ForReading = 1, ForWriting = 2 , ForAppending = 8
Dim objFSO
Dim strSortDate
Dim strSortDate2
Dim fso
Dim file
Dim year
DIm month
strSortDate = DatePart("yyyy",Date)
strSortDate2 = DatePart("m",Date)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set fso = CreateObject("Scripting.FileSystemObject")
strSortDate = DatePart("yyyy",Date)
year = Right(strSortDate,2)
strSortDate2 = DatePart("m",Date)
 
If objFSO.FolderExists("\\servername\samplefile\" & year & "0" & strSortDate2 & "01") Then
MsgBox("Hello!")
objFSO.copyfile file.path,\\servername\prod\ & strSortDate & "" & strSortDate2 & "01\" & file.name
Else
If objFSO.FolderExists("\\servername\samplefile\" & year & "0" & strSortDate2 & "02") Then
MsgBox("gotcha!")
objFSO.copyfile file.path, \\servername\prod\ & strSortDate & "" & strSortDate2 & "02\" & file.name
Else
If objFSO.FolderExists("\\servername\samplefile\ & year & "0" & strSortDate2 & "03") Then
objFSO.copyfile file.path,\\servername\prod\& strSortDate & "" & strSortDate2 & "03\" & file.name

End if
set objFSO = nothing
set fSO = nothing
End if



All times are GMT +5.5. The time now is 22:34.