Check for new files at program start - vb.net

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 :)

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.

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

Related

Create an EXE that has files in it

I want to create an Exe that copies files to a directory on the c drive.
So I want it to this.
I have added the files to the project.
And when I run the code. It copies the file.
My.Computer.FileSystem.CreateDirectory("C:\direcotory1")
My.Computer.FileSystem.CopyFile("file1", "c:\direcotry1\file1.dat")
The above code works, but...
After I compile if I move the exe out of the source directory, it doesn't work. I have to leave the exe in the directory where the file is.
Phrased another way. IF the exe is not in the directory it gets compiled into by Visual Studio. Then it cannot find the file to copy.
I know people do this all the time, but I am not sure how to get them in there.
Properties are set to content, or embedded resource don't seem to make a difference.
Can anyone give me the piece I am missing?
Thanks
Dan

VB.NET - Shell batch file vs. Manually opening batch file = different results, for PSFTP.exe

I am using a Script Task in SSIS. So I am writing in vb.net. I have a substantial amount of code that ends up writing two files: one is a .bat file with this EXACT contents in it, minus I've changed hostname, username, and password of course:
C:\Users\ipisors\AppData\Local\Temp\091014121929_Phoenix\psftp.exe -pw PASSWORD -b C:\Users\ipisors\AppData\Local\Temp\091014121929_Phoenix\091014121929.txt USERNAME#HOST
(I'm sure it has this, because I manually check it after the Script Task runs).
The content of the .txt file referenced in the command is this EXACTLY:
cd uploads
cd OUT
mget PhoenixEnrollment_20140910*.txt
bye
MY QUESTION -
After the script task runs successfully, there is no downloaded file inside the directory of PSFTP.EXE (C:\Users\ipisors\AppData\Local\Temp\091014121929_Phoenix). Nothing.
However, if I manually double click on that same batch file that the script task was supposed to SHELL(), it immediately works perfectly. Within a few seconds, there is a downloaded file inside C:\Users\ipisors\AppData\Local\Temp\091014121929_Phoenix
What could possibly be the difference in reaction from the FTP server, between my script task code:
Shell(strBatPhoenix, AppWinStyle.NormalFocus, True)
(which by the way - yes I can actually see it working!)
and double clicking the same da*n file
I've also debugged the script task, it's definition Shelling out the EXACT SAME bat file.
Help please!
I will say the password to the SFTP site has a caret (^) in it, which I am escaping by using a double caret.
Figured it out after additionally testing one other way:
I used the command prompt itself to run my batch file. It ALSO did not download to the expected directory. I began to suspect that, even though the Shell was automating a batch file in the 'desired' folder......that batch file was then reading the 'local' directory as whatever calling application had initially Shelled it out.
In this case: c:\users\myusername. And therein was my file.
Lesson to be learned: Shelling a batch file, even if that batch file shells another file, may end up with one application or another reading your instant directory as the instant directory of the application that did the first shell, not the last shell.

___jb_bak___ and ___jb_old___ files in PyCharm

When I got some PyCharm project from my colleague I saw some backup files of *.py files.
This files have types: *.___jb_old___ and *.___jb_bak___.
I open the files in Notepad++ and see that these are identical backup files of the corresponding *.py files.
I asked my colleague, but he didn't know what these are.
Why are there TWO identical backup files for each *.py file?
How can I tune PyCharm? We want to turn off this backup.
Google gave me nothing :(
You can disable "safe write"
Use "safe write" (save changes to a temporary file first) If this
check box is selected, a changed file will be first saved to a
temporary file; if the save operation is completed successfully, the
original file is deleted, and the temporary file is renamed.
https://www.jetbrains.com/webstorm/help/system-settings.html
i had this problem in webstorm when a script file was running and i was editing it in webstorm. when i stopped the script and edited it everything was fine
it's a temporary file used by PyCharm to make sure you change will not be lost when editing files. it's safe to delete them manually, you will only loss very recent changes. IntelliJ IDEA works the same as PyCharm.
How to delete them?
To delete a file on a file system requires two things: 1)you have the permission. 2)no program is using it.
so make sure you have 'w' the permission, and stop all program which is using it. then you can remove it.
How to know which program is using it?
Normally you should already know it. but sometimes some background programs(like crash plan, google drive sync, e.g.) may also hold it quietly, then find and kill all programs may be very tricky. the easiest way is reboot your computer with 'safe mode', in which only the OS kernel is loaded.
I spend two hours to figure out the reason why I cannot delete the temp file even when I have whole permission. a crash plan service is holding it in background. This may not be your issue, but if you cannot delete the temp file, this will save your time.
While JeremyWeir's solution probably does work, the real fix - imo - is to enable write permission on the directory.
Saving a file would only need write permission to that file itself. But with the "safe write", you need permission to create the file and rename it - which means you need write access to the directory.
In Linux this would be e.g. chmod ug+w DIR, if you want to give write access to user and group.
I have exact same issue with PhpStorm after system crash. The fix I found was to manualy delete *._jb_old_ and *._jb_bak_ files and reinstall PhpStorm

Move folder structure from one computer to another computer when these computers aren't networked?

I have two computers that aren't networked. I need to replicate the folder structure of one drive that exists on one computer and put it on the other computer. Both Windows 7 machines. I don't need the files, just folders/directories. The drive letters are the same on both computers (Y). The computers are miles apart physically, but I do have access to the computer I am trying to get the folder structure of via LogMeIn.
I am thinking I need to save the folder structure to a file using some process. Move that file to my computer (via email or LogMeIn File Manager) and run some process to put in on my computer.
Is there a better solution? If not, is there code out there to do this via VBA, Cmd window, .bat script, VB.NET, or VBS? I know I can write it in VBA, but I'd rather not recreate the wheel if I don't have to.
I don't have a 'process'/program that does this. LogMeIn File Manager doesn't do this either (I asked). There are lots of paths on this drive that I need so creating them by hand would take a long time. I searched and found a lot of solutions that work with computers that are networked, but these computers are not networked, hence why I think I need to save it to a file. Again, I don't want all the files on the drive (its huge and most of the files are unnecessary), just folders.
thanks.
Create a directory listing of the source computer and redirect the output to a text file:
dir /ad /b /s >> dirlist.txt
The switches to dir are (more info at MS TechNet):
/ad List only files with attribute of directory
/b Bare format (no heading information or summary)
/s Displays files in specified directory and all subdirectories
Transfer dirlist.txt from the source computer to the destination computer.
On the destination computer, use a for /F with that text file from the command prompt to create the directory structure:
for /F "delims=" %i in (dirlist.txt) do md "%i"
The switches to for are documented at MS TechNet
You have many options:
Windows command xcopy source destination /T /E. The /T option creates the directory structure, and the /E option includes empty directories.
Bundle the empty directory structure into an installer (perhaps as a zip file).
If the structure isn't relatively small and not likely to change, you could just put a bunch of md commands into a batch file.
Combination - xcopy the structure locally, zip it, transfer it, unzip it.

How can I prevent Visual Studio from locking the xml documentation files in the bin directory?

My visual studio solution includes a web application and a unit test application. My web application uses log4net. I want to be able to use msbuild from the command-line to build my solution. However, whenever I build the solution from the command-line, I get build errors because it can't copy log4net.xml to the test project's bin directory.
The error message is:
"Unable to copy file '\bin\log4net.xml' to 'bin\Debug\log4net.xml'. Access to the path '\bin\log4net.xml' is denied."
It looks like Visual Studio is locking this file, but I can't figure out why it would need to. Is there a way to prevent VS from locking the XML documentation files in a project that it has loaded?
I've found the following solution:
In VS postbuild event or in NAnt/MSbuild script execute the cmd script
handle.exe -p devenv [Path to the folder with locked files] > handles.txt
FOR /F "skip=5 tokens=3,4 delims=: " %%i IN (handles.txt) DO handle -p %%i -c %%j -y
handle.exe is available here http://technet.microsoft.com/en-us/sysinternals/bb896655.aspx
first line of the script dumps to handles.txt all handles for files locked by VS
second line reads handle ids from the file and kills the handles
After the script is executed files may be removed/replaced/moved etc
If you're fine with omitting the xml & pdb files altogether from the output, you can pass /p:AllowedReferenceRelatedFileExtensions=none to msbuild on the command line.
(Thanks to related answer https://stackoverflow.com/a/8757941/251011 )
EDIT: If you also have problems with dll files having this error, I recently discovered an environment variable solution: https://stackoverflow.com/a/23069603/251011
I've had this problem with Visual Studio, too. We use NAnt instead of MSBuild, but the problem is the same. I was able to work around it by modifying the build file to ignore failures when copying xml documentation.
Note that this doesn't actually solve the original problem since the xml files are still locked, but this workaround was good enough for us since the actual content of our xml documentation doesn't change very often.
Krystan wrote:
You could drop this file into another directory and reference it from there or place code that uses it into a library and have the post build event on that copy it to its bin directory and then reference.
Our xml file locking problem is not in the projects bin directory, rather an external reference directory. We hit it when performing TortoiseSVN->Update where a new version is available. Assuming it's because VS is using the file for intellisense.
For those who hit this locking issue due to TortoiseSVN->Update, I'm currently experimenting with a pre-update hook which deletes the offending file(s) before updating (they will be restored if no update is needed), so far this seems to work (which is weird) but I haven't tested it thoroughly enough to say for sure. Will update this answer if it proves reliable.
Here's hoping MS fix it in VS 2010.
Basically don't check files into the bin folder, its a bad idea.
You could drop this file into another directory and reference it from there or place code that uses it into a library and have the post build event on that copy it to its bin directory and then reference.
Msbuild will then copy that to the webprojects bin directory for you :)
We have this exact issue with people checking in stuff to the bin directory, unless you absolutely have to bin directories should either not be checked in at all or just have .refresh files in there to avoid these sorts of locking issues.
Bit late on the reply, sorry :)