Class Path

Med denne klasse kan du nemt oprette en ny folder på disken. Nedenstående eksempel opretter folder “d:\temp\sample dir”

Dim p As New path( "d:\temp" )
Call p.add( "Sample dir?" )
Call p.create()
Print p.getPath()

Class Path kan kopieres herfra:


Class Path As AbstractObject
	Private path As String

%REM
        path: the default path. Use 'TEMP' or 'MY' to use the default temp or 'My Documents' path
%END REM
	Sub New( path As String )
		Select Case Ucase( path )
		Case "TEMP":
			Me.path = getTempPath()
		Case "MY":
			Me.path = getMyDocuments()
		Case Else
			Me.path = path
		End Select

		Call legalize()
	End Sub

%REM
        Returns the 'My Documents' path
%END REM
	Private Function getMyDocuments() As String
		Dim formula As String
		formula = |@RegQueryValue( "HKEY_CURRENT_USER" ; "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders" ; "Personal" )|
		Dim v As Variant
		v = Evaluate( formula )
		getMyDocuments = v( 0 )
	End Function

%REM
                Function getTempPath
                Returns the temp path
%END REM
	Private Function getTempPath() As String
		getTempPath = Environ( "temp" )
	End Function

	Function add( subdir As String )
		On Error Goto eh
		subdir = Replace( subdir, "/", "\" )
		If Left( subdir, 1 ) = "\" Then
			path = path & Mid( subdir, 1 )
		Else
			path = path & subdir
		End If
		Call legalize()

		Exit Function
eh:
		Error Err, getErrorInfo( Me )
	End Function

%REM
Returns the name of the current path
%END REM
	Function getPath() As String
		getPath = path
	End Function

%REM
                Convert to a legal path (remove illegal chars)
%END REM
	Private Function legalize()
		On Error Goto eh
		Const ILLEGAL_CHARS = |*?:"|
		Dim char As String
		Dim s As String
		Dim i As Integer

		Me.path = Replace( path, "/", "\" )
		If Right( path, 1 ) <> "\" Then path = path & "\"

		s = ""

                'Note - : is legal if in second position
		If Mid( path, 2, 1 ) = ":" Then
			s = Left( path, 2 )
			path = Mid( path, 3 )
		End If

		For i = 1 To Len( path )
			char = Mid( path,i ,1 )
			If Instr( ILLEGAL_CHARS, char ) = 0 Then
				s = s & char
			End If
		Next

		path = s

		Exit Function
eh:
		Error Err, getErrorInfo( Me )
	End Function

%REM
Creates the specified path
%END REM
	Function create( )
		On Error Goto eh
		Dim v As Variant
		Dim p As String

		v = Split( path, "\" )

		p = v( 0 )
		Dim i As Integer
		For i = 1 To Ubound( v )
			If v( i ) <> "" Then
				p = p & "\" & v( i )
				If Dir( p, 16+4+2 ) = "" Then 'ATTR_DIRECTORY + ATTR_HIDDEN + ATTR_SYSTEM
					Mkdir( p )
				End If
			End If
		Next

		Exit Function
eh:
		Error Err, getErrorInfo( Me )
	End Function

End Class

cls.system findes i Developer Toolbar basen

Skriv et svar

Din e-mailadresse vil ikke blive publiceret. Krævede felter er markeret med *

*