Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBS MoveFile results in error 1

Status
Not open for further replies.

cwsstins

MIS
Aug 10, 2004
412
0
0
US
Trying to write a script to move files with last month's date in the filename to an archive folder that exists in the same folder. For instance:

C:\_Temp_Delete\Daily Reports\Report 1 folder --> this is where the file initially resides
C:\_Temp_Delete\Daily Reports\Report 1 folder\Archive-2013\2013-01 --> this is where i want to put it

I'm getting a Path not Found error on the MoveFile command:
FSO.MoveFile Subfolder & "\" & File.Name, ArchDir

If I change that line like so, I get a File Already Exists error (but it doesn't exist in the archive folder):
FSO.MoveFile Subfolder & "\" & File.Name, Subfolder & "\" & ArchDir

Help would be greatly appreciated.

Code:
Dim Folder
Dim File

'MainPath = "\\cpo\shares\common\daily reports"
MainPath = "C:\_Temp_Delete\Daily Reports"
ArchDir  = "Archive-" & Year(Date) & "\" & Year(Date) & "-" & Right("0" & Month(Date)-1,2)

'msgbox ArchDir

Set FSO = CreateObject("Scripting.FileSystemObject") 
Set Folder = FSO.GetFolder(MainPath)

For Each Subfolder in Folder.Subfolders   

	If Subfolder.ParentFolder <> "Archive-" & Year(Date) Then 
		
		For Each File in Subfolder.Files

		If (inStr(File.Name, Year(Date) & Right("0" & Month(Date)-1,2))) Then
		
'			msgbox Subfolder & "\" & File.Name & ", " & ArchDir
			FSO.MoveFile Subfolder & "\" & File.Name, ArchDir

		End If

		Next 
	
	End If

	Next
 
I'm not sure I understand exactly what you're asking for but you can try this:
Code:
Dim Folder
Dim File

'MainPath = "\\cpo\shares\common\daily reports"
MainPath = "C:\_Temp_Delete\Daily Reports"


Set FSO = CreateObject("Scripting.FileSystemObject") 
Set Folder = FSO.GetFolder(MainPath)

For Each Subfolder in Folder.Subfolders   

	If Subfolder.ParentFolder <> "Archive-" & Year(Date) Then 
		
		For Each File in Subfolder.Files

		If (inStr(File.Name, Year(Date) & Right("0" & Month(Date)-1,2))) Then
			ArchDir  = MainPath & "\" & SubFolder.Name & "\Archive-" & Year(Date) & "\"
			ArchDirDay=ArchDir & Year(Date) & "-" & Right("0" & Month(Date)-1,2) & "\"
'			msgbox Subfolder & "\" & File.Name & ", " & ArchDir
			If Not FSO.FolderExists(ArchDir) Then
				FSO.CreateFolder(ArchDir)
				If Not FSO.FolderExists(ArchDirDay) Then
					FSO.CreateFolder(ArchDirDay)
				End If
			End If
			FSO.MoveFile Subfolder & "\" & File.Name, ArchDirDay

		End If

		Next 
	
	End If

	Next
WScript.Echo "Script Complete"
 
One more question...I was testing this locally and it worked. When I changed the MainPath to a network share instead of hard drive, it doesn't work. Is that something to do with FileSystemObject and if so, how do I correct?

Instead of this:
MainPath = "C:\_Temp_Delete\Daily Reports"

I need this:
MainPath = "\\cpo\shares\common\daily reports"

 
Not sure. It works for me as a share just fine. You could try mapping to the share first with MapNetworkDrive.
 
UNC paths should work the same as mapped or local drives. Do you get an error? Also, check you rights on the share.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top