1. Createdir and RemoveDir functions
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()
2. Re: Createdir and RemoveDir functions
and right after i hit my email's Send button i received Ray Smith's code which
is almost identical to my own which only proves that a) i am a code thief or, b)
great minds think alike
<giggle>
3. Re: Createdir and RemoveDir functions
Hahaha
Yes what a coincidence!!!
I'll go for choice b
Ray Smith
----- Original Message -----
From: OtterDad <otter at FULL-MOON.COM>
To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
Sent: Monday, October 09, 2000 9:38 AM
Subject: Re: Createdir and RemoveDir functions
> and right after i hit my email's Send button i received Ray Smith's code
which is almost identical to my own which only proves that a) i am a code
thief or, b) great minds think alike
>
> <giggle>
4. Re: Createdir and RemoveDir functions
- Posted by Aku <aku at inbox.as>
Oct 12, 2000
-
Last edited Oct 13, 2000
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...