All,
First I apologize for posting another longish similar script, that is very much like my other one above. I've been working with this script to reduce annoyance, and make it more reliable. One of the things I disliked about it, is the start /w bit that starts a secondary script would often remove focus from something else I was working on, right in the middle of typing or something else, and was creating very annoying interruptions in my own workflow. The second thing I disliked was that sometimes the cameras would ftp another [M][0@0][0].jpg file to my system while the script was running but after it has gotten a directory list, and this [M][0@0][0].jpg file would be moved with the others, making a mess of correctly-named and poorly named files in the same output directory.
So this update to the script no longer calls an external batch file that it creates, it instead does the rename and move all within the same script. It also ensures that any extra files that are ftp'd from the cameras to the landing zone are not touched until the next loop. I now run this minimized in the background, and it does it job quietly and leaves me alone to do other work.
Once again, I hope this helps! I have added a lot of REM statements to explain the flow of the batch file and try to explain what is going on, in case you find you want to modify it. Also, to test the script and see if it is doing what you want, rem out the echo off, and echo these other two lines like this:
rem @echo off &setlocal
echo rename !line1! !line!
echo move !line2!\!line! %basedir2%
Then run the script with your own directory and drive modifications, and ctrl-s pause it, scroll up, and make sure the RENAME and MOVE commands are formed the way you want. If the commands are wrong, work with the replacement values in the set commands till you get it the way you want. Once you remove the rem and echo from those lines, the script is "dangerous" again, and will actually rename and move files.
Code: Select all
:endlessloop
@echo off &setlocal
rem File organization script dealing with ftp-named/created files/directories from an AMCREST IPM-721 camera
rem First rename all the files from basedir1 so they include the subdirectory path in the filename
rem Example:
rem rename F:\BACKUPS\kahtrynnyawcam\AMCAM2\2016-11-04\001\jpg\10\28\46[M][0@0][0].jpg AMCAM2-2016-11-04-001-10-28-46M.jpg
rem rename F:\BACKUPS\kahtrynnyawcam\AMCAM2\2016-11-04\001\jpg\10\28\46[R][0@0][0].jpg AMCAM2-2016-11-04-001-10-28-46R.jpg
rem Second move the files to the storage directory basedir2 (best if this is not the same as basedir1)
rem move F:\BACKUPS\kahtrynnyawcam\AMCAM2\2016-11-04\001\jpg\10\28\AMCAM2-2016-11-04-001-10-28-46R.jpg F:\BACKUPS\kahtrynnyawcampictures\
rem Then move all of the AM*.jpg and CA*.jpg files from basedir (and it's subdirectories) to basedir2 (any remaining amcam or cam files)
rem Finally, delete any empty directories under basedir, wait 60 seconds, and do it again
rem Note: my cameras are named AMCAM1, AMCAM2, etc. and the ftp filenames created always start with the camera name (thus: AM*.jpg)
rem My yawcam files are named starting with CAM. (thus: CA*.jpg)
rem set environment
set "basedrive=F:" ;rem drive where your ftp landing zone is that cameras ftp to
set "basedir=F:\BACKUPS\kahtrynnyawcam\" ;rem full path directory of ftp landing zone
set "basedir2=F:\BACKUPS\kahtrynnyawcampictures\" ;rem full path of storage directory to move renamed jpg files to
set "search=\" ;rem first search term, a backslash
set "search1=F:\BACKUPS\kahtrynnyawcam\" ;rem second search term, path to ftp landing zone
set "search2=[M][0@0][0]" ;rem third search term, motion-name given to files by amcam
set "search3=[R][0@0][0]" ;rem fourth search term, regular name given to files by amcam
set "search4=-jpg-" ;rem fifth search term, a directory level name created by amcam
set "replace=-" ;rem first replace term, a dash
set "replace1=" ;rem second replace term, null
set "replace2=M" ;rem third replace term, letter M for motion
set "replace3=R" ;rem fourth replace term, letter R for regular
set "textfile=#jpegfiles.txt" ;rem a temp filename to store a directory output to
rem switch to base drive (i.e. F:)
%basedrive%
rem switch to base directory where ftp files are uploade (i.e. F:\BACKUPS\kahtrynnyawcam)
cd %basedir%
rem list all JPG filenames and their full paths into textfile
dir /s/b *.jpg > %textfile%
rem loop through textfile dir listing doing finds and replaces, form and execute the rename and move commands
(for /f "delims=" %%i in (%textfile%) do (
set "line=%%i" ;rem line equals the line from text file, to do find and replace on
set "line1=%%i" ;rem line1 also equals line from text file
set "line2=%%~dpi" ;rem get path in line2 with tilde dp drivepath
setlocal enabledelayedexpansion
set "line=!line:%search1%=%replace1%!" ;rem replace path with blank to delete path
set "line=!line:%search2%=%replace2%!" ;rem replace motion name with letter M
set "line=!line:%search3%=%replace3%!" ;rem replace regular name with letter R
set "line=!line:%search%=%replace%!" ;rem replace backslashes with dashes
set "line=!line:%search4%=%replace%!" ;rem replace -jpg- with blank to delete it
rem execute the rename command on the jpg file selected
rename !line1! !line!
rem move the newly named jpg file to basedir2
move !line2!\!line! %basedir2%
endlocal
))
rem temporary textfile no longer needed, delete it
del %textfile%
rem Move all AM*.jpg and CA*jpg files in basedir to basedir2, and with their new names they'll sort nicely
for /r %basedir% %%i in (AM*.jpg) do @move "%%i" "%basedir2%"
for /r %basedir% %%i in (CA*.jpg) do @move "%%i" "%basedir2%"
rem Delete all empty subfolders (ones with new files wont be empty so wont be deleted)
for /d %%i in (%basedir%*) do @rmdir /s /q "%%i"
rem pause 60 seconds and then scan again for jpg files to rename and move
timeout 60
goto endlessloop
Thanks and best regards,
Zistrosk