file and folder copy

BlitzMax Forums/BlitzMax Beginners Area/file and folder copy

Bremer(Posted 2008) [#1]
I have not had much experience in file handling, so I am having some problems getting a specific program made.

I need to be able to copy files and folders from one location to another, but I need it to copy and delete the source files one file at a time.

I have the following code to go though folders and files, but I am uncertain as to how I can use this to do the above.

enumFiles("E:\applications\")

Function enumFiles:Int(dir:String)
	
	Local folder:Int=ReadDir(dir)
	Local file:String
	Local fullpath:String

	Repeat
		file=NextFile(folder)
		If (file) And (file <> ".") And (file <> "..")
			fullPath = RealPath(dir+"/"+file)
			If FileType(fullPath)=FILETYPE_DIR
				Print "Folder: "+file
				enumFiles(fullPath)
			Else
				Print "File: "+file
			End If
		End If
	Until (file=Null)

	CloseDir folder
End Function


I cannot have it simply copy an entire directory at once, I need it to copy each file one at a time, and have it create the folders at the destination location one at a time as needed.


SebHoll(Posted 2008) [#2]
Just knocked this up for you:

SuperStrict

'Warning, this function will overwrite any existing files in the destination
'directory if they have the same filenames WITHOUT WARNING.

CopyFolder( "D:\Favorites\", "C:\Temp\My Favorites\")

Function CopyFolder( pSource$, pDest$ )
	
	pSource = StripSlash(pSource.Replace("\","/"))
	pDest = StripSlash(pDest.Replace("\","/"))
	
	'Create the directory if it isn't already there.
	CreateDir( pDest, True )
	
	For Local tmpItem$ = EachIn LoadDir(pSource,True)
		
		Local tmpFullItemPath$ = pSource + "/" + tmpItem
		Local tmpFullItemDest$ = pDest + "/" + tmpItem
		
		Select FileType(tmpFullItemPath)
		
			Case FILETYPE_FILE
				Print "Copying file: ~q" + tmpItem + "~q"
				CopyFile( tmpFullItemPath, tmpFullItemDest )
				
			Case FILETYPE_DIR
				Print "Copying folder: ~q" + tmpItem + "~q"
				CopyFolder(tmpFullItemPath, tmpFullItemDest)
			
		EndSelect
	
	Next

EndFunction
Hope it helps!


Bremer(Posted 2008) [#3]
I must have overlooked the LoadDir command, because that surely simplyfies filehandling somewhat.

It works really well as far as copying files and folders. But it does not delete the source files and folders.

I am not sure what I need is possible. This is the senario that I am trying to solve:

We have some folders with files and subfolders that needs to be moved to another area of a network drive. Unfortunately we cannot just move the files because of problems with access rights being moved with the files when using move, but if we copy first and then delete the old, then the access rights are not transfered with the files.

We also have the problem of not having room on the drive to simply move complete folders at once, but we figured that if we copied one file at a time and deleted the source afterwards, then we would not need much extra space and there should be room on the drive for it.


SebHoll(Posted 2008) [#4]
Aha, if you wanted an equivalent MoveFolder() function, why didn't you ask... ;-)

SuperStrict

'Warning, this function will overwrite any existing files in the destination
'directory if they have the same filenames WITHOUT WARNING.

MoveFolder( "C:\Temp\My Favorites\", "D:\Favorites\")

Function MoveFolder%( pSource$, pDest$ )
	
	Local tmpReturnValue% = True
	
	pSource = StripSlash(pSource.Replace("\","/"))
	pDest = StripSlash(pDest.Replace("\","/"))
	
	'Create the directory if it isn't already there.
	CreateDir( pDest, True )
	
	For Local tmpItem$ = EachIn LoadDir(pSource,True)
		
		Local tmpFullItemPath$ = pSource + "/" + tmpItem
		Local tmpFullItemDest$ = pDest + "/" + tmpItem
		
		Select FileType(tmpFullItemPath)
		
			Case FILETYPE_FILE
				'Test if copy was successful and if so, remove the original file.
				If CopyFile( tmpFullItemPath, tmpFullItemDest ) Then
					DeleteFile( tmpFullItemPath )
				Else
					Print "Unable to move ~q" + tmpFullItemPath + "~q"
					tmpReturnValue = False
				EndIf
				
			Case FILETYPE_DIR
				If Not MoveFolder(tmpFullItemPath, tmpFullItemDest) Then tmpReturnValue = False
			
		EndSelect
	
	Next
	
	If tmpReturnValue Then DeleteDir(pSource) Else Print "Unable to delete source folder: ~q" + pSource + "~q"
	Return tmpReturnValue

EndFunction
Seems to work fine here. :-P


Bremer(Posted 2008) [#5]
That sure does seem to do the job just fine. Thank you very much, its really appreciated.