Createdir and RemoveDir functions

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

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 you can see it as it works - enjoy!

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

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

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

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

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

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

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

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

-- optional functions end here
-- **************************************************************************

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

     result = 0

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

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

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

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

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

         end if
     end if
     return result
end function
-- **************************************************************************

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

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

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

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

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

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

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

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

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

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

printscreen("program complete")
pause()

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

Search



Quick Links

User menu

Not signed in.

Misc Menu