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.
100questionsAsked:
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.

arnoldCommented:
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.
0
Steve KnightIT ConsultancyCommented:
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
0
Steve KnightIT ConsultancyCommented:
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

0
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

oBdACommented:
Try this:
@echo off
setlocal enabledelayedexpansion
set SourceMask=*.txt
set SourceFolder=C:\Temp\Folder1
set TargetFolder=C:\Temp\Folder2
set IniFile=%~dpn0.ini
set LatestFile_Name=
set LatestFile_Size=
set LatestFile_Name=
for /f "delims=" %%a in ('dir /a:-d /b /o:-d "%SourceFolder%\%SourceMask%" 2^>NUL') do (
	set LatestFile_Name=%%~nxa
	set LatestFile_Size=%%~za
	set LatestFile_Date=%%~ta
	goto Break_LatestFile
)
:Break_LatestFile
if "%LatestFile_Name%"=="" (
	echo No files matching '%SourceMask%' found in '%SourceFolder%'.
	goto Leave
)
if exist "%IniFile%" (
	for /f "tokens=1* delims==" %%a in ('type "%IniFile%"') do (set %%a=%%b)
) else (
	set CopiedFile_Name=
	set CopiedFile_Size=
	set CopiedFile_Date=
)
if "%LatestFile_Name%_%LatestFile_Size%_%LatestFile_Date%"=="%CopiedFile_Name%_%CopiedFile_Size%_%CopiedFile_Date%" (
	echo File '%CopiedFile_Name%' has already been copied to '%TargetFolder%'.
	if exist "%TargetFolder%\%LatestFile_Name%" (
		del "%TargetFolder%\%LatestFile_Name%"
		echo Deleted file '%CopiedFile_Name%' from '%TargetFolder%'.
	)
	goto Leave
)
if exist "%TargetFolder%\%SourceMask%" del /q "%TargetFolder%\%SourceMask%"
copy "%SourceFolder%\%LatestFile_Name%" "%TargetFolder%" >NUL
echo Copied file '%LatestFile_Name%' to '%TargetFolder%'.
if exist "%IniFile%" del "%IniFile%"
for %%a in (Name Size Date) do (
	>>"%IniFile%" echo CopiedFile_%%a=!LatestFile_%%a!
)
:Leave
pause

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
100questionsAuthor Commented:
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.
0
100questionsAuthor Commented:
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.
0
oBdACommented:
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?
0
Steve KnightIT ConsultancyCommented:
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
0
100questionsAuthor Commented:
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?
0
100questionsAuthor Commented:
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.
0
oBdACommented:
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.
0
100questionsAuthor Commented:
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.
0
100questionsAuthor Commented:
@oBdA -- In  your script should I disable the setlocal enabledelayedexpansion after the script runs?
0
oBdACommented:
No need; that's only valid while the script is running.
0
100questionsAuthor Commented:
@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.
0
Steve KnightIT ConsultancyCommented:
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.
0
100questionsAuthor Commented:
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.
0
Steve KnightIT ConsultancyCommented:
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
0
100questionsAuthor Commented:
Thanks. File names will always be different.
0
Steve KnightIT ConsultancyCommented:
Ok will pickup tomorrow if no-one follows that up, late now.

Steve
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
Microsoft Legacy OS

From novice to tech pro — start learning today.