The archive of the snapshot is crazy

Have some questions or having issues with your IP Camera(s), Post them here for the mods and other users to assist you with.
zistrosk
Posts: 2
Joined: Fri Nov 04, 2016 10:44 am

Re: The archive of the snapshot is crazy

Post by zistrosk »

Hi everyone,

I had the same issue with my IPM-721 camera. All of those ftp'd jpg pictures are buried under tons of directories. I'm posting up the code I came up with, in case it fits anyone's needs better than the other awesome solutions already suggested here. This is a single windows batch file that you run in the root ftp landing zone where the AMC* directory is created by the camera when it does it's FTP. It takes the path the camera creates, and uses parts of it, like the date and time, to make a rename command to create a new filename for the JPG file. Then, like the other examples here, it flattens the directory structure created by the camera, moving all the JPG pictures to the root ftp directory. I named it "#MOVEFLAT.BAT" but you can call it anything you want.

Code: Select all


@echo off &setlocal
rem For dealing with ftp named files/directories from an AMCREST IPM-721 camera
rem First rename all the files so they include the subdirectory path in the filename
rem Example:
rem rename F:\BACKUPS\kahtrynnyawcam\AMC001YX_48Y180\2016-11-04\001\jpg\10\28\46[M][0@0][0].jpg AMC001YX_48Y180-2016-11-04-001-10-28-46M.jpg

rem set environment
set "basedrive=F:"
set "basedir=F:\BACKUPS\kahtrynnyawcam\"
set "search=\"
set "search1=F:\BACKUPS\kahtrynnyawcam\"
set "search2=[M][0@0][0]"
set "search3=-jpg-"
set "replace=-"
set "replace1="
set "replace2=M"
set "textfile=#rename.bat"
set "newfile=#rename1.bat"

rem switch to base drive (i.e. F:)
%basedrive%

rem switch to base directory where ftp files are uploaded (i.e. F:\BACKUPS\kahtrynnyawcam)
cd %basedir%

rem list all JPG filenames and their full paths into #rename.bat
dir /s/b *.jpg > #rename.bat

rem loop through doing find and replaces and form the rename commands
(for /f "delims=" %%i in (%textfile%) do (
set "line=%%i"
set "line1=%%i"
setlocal enabledelayedexpansion
set "line=!line:%search1%=%replace1%!"
set "line=!line:%search2%=%replace2%!"
set "line=!line:%search%=%replace%!"
set "line=!line:%search3%=%replace%!"
echo(rename !line1! !line!
endlocal
))>"%newfile%"
echo exit >> %newfile%
del %textfile%
rename %newfile% %textfile%

rem execute the rename batch script just created so filenames get their path info in them
start /w #rename.bat
del #rename.bat

rem Move all files in subfolders to this folder, and with their new names they'll sort nicely
for /r %basedir% %%i in (*) do @move "%%i" "%basedir%"
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"
exit
If you want to test this without changing anything, put it in the ftp landing zone base directory and remove everything after and including the "start /w" command. Then you can inspect the #rename.bat file and make sure its going to rename the files you want, the way you want, before actually changing anything.

Hope this helps!

Zistrosk
stanton
Posts: 3
Joined: Sun Mar 20, 2016 9:56 am

Re: The archive of the snapshot is crazy

Post by stanton »

Thanks guys. I used Zistrosk's script and it worked fine. I did like the ability and suggestion to check the intermediate batch file to ensure it was going to do the right thing!

I still think the forced directory structure is ususable and unacceptable. I too have stopped buying the cameras. I haven't been following all the other discussions, but is anyone finding the default directory structure (and lack of discrete file naming) useful? There seems to be no software that utilizes it, and it's a nightmare to find an event if you don't know when it happened. Seems like such an easy firmware fix.

Using the script keeps me from selling the camera, but I won't be buying any more.
zistrosk
Posts: 2
Joined: Fri Nov 04, 2016 10:44 am

Re: The archive of the snapshot is crazy

Post by zistrosk »

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
masymas
Posts: 3
Joined: Wed Jan 02, 2019 2:21 am

Re: The archive of the snapshot is crazy

Post by masymas »

I used McJohns script using renamer and it worked. It seems like every time I get a new camera, there is a new file and directory format. Using this, I can just set the filenames to the modification date which is what I need.
NotAkihabara
Posts: 1
Joined: Sat Jun 04, 2022 10:55 pm

The archive of the snapshot is crazy - Linux/BSD/OSX

Post by NotAkihabara »

If you need to flatten the files into a single directory in Unix/Linux/OSX, save the following as flatten.sh:

Code: Select all

#!/bin/sh
{
        cd "$1"

        find -type f -iname '*.jpg' | awk '
                BEGIN { FS = "/" } ; {
                        mvcmd = sprintf("mv %s ./%s:%s:%s", $0, $4, $5, $6)
                        print mvcmd
                        system(mvcmd)
                }
        '
}
Make the script executable (only need to do this once):

Code: Select all

chmod ug+x ./flatten.sh
To flatten a directory, pass it in as the first parameter to the script:

Code: Select all

./flatten.sh ./ftp/cam1/2022-06-05
They might have changed something since I wrote this, or your amcrest camera may be different from mine, so try it out on a copy for the first run and make sure it works!
Post Reply