Author Topic: moving files based on partial file name and date criteria from massive directory  (Read 839 times)


  • Newbie
  • *
  • Posts: 4
I have a NAS that is getting images dumped to it from 4 process monitoring cameras.

The images are named after each camera, such as CL100001, CL200021, CL3000011  etc., with CL1, CL2, CL3 being camera name.

I want to be able to identify files in the source directory by name, them move them to a folder called "\CAMERANAME\MMDDYY" (i.e. \CL1\051918) based on the camera name that generated the image and the images timestamp.

The folder will have multiple-hundreds of files daily so I want to be able to identify the files in the directory and move them (based on camera name and creation date) as efficiently as possible in one run per day.

The logic is simple enough, so I am looking for ideas on how to handle this process with WinBatch functions.

Any suggestions?


  • Pundit
  • *****
  • Posts: 988
    • Data & Stuff Inc.
I would recommend going to the Help file and reading down the list of Filexxxx() functions.   The function names are functionally descriptive so should reasonably obvious which ones you might want to use.  FileInfoToArray() might be useful as a recommendation but there are other ways to do the same thing.

Looping Function names such as For/Next and While are obvious as well.

First, probably have done this but if not, I would see if your camera configuration would allow you to set directory names.  Obviously there is something like that since they are writing to a directory already. 

Sorry if I am not being as pointed as you might want but many ways to do what you want and the WinBatch Function names are normally descriptive enough to find what you need and they all include examples, hence my suggestion.



  • Newbie
  • *
  • Posts: 4
I haven't found any functions that do what I want directly.
I tried FileItemize which created a variable with a string of filenames tab deliminted, but I was having a hard time getting other string parsing commands to recognize the tabs.
Plus, I am not sure how that string will work with 1000 or 10000 files in a dir.

So I was thinking about using the DOS command dir *.* /b >file.txt to create a list and then reading through it in a loop to process files in directory.



  • Pundit
  • *****
  • Posts: 988
    • Data & Stuff Inc.

To deal with files in batch (after reading things again)....

Use FileMove() with wildcards.   It sounds like this would be the best option since your criteria is very simple.

To deal with files individually...

FileItemize() and FileTimeGetEX() is one option.  You could use ItemExtract() in a For/Next Loop to read the file names.

FileInfoToArray() would be a way to get Name and Time info in one request and then use a For/Next Loop to walk through the Array.   

FileMove should do the Move for you. 



  • Newbie
  • *
  • Posts: 32
I don't know if you've solved this yet but it peaked my interest... I was working with file dates and deletions recently and popped this out as a proof of concept. It may or may not work in your setting but it worked in my sandbox.

Just my two cents...

Code: [Select]
; Load Appropriate Extender

Source = "D:\RAWCameras\"
Cam1  = "CL1"
Cam2  = "CL2"
Cam3  = "CL3"
Cam01 = "C:\Cameras\%Cam1%\"
Cam02 = "C:\Cameras\%Cam2%\"
Cam03 = "C:\Cameras\%Cam3%\"
FileMoveFlag = "4"  ;See aFileMove for flags
Moved = "0"

Dirchange (Source)

MyFiles = FileItemize ("*.*")
FileNum = ItemCount (myfiles, @tab)
For FileIndex=1 to FileNum
      File = ItemExtract(FileIndex, MyFiles, @tab)
;     GetDate = FileTimeGetEx(file, 1) = CREATED DATE 
;     GetDate = FileTimeGetEx(file, 2) = MODIFIED DATE
;     GetDate = FileTimeGetEx(file, 3) = LAST ACCESS DATE
      GetDate = FileTimeGetEx(File, 2)
      First3 = StrSub (File, 1, 3)
      Year = StrFixLeft (ItemExtract (1,getdate,":")," ",2)
      Extracted = StrCat (ItemExtract (2,getdate,":"),ItemExtract (3,getdate,":"), Year)
      If First3 == Cam1 Then TargetDir = StrCat (Cam01,Extracted)
      If First3 == Cam2 Then TargetDir = StrCat (Cam02,Extracted)
      If First3 == Cam3 Then TargetDir = StrCat (Cam03,Extracted)

      Message("File Info", "Name - %file%%@CRLF%First 3 - %First3%%@CRLF%Date - %getdate%%@CRLF%Extracted date - %Extracted%%@CRLF%Target - %TargetDir%\")
      If DirExist(TargetDir) == @FALSE Then DirMake(TargetDir) 
      aFileMove (file,TargetDir,FileMoveFlag)
      Moved = Moved + 1


Message ("Ended","%Moved% files moved.")


  • Newbie
  • *
  • Posts: 4
Thanks for all the input...

Here's where I ended up...

a = FileItemize("e:\images\*.*")

numberfiles = ItemCount(a, @TAB)

For Start = 1 to numberfiles

FileName = ItemExtract(Start,a,@TAB)
FileNPath = StrCat(SourceDir, "\", FileName)
CameraName = StrSub(FileName,1,3)
CameraDir = StrCat("%SourceDir%", "\", "%CameraName%")
If DirExist (CameraDir) == @FALSE THEN DirMake(CameraDir)

CreateDateDetail = FileTimeGetEx(FileNPath,1)
ImageYear = StrSub(CreateDateDetail,1,4)
ImageMonth = StrSub(CreateDateDetail,6,2)
ImageDay = StrSub(CreateDateDetail,9,2)
TimeStamp = StrCat(ImageYear, ImageMonth, ImageDay)
TargetDir = StrCat(CameraDir, "\", TimeStamp)
If DirExist (TargetDir) == @FALSE THEN DirMake(TargetDir)

FileMove (FileNPath,TargetDir,@FALSE)

Next Start