Login Register
Frontpage Code library Pastebin

CopyDirectory()

Author: Jare
Added: 17. maaliskuuta 2021 kello 8.48
Edited: 17. maaliskuuta 2021 kello 8.48
Category: Tiedostot

Description

Kopioi kansion sekä sen sisältämät tiedostot ja alikansiot toiseen kansioon. Vaatii toimiakseen IsAbsolutePath()-funktion: http://www.cbrepository.com/codes/code/105/ Palauttaa True, jos koko kopiointioperaatio onnistui täydellisesti. Palauttaa False, jos jotakin kansiota ei pystytty luomaan tai jos jotakin tiedostoa ei pystytty kopioimaan. Tällaisen virheen havaitessaan funktio ei pysäytä toimintaansa, vaan jatkaa seuraavien tiedostojen/kansioiden kimppuun, joten virhetilanteessa sinulla saattaa olla seurauksena vaillinainen kopiokansio. Funktio pysäyttää toimintansa silloin, jos kohdehakemisto on tavallinen tiedosto tai jos kohdehakemistoa ei ole eikä pystytä jostain syystä luomaan. Myös tällöin palautetaan False. Jos kohdekansio on jo olemassa, sen sisältämille tiedostoille käy näin: - Jos kohdekansiossa on saman nimisiä tiedostoja kuin lähdekansiossa, ne ylikirjoitetaan. - Jos kohdekansiossa on sellaisia tiedostoja, joiden nimiä ei löydy lähdekansiosta, niille ei tapahdu mitään.

Code

Select all
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
Function CopyDirectory(source_directory$, target_directory$)
	// Support Force Variable Declaration
	Dim original_directory$, found_file$, nested_result
	Dim subdirectory.CopyDirectory_subdirectory
	
	// Make the target directory absolute so that it works always when we change the working directory
	If Not IsAbsolutePath(target_directory) Then target_directory = CurrentDir() + target_directory // CurrentDir() contains a trailing backslash.

	// Ensure that the source directory exists
	If Not IsDirectory(source_directory) Then Return False
	
	// Ensure that the target directory exists
	If Not IsDirectory(target_directory) Then
		// The directory does Not exist
		If FileExists(target_directory) Then
			// A regular file with the same name exists.
			// Can't do anything.
			Return False
		Else
			// We can create the directory.
			MakeDir target_directory
			
			// Check that it succeeded.
			If Not IsDirectory(target_directory) Then Return False
		EndIf
	EndIf
	
	nested_result = True // If this stays True, it means that all files and subfolders were copied correctly.
	
	// Go To the source directory
	original_directory = CurrentDir()
	ChDir source_directory
	
	// Iterate the source directory's files And subdirectories.
	StartSearch
	Repeat
		found_file = FindFile()
		If "" = found_file Then Exit
		If found_file <> "." And found_file <> ".." Then
			If IsDirectory(found_file) Then
				// This is a subdirectory.
				// Put it To queue so it will be copied After this StartSearch ... EndSearch session.
				subdirectory = New(CopyDirectory_subdirectory)
				subdirectory\parent_directory = source_directory // Needed For identifying that this subdirectory belongs To this iteration, Not To any subrecursive iteration.
				subdirectory\source_directory = source_directory + "\" + found_file
				subdirectory\target_directory = target_directory + "\" + found_file
			Else
				// This is a regular file.
				// Copy it now.
				CopyFile found_file, target_directory + "\" + found_file // If the target file exists already, it will be overwritten.
				nested_result = nested_result And FileExists(target_directory + "\" + found_file)
			EndIf
		EndIf
	Forever
	EndSearch
	
	// Go back To the original directory already here so that possible relative directory names work in subdirectory copying below.
	ChDir original_directory
	
	// Copy subdirectories
	For subdirectory = Each CopyDirectory_subdirectory
		// Due To recursion, the Type list may contain unprocessed instances from parent directory iterations (damn why cannot CB have local arrays? Would be so much simpler).
		If subdirectory\parent_directory = source_directory Then
			// This subdirectory belongs To this iteration, Not To any parent iteration.
			nested_result = nested_result And CopyDirectory(subdirectory\source_directory, subdirectory\target_directory)
			Delete subdirectory // Done handling this subdirectory.
		EndIf
	Next subdirectory
	
	Return nested_result
EndFunction
Type CopyDirectory_subdirectory
	Field parent_directory$
	Field source_directory$
	Field target_directory$
EndType

Comments

No comments. You can be first!

Leave a comment

You must be logged in to comment.