Solved

I can copy the newest file. But I cant't Move it.

Posted on 2010-09-08
9
500 Views
Last Modified: 2013-11-26
Hello here is my stumbling block. I have a script that copies the newest file in a folder based on file extention into another folder perfectly. But I am having trouble changing the script so that it moves the newest file. I Tried to Edit the line where objFSO.CopyFile strNewestFilePath, and change it to objFSO.MoveFile but this does not work.  Can some one help me to get it to move the file.
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Define the folder path to find the newest file here:

strSourceFolder = "C:\Test1"

' Define the folder path to find the newest file here:

strTargetFolder = "C:\Test2"

' Define each extension that you want to find the newest file of, separated by a semi-colon

' leave it blank to find the newest file in the whole folder

strExtensions = ".log"



' Make sure the target folder has a trailing slash

If Right(strTargetFolder, 1) <> "\" Then strTargetFolder = strTargetFolder & "\"

If strExtensions = "" Then

	arrExtensions = Array("")

Else

	arrExtensions = Split(strExtensions, ";")

End If

For Each strExt In arrExtensions

	' Here we call the GetNewestFile function, passing the path to the source folder for it to search

	strNewestFilePath = GetNewestFile(strSourceFolder, strExt)

	

	' Now copy the newest file to the target folder

	objFSO.CopyFile strNewestFilePath, strTargetFolder, True



	MsgBox strNewestFilePath & " was copied to " & strTargetFolder

Next



Function GetNewestFile(ByVal sPath,ByVal sExt)



	sNewestFile = Null   ' init value



	Set oFSO = CreateObject("Scripting.FileSystemObject")

	Set oFolder = oFSO.GetFolder(sPath)

	Set oFiles = oFolder.Files



	' enumerate the files in the folder, finding the newest file

	For Each oFile In oFiles

		If sExt = "" Then

			On Error Resume Next

			If IsNull(sNewestFile) Then

				sNewestFile = oFile.Path

				dPrevDate = oFile.DateLastModified

			ElseIf dPrevDate < oFile.DateLastModified Then

				sNewestFile = oFile.Path

				dPrevDate = oFile.DateLastModified

			End If

			On Error Goto 0

		ElseIf Right(LCase(oFile.Name), Len(sExt)) = LCase(sExt) Then

			On Error Resume Next

			If IsNull(sNewestFile) Then

				sNewestFile = oFile.Path

				dPrevDate = oFile.DateLastModified

			ElseIf dPrevDate < oFile.DateLastModified Then

				sNewestFile = oFile.Path

				dPrevDate = oFile.DateLastModified

			End If

			On Error Goto 0			

		End If

	Next



	If IsNull(sNewestFile) Then sNewestFile = ""



	GetNewestFile = sNewestFile



End Function

Open in new window

0
Comment
Question by:doubled911
  • 4
  • 3
  • 2
9 Comments
 
LVL 18

Expert Comment

by:Cluskitt
ID: 33628645
Check to see if you have write permissions on that folder. If you don't, then all you can do is read. It would help if you would post the error as well.
0
 

Author Comment

by:doubled911
ID: 33629070
I have full permissions to my folders and I am the owner. I dont have problems writting the folders.   But here is the error I get
The error I get says
Line: 22
Char: 2
Error: Wrong arguments or invalid property assignment: 'objFSO.MoveFile'
Code: 800A01C2
0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 33630354
Movefile has different arguments:
fso.MoveFile "SourcePath&Filename", "TargetPath"
Or
fso.MoveFile Source:="SourcePath&Filename", Destination:="TargetPath"
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 33632713
This should work fine:
      objFSO.MoveFile strNewestFilePath, strTargetFolder

the last parameter doesn't exist, but it doesn't overwrite either, it doesn't support that.

What you need to is delete it before moving it, by placing this line above that
      If objFSO.FileExists(strTargetFolder & Mid(strNewestFilePath, InStrRev(strNewestFilePath, "\") + 1)) = True Then objFSO.DeleteFile strTargetFolder & Mid(strNewestFilePath, InStrRev(strNewestFilePath, "\") + 1), True

Regards,

Rob.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:doubled911
ID: 33637929
Just wanted to say Thank your for the original code Rob... sir.  I believe I might be placing this in the wrong spot.  I believe the  
If objFSO.FileExists(strTargetFolder & Mid(strNewestFilePath, InStrRev(strNewestFilePath, "\") + 1)) = True Then objFSO.DeleteFile strTargetFolder & Mid(strNewestFilePath, InStrRev(strNewestFilePath, "\") + 1), True
 
is supposed to go back and delete the file after it copies it over???So I copied the upper portion of the code that I believe I should be altering based on how I'm interpreting your advice. The thing is its still copying which is good but the newest file is still in the original source folder Test 1.

Orginal Working Syntax:



Set objFSO = CreateObject("Scripting.FileSystemObject")

' Define the folder path to find the newest file here:

strSourceFolder = "C:\Test1"

' Define the folder path to find the newest file here:

strTargetFolder = "C:\Test2"

' Define each extension that you want to find the newest file of, separated by a semi-colon

' leave it blank to find the newest file in the whole folder

strExtensions = ".log"



' Make sure the target folder has a trailing slash

If Right(strTargetFolder, 1) <> "\" Then strTargetFolder = strTargetFolder & "\"

If strExtensions = "" Then

	arrExtensions = Array("")

Else

	arrExtensions = Split(strExtensions, ";")

End If

For Each strExt In arrExtensions

	' Here we call the GetNewestFile function, passing the path to the source folder for it to search

	strNewestFilePath = GetNewestFile(strSourceFolder, strExt)

	

	' Now copy the newest file to the target folder

	objFSO.CopyFile strNewestFilePath, strTargetFolder, True



My Changes:



Set objFSO = CreateObject("Scripting.FileSystemObject")

' Define the folder path to find the newest file here:

strSourceFolder = "C:\Test1"

' Define the folder path to find the newest file here:

strTargetFolder = "C:\Test2"

' Define each extension that you want to find the newest file of, separated by a semi-colon

' leave it blank to find the newest file in the whole folder

strExtensions = ".log"



' Make sure the target folder has a trailing slash

If objFSO.FileExists(strTargetFolder & Mid(strNewestFilePath, InStrRev(strNewestFilePath, "\") + 1)) = True Then objFSO.DeleteFile strTargetFolder & Mid(strNewestFilePath, InStrRev(strNewestFilePath, "\") + 1), True

If Right(strTargetFolder, 1) <> "\" Then strTargetFolder = strTargetFolder & "\"

If strExtensions = "" Then

	arrExtensions = Array("")

Else

	arrExtensions = Split(strExtensions, ";")

End If

For Each strExt In arrExtensions

	' Here we call the GetNewestFile function, passing the path to the source folder for it to search

	strNewestFilePath = GetNewestFile(strSourceFolder, strExt)

	

	' Now copy the newest file to the target folder

	objFSO.CopyFile strNewestFilePath, strTargetFolder, True

Open in new window

0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 33642439
>> is supposed to go back and delete the file after it copies it over???

No, that's not correct.  Sorry if I was unclear.

There is a CopyFile method, and also a MoveFile method, but the MoveFile method does not support overwriting.  So, you have two options.

1) Use CopyFile to copy the file, overwriting any existing file in the destination, then delete the original file, like so:
      ' Now copy the newest file to the target folder
      objFSO.CopyFile strNewestFilePath, strTargetFolder, True
      If objFSO.FileExists(strNewestFilePath) = True Then objFSO.DeleteFile strNewestFilePath, True

2) Check if the file already exists in the destination, and if so, delete it, then use the MoveFile method to move the file, like so:
      ' Now copy the newest file to the target folder
      If objFSO.FileExists(strTargetFolder & Mid(strNewestFilePath, InStrRev(strNewestFilePath, "\") + 1)) = True Then objFSO.DeleteFile strTargetFolder & Mid(strNewestFilePath, InStrRev(strNewestFilePath, "\") + 1), True
      objFSO.MoveFile strNewestFilePath, strTargetFolder

Either way, those three lines all belong together, and should work if you replace your original lines 21 and 22.

Regards,

Rob.
0
 

Author Comment

by:doubled911
ID: 33645694
OMG!!!! Dude you are pure Genius!!!! LOL Man you just made my day!! Thank you!!!! I feel like I owe you a beer or somthing. LOL!! Thank you sir!!
0
 

Author Closing Comment

by:doubled911
ID: 33645703
Rob is really on his A game really truely knowledgeable.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 33651690
Thanks for the grade.

Glad I could help.

Regards,

Rob.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now