Benguino
Benguino

Reputation: 67

Check for new files at program start

I need to write a .dll for a program that works with some files.

I need to code that every time someone starts the program, it should check in a folder for new files and if there are new ones, it should copy them automatically in the programs folder and restart the program. If there are none the program should just start normally.

My problem is that I dont know how to make the program identify new files since the last time the program was started/closed, because I dont want to monitor the folder the whole time the program is running, but only at the start of the program.

I thought about comparing the files in the 2 folders, but with houndreds of files it would be bad perfomance wise, wouldnt it ?

Besides that if someone knows how to automatically copy files and could give me an idea for it too, that would be great.

Iam very new to vb.net and only have limited experience with coding, so forgive me if I ask some easy questions.

Every idea and answer is much appreciated. Thank you!

UPDATE: Thank you very much guys for your ideas, gonna test some of these :)

Upvotes: 0

Views: 112

Answers (2)

Aethan
Aethan

Reputation: 1985

You could try this approach.

I use this approach in backing-up my files into our server.

This will check for new files and newer-dated version of an existing file (if any) then overwrites it.

I used the XCopy command in my batch file

Creating batch files is pretty much basic.

  • Open a Notepad
  • Paste this command: XCopy "C:\YourSourceFolder\*.*" "D:\YourDestinationFolder" /D /S /Y
  • Save the file having an extension of .bat

Explanation regarding XCopy:

  • *.* specifies that all the files inside your directory will be copied.
  • /D means it copies only those files whose source time is newer than the destination time.
  • /S will copy directories and sub directories except empty ones.
  • /Y overwrites existing files without prompting

Source: MS-DOS XCopy Command

Now, running it through VB.Net takes only a one line of code. You could put it in a Button_Click,Timer_tick or any events you prefer.

System.Diagnostics.Process.Start("C:\YourBatFile.bat")

Of course, the directory still varies on where did you saved your .bat file.

Source: Run a batch file in VB.Net

In your case: You could use a timer that executes this batch file in an interval of your preference.

Upvotes: 1

Marcel Hoekstra
Marcel Hoekstra

Reputation: 1488

You can create a windows service and use a filewatcher https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher(v=vs.110).aspx

Upvotes: 0

Related Questions