Author Topic: File copying with checking  (Read 1733 times)

0 Members and 1 Guest are viewing this topic.

chrisc

  • Guest
File copying with checking
« on: April 27, 2018, 11:33:00 AM »
The following code shows how to  check for file existence and before  copying the files
from one folder to another.

Code: [Select]
' ChkCopy_File.o2bas
   $ filename "ChkCopy_File.exe"
       uses rtl64

  '  this program checks whether a file exist before copying
 '  the file to another folder

uses corewin


===================================
'  Check whether folder or file exist
'  do not place in the last slash for the folder name
function FileExists(char*wFil) as int
       WIN32_FIND_DATA wFildat
       sys hFil
        hFil=FindFirstFile wFil,wFildat
        if hFil>0 then
            FindClose hFil
            return 1
        end if
end function



  ==============================
'  This function obtains the path name of
'  a given file wfnam
  Function GitPathNam( string wfnam ) as string
   if len(wfnam) = 0 then
      return ""
      exit function
  end if

   byte b at strptr wfnam
 ' iterates around the file name to get the position
'  of the last backslash
  sys islash
  do
    select b
    case 0 
              exit do
    case 92
            '   position of the last backslash
                islash=@b
    end select

    @b++
  end do
  return left wfnam,islash-strptr(wfnam)
  end function


'==========================
'  for copying a file from Source folder to
'  the destination folder
Function NcopyFil(string SrcFilepath, string DstFilepath) as bool
 string  srcFolder , dstFolder
'  Get the folder names
    srcFolder  = GitPathNam(SrcFilepath)
   dstFolder  =  GitPathNam( DstFilepath)


'  check if the folders exist or not
'  exit if it does not exist
      if  FileExists(SrcFolder)  = 0 then
           return %False
           exit function
      end if
       if  FileExists(dstFolder)  = 0 then
             return %False
           exit function
      end if

          if FileExists(SrcFilepath ) = 1 then
 '              destination folder and source file exist , we can copy the source file
'               and overwrites the existing file in destination folder
'               Need to set to %False to get it overwritten
                  Function =  CopyFile(SrcFilepath,DstFilepath  , %False)
           end if

end function






 '====================
'  prog starts
 


      ' check folder  but do NOT place in the back slash at the end
       if  FileExists("C:\Temp")  = 1 then
                   print  " folder C:\Temp  exist "
       else
                 print  " folder C:\Temp  does NOT exist "
        end if


       if  FileExists("C:\Temp\Get Start\Tgh")  = 1 then
                   print  " folder C:\Temp\Get Start\Tgh  exist "
       else
                 print  " folder C:\Temp\Get Start\Tgh  does NOT exist "
        end if


   ' -----------------
   ' Check the destination file folder before copying over
    bool blnAnyCopy
    string   KsrcFile , KdstFile
     KsrcFile = "C:\Temp\BMP files\bari.bmp"
     KdstFile = "C:\Temp\Test dump\bari.bmp"

     ' start the copying
     blnAnyCopy = NcopyFil(  KsrcFile , KdstFile)

          if   blnAnyCopy  then
                print  " copying successful   "
         else
               print  " copying fail "
         end if