November 02, 2017

Batch file to check for new or changed files in a folder (including subfolders)

I created this batch file to check for new or changed files in a folder including subfolders.

  • It uses Robocopy to build a local representation of the files and directory structure, but all files are 0-byte files for speed and storage efficiency
  • It shows results on the screen, and adds everything to a log file for later perusal. 
  • This example contains 4 blocks of the checker code, and only the first is set up to check one folder (my desktop), but you may add more as you wish (my real one monitors 4 folders). 
  • I use Task Scheduler to run it every day; that could be less or more often as you wish.
The code is below, and is somewhat ::commented for your ease of understanding; you can place all this into a text file, change the things that need to be changed, rename the file to anything.bat, and there's your batch file.  Beware of line-wrapping from Blogger here. I'll number the lines so you know when that happens, please remove before use.

1. ::Written by Jeff Gillman
2. @echo off
3. MODE CON COLS=172 LINES=50
4. color 3e

5. ::define the log file
6. set log=C:\Users\[username]\filechecker\checker.log
7. ::write a date/timestamp to the log file
8. date /t >>%log%
9. time /t >>%log%


10. ::text that will be displayed at the beginning 
11. echo.
12. echo FileChecker: checks for new/udpated/deleted files in specific folders
13. echo    New = new file
14. echo    Newer = file updated since previous scan
15. echo    *EXTRA = file deleted at source
16. echo.
17. echo.


18. ::block 1 (first folder)
19. set source=c:\users\[username]\desktop
20. set target=c:\users\[username]\filechecker\desktop
21. echo.
22. echo ........... Checking for new files in %source%
23. robocopy %source% %target% /s /e /create /njh /njs /l /ndl /ns /purge | find /v "    Changed"
24. robocopy %source% %target% /s /e /create /njh /njs /ndl /ns /purge | find /v "    Changed" >>%log%

25. ::color change to help user detect progress
26. color 4b

27. set source=\\[server1]\[folder1]
28. set target=c:\users\[username]\filechecker\[folder1]
29. echo.
30. echo ........... Checking for new files in %source%
31. robocopy %source% %target% /s /e /create /njh /njs /l /ndl /ns /purge | find /v "    Changed"
32. robocopy %source% %target% /s /e /create /njh /njs /ndl /ns /purge | find /v "    Changed" >>%log%

33. color e5

34. ::block 2 (second folder)
35. set source=\\[server2]\[folder2]
36. set target=c:\users\[username]\filechecker\[folder2]
37. echo.
38. echo ........... Checking for new files in %source%
39. robocopy %source% %target% /s /e /create /njh /njs /l /ndl /ns /purge | find /v "    Changed"
40. robocopy %source% %target% /s /e /create /njh /njs /ndl /ns /purge | find /v "    Changed" >>%log%

41. color f9

42. ::block 3 (third folder)
43. set source=\\[server2]\\[folder3][folder3]
44. echo.
45. echo ........... Checking for new files in %source%
46. robocopy %source% %target% /s /e /create /njh /njs /l /ndl /ns /purge | find /v "    Changed"
47. robocopy %source% %target% /s /e /create /njh /njs /ndl /ns /purge | find /v "    Changed" >>%log%


48. ::final display of info
49. echo.
50. color 4f
51. echo \
52. echo \\
53. echo \\\ The above is also logged to %log%
54. ::reset variables to null
55. set log=
56. set source=
57. set target=
58. echo.
59. echo Press any key to exit.
60. ::wait for keypress, hide text because we're using custom text above
61. pause>nul

62. exit