Link to home
Start Free TrialLog in
Avatar of E=mc2
E=mc2Flag for Canada

asked on

Script to copy newest file in folder

I am looking for a script that I can incorporate into an existing script which would allow me to do the following:

2 Folders:
Folder1 - contains new text files that are written to it daily
Folder2 - copies of newest file that arrived in Incoming1 otherwise keep folder blank

When I run the script, I want it to look inside Folder1 and detect the newest most recent text file which has been written inside this folder.   Copy that file to a folder called Folder2.  When I run the script again, if it detects that the newest file in in Folder1 is the same as the file in Folder2, then delete the file in Folder2.  When I run the script yet again, if it notices that the file in Folder1 is still the newest file and has not changed since the last time it was copied into Folder2, and if Folder2 is empty, then do not copy the file in Folder2.  Keep Folder2 empty until when I run the script yet again, if it finds that there is a newer file in Folder 1 then copy it into Folder 2... and so forth.

Summary:
Folder2 should only once contain the newest file found in Folder1, otherwise it should be kept empty, until there is a newer file in Folder1.
Avatar of arnold
arnold
Flag of United States of America image

Some examples and information on vbscript with filesystemobject

http://msdn.microsoft.com/en-us/library/2z9ffy99(v=vs.84).aspx
http://msdn.microsoft.com/en-us/library/6tkce7xa(v=vs.84).aspx

You have to use other tools to make sure the file are identical versus just being similarly named.
On phone at moment but you can do what you need by using a few lines of batch file.  will post you script in a bit if no-one else does first.  Few examples of how to get newest file using dir on http://scripts.dragon-it.co.uk/

dir /b /o-d *.txt will give you filename which we can read.

can I check what you class as the file being the same - does it need to be the same date/time, same contents, same size?  

steve
Try this... not tested yet.  It loops over files in folder1 and picks up the newest filename into variable with it's date time and size.  Then looks for files in the folder2 directory.  If it finds any then if the date/time doesn't match then it deletes the folder2 file and copies the folder1 one.  If the folder2 file is the same it just deletes it.

Now I think you may also want that if the folder2 was the same then don't copy it again (though it isn't there of course to know) so we would have to handle that by having a copy of this latest file somewhere else, can we have a "folder3" for the latest file copied say?

Steve

@echo off
set Folder1=c:\folder 1
Set Folder2=c:\folder 2
Set FileSpec=*.txt

for /f "tokens=*" %%F in ('dir /b /a-d /od "%folder1%\%filespec%"') do (
  set filename=%~nxF
  set datetime=%~tF
  set filesize=%~zF
)
echo The newest file is %%~nxF dated %~tF, size %~zF

for /f "tokens=*" %%F in ('dir /b /a-d "%folder2%\%filespec%"') do (
  echo Found an existing text file
  if not "%filename%%datetime%%filesize%"=="%~nxF%~tF%~zF" (
    del "%folder2%\%filespec%" /q
    copy "%folder1%\%filename%" "%Folder2%"
  ) ELSE (
    del "%folder2%\%filespec%" /q
  )
)

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of E=mc2

ASKER

Thanks dragon-it, I will give it a try. Yes, It's ok to add as many folders as necessary so that we can focus on the content of what is in Folder2.  Looking forward to your solution.
Avatar of E=mc2

ASKER

dragon-it & obDa - thanks.    If upon running the script say a second and third time and the file in Folder2 is the same file which you had previously copied from Folder1 (since that file is still the latest file), then yes of course the contents in Folder2 need to be deleted.  Only should a file be copied to Folder2 if it is the latest file found in Folder1 - only once, that is the first time a newer file appears, then copy the file to Folder2, otherwise every time the script runs, if it has already copied the file into Folder2, then you would need to empty Folder2 - until of course it finds a newer most recent file again in Folder1 where it would copy that newer file into Folder 2.  (If when trying to copy the newer file into Folder2 and it notices that there is a previous file in Folder2, then delete the contents of Folder2 and copy the newest file found in Folder1 into Folder2).

Also to clarify, in Folder1 there are various files, not the same name.  So when we copy to Folder2 it has to be the latest most newest file in Folder1, does not matter about the name of the file.  Never would I copy multiple files into Folder2, but when and if the script allows the copy of the file to go to Folder2 it has to be the latest file which is found in Folder1.  Hope this helps.  Perhaps your scripts reflect this, however I will give them a try.
Avatar of oBdA
oBdA

Yes, my script does that. It remembers the properties of the last file copied in an ini file in the script's folder.
- If the file properties in the ini file match the file properties of the latest file in Folder1, the file in Folder2 will be deleted if it still exists. If the file doesn't exist in Folder2 anymore, no further action will be taken.
- If the latest file in Folder1 differs from the properties in the ini file, Folder2 will be emptied and the file will be copied to Folder2. The ini file will then be updated with the new file properties.

One possible case is not yet covered, though: could there be a situation where
* file1.txt is the latest file in Folder1 (and copied to Folder2)
* then a newer File2.txt is added to Folder1 (and copied to Folder2)
* then only File2.txt is removed from Folder1, so that File1.txt is again the latest file in Folder1
If this situation can come up, what is supposed to happen with File1.txt? Copy it again (because it's now the latest file for the second time), or ignore it because it has already been the latest file before?
In other words: do you need a history of latest files longer than 1, or will the latest file in Folder1 always be the "really" latest file?
Interesting extra twist there...  I could add the extra check I mentioned to my script but go with the other one for the time being it it does what you want.
Steve
Avatar of E=mc2

ASKER

Thanks to both contributors.   To clarify dragon-it's question..  I would like to ensure that the newest file which is copied from Folder1 into Folder2, only gets copied there once.  Then when I run the script again, if there is a newer file in Folder1 it should empty Folder2 and copy the newer file there.

In step 2 in your question, I would never want File2 copied into Folder2 unless Folder2 was emptied out first...since File2 would be the newer file anyway....does this help?
Avatar of E=mc2

ASKER

Folder1 will contain several files.  Only when I run another program is there a possibility that a newer file will exist in Folder1.  Interestingly when I run that program which is triggered by my bat file, it will then do these checks that we are referring to between Folder1 and Folder2.
Yes, I'm aware that Folder1 will contain several files.
The question is if it is possible that the latest file in Folder1 is deleted while there are older files still in Folder1.
In this case the latest of the older files would now again be the latest file in Folder1 and qualify to be copied to Folder2.
Avatar of E=mc2

ASKER

Oh I see what you meant, my apologies.  No there is no possibility that the latest file in Folder1 is deleted while there are older files still in Folder1.

I used your script and it works very well, excellent actually.  Many thanks.
Avatar of E=mc2

ASKER

@oBdA -- In  your script should I disable the setlocal enabledelayedexpansion after the script runs?
No need; that's only valid while the script is running.
Avatar of E=mc2

ASKER

@oBdA - What if I want the script to copy the last files, and not just the latest file (1 file)?
For instance if there were several files that were received since the last time the script ran, I want those to be copied over but not just one file.
Is there any reason behind the filenames, or is date/time all you have to go on?

How about this for logic?

Take the newest file, check it against the last filename processed
Previous file, so on...
record last file processed in text file.
etc.
Avatar of E=mc2

ASKER

The date and time is all I can go on.
So basically if the last time the script ran, it copied the newest file etc.. with a modification to the script I want it to copy over not just the newest file based on date and time, but any newest files based on date and time.

So if for instance file 12345679.txt came in a 4:20 pm, and now I'm running the script again
now at 4:26 pm, and it sees two more files, ie 1546545644.txt at 4:22 pm and 4546456465.txt at 4:24 pm which came in since the last time the script ran, I need those copied over and not just the 4546456465.txt file which would be the newest file - I need both newest files.
easier than dealing wiht the times is if we use the names then, e.g. these are in date order, newest at top.


abc123.txt
defgh.txt
4546.txt
43535.txt

last one copied was 4546.txt so we work down list until hitting 4546.txt.

Off doing something else now for while but can knock something up that will do that if no-one else does.

Steve
Avatar of E=mc2

ASKER

Thanks. File names will always be different.
Ok will pickup tomorrow if no-one follows that up, late now.

Steve