Re: Createdir and RemoveDir functions

new topic     » goto parent     » topic index » view thread      » older message » newer message

Ditulis 12-10-00, 20:13 > 9-10-00 6:34 OtterDad

 Thanks for all who helped me in making dir in win32. it works now.

______________________________________________________________________
O> here is a modified program - i used Jeffrey Fielding's win32fil.zip and added
a CreateDir and a RemoveDir function. tested code follows. there are lots of
comments and pause statements in it to
O> you can see it as it works - enjoy!

O> -- Written by Waldo 10-08-00 otter at full-moon.com
O> -- all code is freely usable. use at your own risk. your mileage may vary.

O> -- demo program to show win32api calls to Create and Remove Directories
O> -- copy, move, and delete files

O> -- **************************************************************************
O> -- these are standard euphoria includes - MUST be included to make
O> -- copyfile, movefile, deletefile, CreateDir and RemoveDir work
O> include file.e
O> include machine.e
O> include dll.e
O> -- **************************************************************************

O> -- **************************************************************************
O> -- this list of optional functions only needed to make demo coding easier

O> -- prints a simple sequence to screen with a carraige return
O> global procedure printscreen(sequence text)
O>      printf(1,"%s\n",{text})
O> end procedure

O> -- prints a simple sequence to screen WITHOUT a carraige return
O> global procedure printscree(sequence text)
O>      printf(1,"%s ",{text})
O> end procedure

O> -- only needed to make my pause function work
O> include get.e

O> -- pause for user input - normally used during debug phase
O> global procedure pause()
O>      atom t
O>      printscreen("Program paused - Press x to exit, any key to continue ")
O>      t=wait_key()
O>      if t=120 or t=88 then
O>          abort(1)
O>      end if
O>      printscreen("")
O> end procedure

O> -- optional functions end here
O> -- **************************************************************************

O> -- **************************************************************************
O> -- Win32 file functions by Jeffrey Fielding (JJProg at cyberbury.net)
O> -- All return 1 if success, 0 if not
O> -- success = DeleteFile(filename)
O> -- success = MoveFile(old, new)
O> -- success = CopyFile(old, new, failIfExists)
O> -- failIfExists is 1 if you want it to cancel if new already exists
O> --                 0 if you want it to overwrite new

O> constant KERNEL32 = open_dll("kernel32")
O> if KERNEL32 = 0 then
O>      puts(1,"Couldn't open kernel32.dll.\n")
O>      abort(1)
O> end if
O> constant pDeleteFile =
define_c_func(KERNEL32,"DeleteFileA",{C_POINTER},C_LONG)

O> constant pMoveFile =
define_c_func(KERNEL32,"MoveFileA",{C_POINTER,C_POINTER},
O>      C_LONG)

O> constant pCopyFile = define_c_func(KERNEL32,"CopyFileA",{C_POINTER,C_POINTER,
O>      C_LONG},C_LONG)

O> global function DeleteFile(sequence name)
O>      atom a
O>      object r
O>      a = allocate_string(name)
O>      r = c_func(pDeleteFile,{a})
O>      free(a)
O>      return r
O> end function

O> global function MoveFile(sequence exists, sequence new)
O>      atom a, b
O>      object r
O>      a = allocate_string(exists)
O>      b = allocate_string(new)
O>      r = c_func(pMoveFile,{a,b})
O>      free(a)
O>      free(b)
O>      return r
O> end function

O> global function CopyFile(sequence exists, sequence new, integer failIfExists)
O>      atom a, b
O>      object r
O>      a = allocate_string(exists)
O>      b = allocate_string(new)
O>      r = c_func(pCopyFile,{a,b,failIfExists})
O>      free(a)
O>      free(b)
O>      return r
O> end function
O> -- **************************************************************************

O> -- **************************************************************************
O> -- CreateDir and RemoveDir by Waldo 10-08-00 otter at full-moon.com
O> -- based on Jeffrey Fielding's code found in win32fil.zip

O> -- success = CreateDir(tocreate,securityinfo)
O> --  note: always pass "" as second parameter
O> -- success = RemoveDir(toremove)
O> constant pCreateDir =
define_c_func(KERNEL32,"CreateDirectoryA",{C_POINTER,C_INT},
O>      C_LONG)

O> constant pRemoveDir =
define_c_func(KERNEL32,"RemoveDirectoryA",{C_POINTER},C_LONG)

O> global function CreateDir(sequence tocreate, sequence security)
O>      atom a, b
O>      object r
O>      a = allocate_string(tocreate)
O>      b = allocate_string(security)
O>      --r = c_func(pCreateDir,{a,b})
O>      -- pass it a null as the security pointer and it works fine
O>      r = c_func(pCreateDir,{a,0})
O>      free(a)
O>      free(b)
O>      return r
O> end function

O> global function RemoveDir(sequence toremove)
O>      atom a
O>      object r
O>      a = allocate_string(toremove)
O>      r = c_func(pRemoveDir,{a})
O>      free(a)
O>      return r
O> end function
O> -- **************************************************************************

O> -- **************************************************************************
O> -- this is not my code and i can't remember where i found it
O> -- Apologies to the original Author for not being able to give proper credit

O> -- return path portion of sequence path\\file.ext WITHOUT the final "\"
O> -- example get_path("c:\\temp\\test\\testfile.dat") returns "c:\temp\test"
O> global function get_path(sequence name)
O>      integer bslash
O>      bslash = 1
O>      for t = length(name) to 1 by -1 do
O>          if name[t] = '\\' then
O>              bslash = t
O>              exit
O>          end if
O>      end for
O>      return name[1 .. bslash - 1]
O> end function

O> -- return filename portion of sequence path\\file.ext WITHOUT the leading "\"
O> -- example get_name("c:\\temp\\test\\testfile.dat") returns "testfile.dat"
O> global function get_name(sequence name)
O>      integer bslash
O>      bslash = 1
O>      for t = length(name) to 1 by -1 do
O>          if name[t] = '\\' then
O>              bslash = t
O>              exit
O>          end if
O>      end for
O>      return name[bslash+1 .. length(name)]
O> end function

O> global function make_dir(sequence dir_name)
O>      --  make a dir plus any parent directories
O>      sequence path
O>      -- sequence cmd
O>      atom lendir,temp,result

O>      result = 0

O>      -- strip off trailing "\" if present
O>      lendir = length(dir_name)
O>      if equal("\\",dir_name[lendir..lendir]) then
O>          dir_name=dir_name[1..lendir-1]
O>      end if

O>      -- only attempt the create if it doesn't already exist
O>      if atom(dir(dir_name)) then
O>          path = get_path(dir_name)

O>          -- create parent directories if necessary
O>          if length(path) > 0 then
O>              temp = make_dir(path)

O>              -- orig system code replaced by CreateDir function
O>              -- cmd = "mkdir " &  "\"" &  dir_name &  "\""
O>              -- puts(1, cmd &  '\n')
O>              -- system(cmd, 2)

O>              if CreateDir(dir_name,"") then
O>                  -- uncomment next line to see dirs created on the fly
O>                  --printscreen(dir_name & " created")
O>                  result = -1
O>              else
O>                  -- uncomment next line to see dirs created on the fly
O>                  -- printscreen("couldn't create " & dir_name)
O>              end if

O>          end if
O>      end if
O>      return result
O> end function
O> -- **************************************************************************

O> global constant false=0, true=not false
O> atom filenum
O> sequence origfile,newfile,testdir,testdir2,testfile1,testfile2,testfile3

O> testdir = "c:\\qwert\\test\\" testdir2 = "c:\\qwert\\"
O> origfile = "testfile.dat" newfile = "testfile2.dat"
O> testfile1 = testdir & origfile testfile2 = testdir & newfile
O> testfile3 = testdir2 & origfile

O> -- create a test directory to play with
O> if make_dir(testdir) then
O>      printscreen(testdir & " created") pause()
O> else
O>      printscreen(testdir & " ** NOT ** created") pause() abort(1)
O> end if

O> -- make a file to play with
O> filenum = open(testfile1,"w")
O> if filenum = -1 then
O>      printscreen("couldn't create " & testfile1) pause() abort(1)
O> else
O>      printf(filenum,"%s\n","garbage goes here") close(filenum)
O>      printscreen(testfile1 & " created successfully") pause()
O> end if

O> -- first copyfile should work just fine
O> if CopyFile(testfile1,testfile2,true) then
O>      printscreen(testfile1 & " copied to " & testfile2) pause()
O> else
O>      printscreen(testfile1 & " ** NOT ** copied") pause() abort(1)
O> end if

O> -- this copyfile should fail because the new file already exists
O> if CopyFile(testfile1,testfile2,true) then
O>      printscreen(testfile1 & " copied to " & testfile2) pause() abort(1)
O> else
O>      printscreen(testfile1 & " ** NOT ** copied to " & testfile2)
O>      printscreen("because " & testfile2 & " already exists") pause()
O> end if

O> -- now move the new file to the parent directory
O> if MoveFile(testfile2,testfile3) then
O>      printscreen(testfile2 & " moved to " & testfile3) pause()
O> else
O>      printscreen(testfile2 & " ** NOT ** moved") pause() abort(1)
O> end if

O> -- now delete the files so we can exit cleanly
O> if DeleteFile(testfile1) then
O>      printscreen(testfile1 & " deleted") pause()
O> else
O>      printscreen(testfile1 & " ** NOT ** deleted") pause() abort(1)
O> end if

O> if DeleteFile(testfile3) then
O>      printscreen(testfile3 & " deleted") pause()
O> else
O>      printscreen(testfile3 & " ** NOT ** deleted") pause() abort(1)
O> end if

O> -- and finally remove the test directory
O> if RemoveDir(testdir) then
O>      printscreen(testdir & " removed") pause()
O> else
O>      printscreen(testdir & " ** NOT ** removed") pause()
O> end if

O> printscreen("program complete")
O> pause()



--
Aku udah ngebales imel seukuran 9219, nih...

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu