Jump to content

VBScript - Move files to another folder


sverkot

Recommended Posts

Hi,I have written a vbscript program to move files that are older than 30 days from a folder and its subfolders to another folder (without keeping the directory structure). However, if a file has the same name as one in its subfolders, it exits saying "File already exists". (Since a folder cannot have two files of the same name ) My question is, is there a way to move files and overwrite if it already exists? Or is there any other work around?

For Each objFile in colFiles            fileModifyDate =objFile.DateLastModified	daysSinceModify = datediff("d", fileModifyDate, date)	'Move file only if it is older than modify date specified	If daysSinceModify > 30 and objFSO.FileExists(objFile) Then    		objFSO.MoveFile objFile, objDestFolder    	End If                	message = objFile.path & "  ," & objFile.name & "    ," &  fileModifyDate                	writeLog logFile, message                	objFSO.MoveFile objFile, objDestFolder    				End If        Next

Link to comment
Share on other sites

Solved it by deleting the file if it already exists in the destination folder. Either way it's gonna get deleted at some point- so why not a bit early! If Not objFSO.FileExists(objFile) Then objFSO.MoveFile objFile, objDestFolderElse objFSO.DeleteFile objFileEnd IfRewrite would have been a less brutal option - but I dont think it's possbile.Thanks,Sang

Link to comment
Share on other sites

  • 3 years later...

I don't know the code to rewrite files either. I'm sure it's POSSIBLE, but the code might look like something from the Matrix. Is this code for a forum or are you looking for table or excel help? Your solution is workable, and that's all that matters in this case.

Link to comment
Share on other sites

Head:FYI: we do not usually respond to threads where the last post is more than a week old, unless the OP has a very good reason for bringing it back to life. This thread is almost FOUR years old. No need for that.We're glad to have you here, but please keep it useful. :)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...