Script needed to copy files from one network share to another

I need a script that can query a directory of multiple folders and files and find two pdf files with the current date in the file name (date format ex. 5-27-2009), and then copy them to a specified directory.  

If you can tell me where in the script I put the first network path for the lookup & copying of the files and then the second place to put the destination path the files need to be copied to, that would be great.
LVL 4
seaninmanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jared LukerCommented:
What is the extension?  .doc, .xls, etc...
0
Jared LukerCommented:
This script will check a c:\data folder (and all subfolders) for a file in the format of month-date-year.txt.  If it finds a file with today's date, it copies it to c:\data\destination.

All this can be customized to suit your needs for source and destination file locations.
strComputer = "."
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim SizeCount
 
Const ForAppending = 8
Const ForWriting = 2
Const ForReading = 1
 
WorkingDate = Date
DateFormated = Replace(WorkingDate,"/","-")
DateSplit = Split(DateFormated,"-")
 
If Len(DateSplit(0)) = 2 Then
	DateMonth = DateSplit(0)
Else
	DateMonth = "0" & DateSplit(0)
End If
	
If Len(DateSplit(1)) = 2 Then
	DateDay = DateSplit(1)
Else
	DateDay = "0" & DateSplit(1)
End If
 
DateYear = DateSplit(2)
 
DateFinal = DateMonth & "-" & DateDay & "-" & DateYear
 
WScript.Echo "Today's Date: " & datefinal
 
 
objStartFolder = "C:\data"
 
Set objFolder = objFSO.GetFolder(objStartFolder)
Wscript.Echo objFolder.Path
Set colFiles = objFolder.Files
For Each objFile in colFiles
    Wscript.Echo objFile.Name
    If objFile.Name = DateFinal & ".txt" Then
    	objfso.CopyFile objStartFolder & "\" & objFile.Name,"c:\data\destination\" & objFile.Name,True
    End If
Next
 
ShowSubfolders objFSO.GetFolder(objStartFolder)
 
Sub ShowSubFolders(Folder)
    For Each Subfolder in Folder.SubFolders
        Wscript.Echo Subfolder.Path
        Set objFolder = objFSO.GetFolder(Subfolder.Path)
        Set colFiles = objFolder.Files
        For Each objFile in colFiles
            Wscript.Echo objFile.Name
        Next
        Wscript.Echo
        ShowSubFolders Subfolder
    Next
End Sub

Open in new window

0
seaninmanAuthor Commented:
It is a pdf file.  The file name is (Daily Status 5-27-2009) as an example.  Can you make it so the script doesnt have any prompts that require someone to click ok?  I am going to schedule this to run using windows scheduler.
0
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Jared LukerCommented:
try this... I have turned off all the echos and you need to change the objStartFolder and objDestinationFolder to match where you want the script to look and stash.
strComputer = "."
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim SizeCount
 
Const ForAppending = 8
Const ForWriting = 2
Const ForReading = 1
 
WorkingDate = Date
DateFormated = Replace(WorkingDate,"/","-")
DateSplit = Split(DateFormated,"-")
 
If Len(DateSplit(0)) = 2 Then
	DateMonth = DateSplit(0)
Else
	DateMonth = "0" & DateSplit(0)
End If
	
If Len(DateSplit(1)) = 2 Then
	DateDay = DateSplit(1)
Else
	DateDay = "0" & DateSplit(1)
End If
 
DateYear = DateSplit(2)
 
DateFinal = DateMonth & "-" & DateDay & "-" & DateYear
 
'WScript.Echo "Today's Date: " & datefinal
 
 
objStartFolder = "C:\data"
objDestinationFolder = "c:\data\destination"
 
Set objFolder = objFSO.GetFolder(objStartFolder)
'Wscript.Echo objFolder.Path
Set colFiles = objFolder.Files
For Each objFile in colFiles
    'Wscript.Echo objFile.Name
    If objFile.Name = "Daily Status " & DateFinal & ".pdf" Then
    	objfso.CopyFile objStartFolder & "\" & objFile.Name,objDestinationFolder & objFile.Name,True
    End If
Next
 
ShowSubfolders objFSO.GetFolder(objStartFolder)
 
Sub ShowSubFolders(Folder)
    For Each Subfolder in Folder.SubFolders
        'Wscript.Echo Subfolder.Path
        Set objFolder = objFSO.GetFolder(Subfolder.Path)
        Set colFiles = objFolder.Files
        For Each objFile in colFiles
		    'Wscript.Echo objFile.Name
		    If objFile.Name = "Daily Status " & DateFinal & ".pdf" Then
		    	objfso.CopyFile objStartFolder & "\" & objFile.Name,"c:\data\destination\" & objFile.Name,True
		    End If
        Next
        'script.Echo
        ShowSubFolders Subfolder
    Next
End Sub

Open in new window

0
seaninmanAuthor Commented:
I modified the locations and Its not copying the file, and im not getting any error messages.
0
Jared LukerCommented:
There is no 0 in front of the 5 for the month?
0
seaninmanAuthor Commented:
ok i added a 0 in front of the 5 and it created a new file but did not put in the destination folder, and can we make it so the name stays the same, it addes destination to the file name?
0
Jared LukerCommented:
Thanks...

That filename problem was just a bug... try this on for size...
strComputer = "."
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim SizeCount
 
Const ForAppending = 8
Const ForWriting = 2
Const ForReading = 1
 
WorkingDate = Date
DateFormated = Replace(WorkingDate,"/","-")
DateSplit = Split(DateFormated,"-")
 
If Len(DateSplit(0)) = 2 Then
	DateMonth = DateSplit(0)
Else
	DateMonth = "0" & DateSplit(0)
End If
	
If Len(DateSplit(1)) = 2 Then
	DateDay = DateSplit(1)
Else
	DateDay = "0" & DateSplit(1)
End If
 
DateYear = DateSplit(2)
 
DateFinal = DateMonth & "-" & DateDay & "-" & DateYear
 
'WScript.Echo "Today's Date: " & datefinal
 
 
objStartFolder = "C:\data"
objDestinationFolder = "c:\data\destination"
 
Set objFolder = objFSO.GetFolder(objStartFolder)
'Wscript.Echo objFolder.Path
Set colFiles = objFolder.Files
For Each objFile in colFiles
    'script.Echo objFile.Name
    If objFile.Name = "Daily Status " & DateFinal & ".pdf" Then
    	objfso.CopyFile objStartFolder & "\" & objFile.Name,objDestinationFolder & "\" & objFile.Name,True
    End If
Next
 
ShowSubfolders objFSO.GetFolder(objStartFolder)
 
Sub ShowSubFolders(Folder)
    For Each Subfolder in Folder.SubFolders
        'Wscript.Echo Subfolder.Path
        Set objFolder = objFSO.GetFolder(Subfolder.Path)
        Set colFiles = objFolder.Files
        For Each objFile in colFiles
		    'Wscript.Echo objFile.Name
		    If objFile.Name = "Daily Status " & DateFinal & ".pdf" Then
		    'WScript.Echo "file found"
		    	objfso.CopyFile Subfolder.Path & "\" & objFile.Name,objDestinationFolder & "\" & objFile.Name,True
		    End If
        Next
        'Wscript.Echo
        ShowSubFolders Subfolder
    Next
End Sub

Open in new window

0
seaninmanAuthor Commented:
How hard will it be to make it so it doesnt have to have a 0 on a single digit month?  This file is created from a system that will require code change to add the 0.
0
Jared LukerCommented:
I assume it's going to be the same with the day of the month.  Correct me if I'm wrong.

I think this will do it...
strComputer = "."
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim SizeCount
 
Const ForAppending = 8
Const ForWriting = 2
Const ForReading = 1
 
WorkingDate = Date
DateFormated = Replace(WorkingDate,"/","-")
DateSplit = Split(DateFormated,"-")
 
DateMonth = DateSplit(0)
DateDay = DateSplit(1)
DateYear = DateSplit(2)
 
DateFinal = DateMonth & "-" & DateDay & "-" & DateYear
 
'WScript.Echo "Today's Date: " & datefinal
 
 
objStartFolder = "C:\data"
objDestinationFolder = "c:\data\destination"
 
Set objFolder = objFSO.GetFolder(objStartFolder)
'Wscript.Echo objFolder.Path
Set colFiles = objFolder.Files
For Each objFile in colFiles
    'script.Echo objFile.Name
    If objFile.Name = "Daily Status " & DateFinal & ".pdf" Then
    	objfso.CopyFile objStartFolder & "\" & objFile.Name,objDestinationFolder & "\" & objFile.Name,True
    End If
Next
 
ShowSubfolders objFSO.GetFolder(objStartFolder)
 
Sub ShowSubFolders(Folder)
    For Each Subfolder in Folder.SubFolders
        'Wscript.Echo Subfolder.Path
        Set objFolder = objFSO.GetFolder(Subfolder.Path)
        Set colFiles = objFolder.Files
        For Each objFile in colFiles
		    'Wscript.Echo objFile.Name
		    If objFile.Name = "Daily Status " & DateFinal & ".pdf" Then
		    'WScript.Echo "file found"
		    	objfso.CopyFile Subfolder.Path & "\" & objFile.Name,objDestinationFolder & "\" & objFile.Name,True
		    End If
        Next
        'Wscript.Echo
        ShowSubFolders Subfolder
    Next
End Sub

Open in new window

0
seaninmanAuthor Commented:
no your exactly right & this is great.  One last thing I just thought of that would be nice if we could delete the previous days file from the destination folder either before the copy or after it doesnt matter.
0
Jared LukerCommented:
That muddies the waters a bit, but I think this will do it...
strComputer = "."
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim SizeCount
 
Const ForAppending = 8
Const ForWriting = 2
Const ForReading = 1
 
WorkingDate = Date
DateFormated = Replace(WorkingDate,"/","-")
DateSplit = Split(DateFormated,"-")
 
DateMonth = DateSplit(0)
DateDay = DateSplit(1)
DateYear = DateSplit(2)
 
DateFinal = DateMonth & "-" & DateDay & "-" & DateYear
 
Yesterday = Date - 1
YesterdayFormated = Replace(Yesterday,"/","-")
'WScript.Echo YesterdayFormated
YesterdaySplit = Split(YesterdayFormated,"-")
 
YesterdayMonth = YesterdaySplit(0)
YesterdayDay = YesterdaySplit(1)
YesterdayYear = YesterdaySplit(2)
 
YesterdayFinal = YesterdayMonth & "-" & YesterdayDay & "-" & YesterdayYear
'WScript.Echo "yesterday = " & YesterdayFinal
 
 
'WScript.Echo "Today's Date: " & datefinal
 
 
objStartFolder = "C:\data"
objDestinationFolder = "c:\data\destination"
 
Set objFolder = objFSO.GetFolder(objStartFolder)
'Wscript.Echo objFolder.Path
Set colFiles = objFolder.Files
For Each objFile in colFiles
    'Wscript.Echo objFile.Name
    If objFile.Name = "Daily Status " & DateFinal & ".pdf" Then
    	'WScript.Echo "file found"
    	'WScript.Echo objStartFolder & "\" & objFile.Name,objDestinationFolder & "\" & objFile.Name,True
    	objfso.CopyFile objStartFolder & "\" & objFile.Name,objDestinationFolder & "\" & objFile.Name,True
    	If objfso.FileExists (objDestinationFolder & "\Daily Status " & YesterdayFinal & ".pdf") Then
    		'WScript.Echo "old file found"
   			objfso.DeleteFile objDestinationFolder & "\Daily Status " & YesterdayFinal & ".pdf"
   		End If
    End If
Next
 
ShowSubfolders objFSO.GetFolder(objStartFolder)
 
Sub ShowSubFolders(Folder)
    For Each Subfolder in Folder.SubFolders
        'Wscript.Echo Subfolder.Path
        Set objFolder = objFSO.GetFolder(Subfolder.Path)
        Set colFiles = objFolder.Files
        For Each objFile in colFiles
		    'Wscript.Echo objFile.Name
		    If objFile.Name = "Daily Status " & DateFinal & ".pdf" Then
		    'WScript.Echo "file found"
		    	objfso.CopyFile Subfolder.Path & "\" & objFile.Name,objDestinationFolder & "\" & objFile.Name,True
		    	If objfso.FileExists (objDestinationFolder & "\Daily Status " & YesterdayFinal & ".pdf") Then
		    		objfso.DeleteFile objDestinationFolder & "\Daily Status " & YesterdayFinal & ".pdf"
		   		End If
		    End If
        Next
        'Wscript.Echo
        ShowSubFolders Subfolder
    Next
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
seaninmanAuthor Commented:
Perfect!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.