News:

Function Finder  Find all the functions within source code files

Main Menu

Scan and Backup Media

Started by kevin, April 09, 2014, 01:55:32 AM

Previous topic - Next topic

kevin

 Scan and Backup Media (Work In Progress)

     It's become necessary to quickly knock together a tool that will run through a source folder and copy out the required file types to a target folder.   I've written a number of these over the years, so yeah it's another 'slapped' together cut'n'pasted thing :)   But if you're in a spot where you need something to do a quick copy of your files, then this could be a easy place to start.  


     Edit #1
 
        So far the tool scan through the C: drive and builds an array of the all the legal file names.   The next step it to trim those files down to a list of names that we're interested in.     In this case we're looking for media files, but you could set the extension to anything you want..  

     Edit #2

         The initial file scanning and filtering is done, just adding the copy file routines now (well after I have something to eat :) ).



     Edit #3

         Added the file copying as well as report generation.  The report just contains a list of all the files that it copied out. 

    Tested with PlayBASIC V1.64L  (Learning Edition)   (Download PlayBASIC)




; PROJECT : Scan-System-Backup-Media
; AUTHOR  : Kevin Picone
; CREATED : 4/9/2014
; EDITED  : 4/9/2014
; ---------------------------------------------------------------------


; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------
; ------------------------ { PATHS & EXTENSIONS ]---------------------------------
; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------

; list of file type the program should back up  (ADD THE FILE TYPES YOU CARE ABOUT HERE)
BackupTheseFileTypes$="|.jpeg|.jpg|.bmp|.png|.gif|.mov|.mp3"

; src folder  (Here i'm scanning my system folder
SrcPath$ ="c:\"

; dest folder, the location files will be copied into.  
DestPath$ ="e:\_BACKUPS\"


; function to copy the media files from the source to destination device
BackUp_Files(SrcPAth$,DestPath$,BackupTheseFileTypes$)

; refresh the screen and wait for a key input before ending program
Sync
waitkey

; end program now
end

; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------
; --------------------------- { BACK UP FUNCTION ]--------------------------------
; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------


Function BackUp_Files(SrcPAth$,DestPath$,WantedExtensions$)

w=GetDeskTopWidth()
h=GetDeskTopHeight()

Ratio#=float(h)/w

winWidth=w*0.8
WinHeight=WinWidth*Ratio#

OpenScreen WinWidth,WinHeight,32,1



; load a nicer font on start up
LoadFont "veranda",1, 32,0



Print "STAGE#1: Scanning Source Folders (By Warned this is stage can be SLOW as HELL)"
Sync
wait 1000

Dim SrcFileList$(0)
FileCount=GetSelectedFilesFromDir("Refresh_Scanning",SrcFileList$(),Srcpath$,true)

print ""
Print "STAGE#2: Screening Files By File Extension"
print WantedExtensions$
print "Total Number of Files Found #"+Str$(FileCount)
Sync

FileCount=TrimFileList$(SrcFilelist$() , WantedExtensions$)
print "Number of Wanted Files Found #"+Str$(FileCount)

ApproximateSizeInK=COmpute_Total_File_Size_In_K(SrcFilelist$())

print "Approx Total File Size In KiloBytes #"+Str$(ApproximateSizeInK)+"  Megs #"+str$(ApproximateSizeInK/1024)
print ""

Print "STAGE #3: Copying Files"
Sync
wait 1000
CopyFileList$(SrcFilelist$() , SrcPath$,DestPAth$,WantedExtensions$)

print "done"
sync


EndFunction




; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------
; --------------------------- { REFRESH FUNCTIONS ]-------------------------------------
; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------


Function Refresh_Scanning(Files,StartTime)

cls

print "Scanning Source Drive:"
print "Current Files Count #"+Str$(Files)
print "Current Scan Time: "+Str$( (timer()-startTime)/1000)+" Seconds"

sync
wait 100
EndFunction


; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------
; ---------------------- { COPY FILE LIST FUNCTIONS ]----------------------------
; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------


Function  CopyFileList$(Filelist$() , SrcPath$,DestPAth$, Wantedextensions$)


FileCount=getarrayelements(filelist$())

cr$ =chr$(13)+chr$(10)
Row$ =make$("----",50)

LogFile$+=row$+cr$
LogFile$+="[Backup Info]"+cr$
LogFile$+=row$+cr$

LogFile$+="     Files:"+str$(filecount)+cr$
LogFile$+="      Date:"+currentdate$()+cr$
LogFile$+="  Src Path:"+srcpath$+Cr$
LogFile$+=" Dest Path:"+destpath$+Cr$
LogFile$+="Extensions:"+Wantedextensions$+cr$
LogFile$+=cr$+cr$


; destpath$=trimright$(destPath$,"/")
; destpath$=trimright$(destPath$,"\")

if folderexist(DestPAth$)

CopyToPath$=Build_Unique_Folder_Name_with_Index(DestPath$,4)
print "Creating Output Folder:"
print CopyToPath$

if Folderexist(CopyToPath$)=false
Makedir CopyToPath$
endif

if Folderexist(CopyToPath$)=true

LogFile$+=row$+cr$
LogFile$+="[Output Files]"+cr$
LogFile$+="Path:"+CopyToPath$+cr$
LogFile$+=row$+cr$+cr$



print "Copying file list"

oldink=getink()
oldfont =getcurrentfont()
TempFont=LoadnewFont("veranda",32,0)
setfont tempfont

Width =getscreenwidth()
Height=getscreenheight()
TempScreen=NewIMage(Width,Height)
CopyRect 0,0,0,width,height,TempScreen,0,0

For lp=0 to FileCount

drawimage TempScreen,0,0,false

s$="Copying File #"+str$(lp)+" of #"+Str$(FileCount)
ink $ff4080a0
Ypos=Height*0.40
CenterText Width/2,Ypos,s$

file$=filelist$(lp)
if len(file$)
DestFileName$=CopyTOPath$+replace$(File$,SrcPath$,"")
if lower$(FIle$) <> lower$(DestFileName$)

ThisFileSize=Filesize(File$)
TrimmedOutputName$=replace$(DestFilename$,CopyToPath$,"")

LogFile$+=digits$(ThisFIleSize,10)+"  "+TrimmedOutputName$+cr$

Ystep=GetTextHeight("|")*1.5

Ypos+=yStep
Text 10,Ypos,File$
Ypos+=yStep
Text 10,Ypos,DestFileName$

#print DestFileName$
CopyFileAndMakePath file$,DestFileName$
; wait 20
endif
endif
sync


next


LogFile$+=Row$+cr$
LogFile$+=Row$+cr$+cr$

LogFile$+=""+cr$
LogFile$+=""+cr$


drawimage TempScreen,0,0,false

deleteimage tempscreen
setfont oldfont
ink oldink


Fh=writenewfile(CopyToPath$+"___COPIED_FILE_LIST.txt")
if fh
writechr fh,logfile$,len(logfile$)
closefile fh
endif


endif

else
 ink $ff0000
print "Back up folder doesn't exist"
     ink -1
endif

#print logfile$

EndFUnction





; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------
; ---------------------- { FILE LIST SCREEN FUNCTIONS ]----------------------------
; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------


Function  TrimFileList$(Filelist$() , WantedExtensions$)


WantedExtensions$=trimright$(WantedExtensions$,"|")
WantedExtensions$+="|"

count=getarrayelements(filelist$())
outputCount=0

For lp=0 to Count
file$=filelist$(lp)
if len(file$)

ext$=getfileext$(File$)
if len(ext$)
ext$="|."+lower$(Ext$)
if instring(wantedExtensions$,ext$)>0
if fileexist(File$)
Filelist$(outputCount)=File$
#print file$
outputCount++
endif
endif
endif
endif
next

redim Filelist$(outputCount)

EndFUnction outputCount



Function  COmpute_Total_File_Size_In_K(Filelist$())

MegaByteSize=1024*1024
For lp=0 to getarrayelements(filelist$())
file$=filelist$(lp)
if len(file$)

if fileexist(file$)
filesizeinbytes=Filesize(file$)
FileSizeInK=FileSizeINBytes/1024
if (FileSizeInK*1024)<FileSizeINBytes
FileSizeInK++
endif
TotalSizeInK+=FileSizeInK
endif
endif
next

EndFUnction TotalSizeInK





Function CopyFileAndMakePath(SrcFile$,DEstFile$)

#print "Copying File:"
#print SrcFIle$
#print DestFile$
#print ""

Destpath$=GetFoldername$(DestFile$)

if folderexist(DestPath$)
CopyThisFile=true
else

Dim SubFolders$(256)
DestPath$=replace$(destpath$,"/","\")

Count=splittoarray(DestPath$,"\",SubFolders$(),0)

; print count
Path$=""
For lp=0 to Count-1
Path$+=SubFolders$(lp)

if LP<Count
Path$+="\"
endif
if folderexist(path$)=false
makedir Path$
endif
next

CopyThisFile=true

endif

if CopyThisFile=true
if fileexist(srcfile$)
copyfile SrcFile$,DestFile$
endif
endif

EndFUnction



; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------
; ---------------------- { FOLDER SCANNING FUNCTIONS ]----------------------------
; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------
; --------------------------------------------------------------------------------



Function GetSelectedFilesFromDir(RefreshRenderFunction$,FileList$(),path$,Recursive=false,MaxNumberOfFiles=-1)
oldpath$=currentdir$()
Dim TempScanfolder$(500)

device$=getdevicename$(Path$)


RefreshINterval=250
; NextRefresh=Timer()+RefreshINterval
StartTime=Timer()

do

if Timer()=> NextRefresh
callfunction RefreshRenderFunction$,FilesFound,StartTime
NextRefresh=Timer()+RefreshINterval
endif

FilesFound,FoldersFound=Priv_Scanfolder(fileList$(),TempScanfolder$(),FilesFound,FoldersFound,path$)

if FOldersFound>0

if Recursive=false
exitdo
endif

repeat
  FoldersFound--

BannedPath=false
  path$=TempScanfolder$(FoldersFound)+"\"

// -------------------------------------------------------------
// Check for windows path, if so, ignore that path completely
// -------------------------------------------------------------
if instring(Path$,device$+"\Windows\")>0
#print "ignore windows folder"
#print Path$
BannedPath=true
if FoldersFound<1
Exit
endif
endif

if instring(Path$,"PlayBASIC\")>0
#print "ignore PlayBASIC folder"
#print Path$
BannedPath=true
if FoldersFound<1
Exit
endif
endif

until BannedPath=false

else
  exitdo
endif
if MaxNumberOfFiles>-1
if FilesFound>MaxNumberOfFiles
exitdo
endif
endif

loop

undim TempScanfolder$()  
redim FileList$(FilesFound)
cd oldpath$
Endfunction FilesFound



` *=----------------------------------------------------------------=*
`           The following functions are just used in the process
` *=----------------------------------------------------------------=*

Function Priv_Scanfolder(files$(),Folder$(),FilesFound,FoldersFound,path$)
 FileArraySize =GetArrayElements(Files$(),1)
 FolderArraySize =GetArrayElements(Files$(),1)

;  CD path$

 File$=FirstFile$(path$)
 While File$<>""
  if _FileLib_Screen_Filename(file$)
   Filename$=Path$+File$
  if FileType(filename$)=1
  ; Add the File
   if FilesFound=>FileArraySize
    NewSize=(FilesFound * 2)
    if NewSize<1000 then NewSize=1000
    ReDim Files$(NewSize)
    FileArraySize=getarrayelements(Files$(),1)
   endif
   Files$(FilesFound)=Filename$
   inc FilesFound

  else

  ; Add folder
   if FoldersFound=>FolderArraySize
    NewSize=FoldersFound * 2
    if NewSize<1000 then NewSize=1000
    Redim Folder$(NewSize)
    FolderArraySize=getarrayelements(Folder$(),1)
   endif
   Folder$(FoldersFound)=Filename$
   inc FoldersFound
 endif
  endif
    File$=NextFile$()
 EndWhile
EndFunction FilesFound,FoldersFound



Function _FileLib_Screen_Filename(filename$)
 select filename$
  case ".", ".." , "\"
   exitfunction false
  default
   exitfunction true
 endselect
EndFunction flag




Function Build_Unique_Folder_Name_with_Index(BasePath$,Digits)

; print BasePath$
ThisDate$=replace$(GetNumericDateYMA(CurrentDate$()),",","")+"-"
BaseFolderName$+=D$

Max=10^Digits
For lp=1 to Max
TestFoldername$=Basepath$+ThisDate$+digits$(lp,Digits)+"\"
if folderexist(testFolderName$)=false
NewFoldername$=TestFoldername$
exit
endif
next


; print NewFoldername$

EndFUnction NewFoldername$



Function GetNumericDateYMA(THisdate$)

  ListOFmonths$="Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"
 
  Dim _GetNumericDate$(3)

  Thisdate$=trim$(thisdate$," "+chr$(9))

  // replace delimiters chr's
  Thisdate$=replace$(thisdate$," ","/",1,false,false)
  Thisdate$=replace$(thisdate$,"/",",",1,false,false)

  // Split the feilds to an array
  if splittoarray(Thisdate$,",",_GetNumericDate$(),1)=3
     pos=INstring(lower$(listofmonths$),lower$(_GetNumericDate$(2)),1,false)
     Month$=digits$(((pos-1)/4)+1,2)
     Result$=_GetNumericDate$(3)+","+Month$+","+_GetNumericDate$(1)
  endif
  unDIm _GetNumericDate$()

EndFunction Result$, DayInHistory