Creating directory preserving name case

MSDN:

This is sample code. Add error handling and adjust to your requirements as necessary.

* WIN API
 
lcNewFolder = "C:\Test\TEST2\test3\TeSt4"
? MakeDir(lcNewFolder)
? SHMakeDir(lcNewFolder)
 
*---------------------------------------------------
* CreateDirectory - one level at a time
FUNCTION MakeDir(tcNewFolder)
LOCAL laList[1], lcTemp, lnI, llOK
= ALINES(laList, tcNewFolder, "\") 
lcTemp = laList[1]  
llOK = .T.
FOR lnI=2 TO ALEN(laList)
	lcTemp = lcTemp + "\"+ laList[lnI]
	IF NOT DIRECTORY(lcTemp)
		llOK = CreateDirectory(lcTemp, 0) <> 0
		IF NOT llOK 
			EXIT
		ENDIF	
	ENDIF	
ENDFOR
 
RETURN llOK AND DIRECTORY(tcNewFolder)
 
FUNCTION CreateDirectory(tcNewFolder, tnSecurityAttributes)
	DECLARE Long CreateDirectory IN WIN32API ;
    		String lpPathName, Long lpSecurityAttributes
RETURN CreateDirectory(tcNewFolder, tnSecurityAttributes)   		
*----------------------------------------------------------------
 
* SHCreateDirectory - may not be avaialble in the future versions of Windows
FUNCTION SHMakeDir(tcNewFolder)
RETURN SHCreateDirectory(0, STRCONV(tcNewFolder,5)) = 0
 
FUNCTION SHCreateDirectory(tnhWnd, tcNewFolder)
	DECLARE Long SHCreateDirectory IN shell32.dll Long hwnd, String  pszPath
RETURN SHCreateDirectory(tnhWnd, tcNewFolder)
* WSH
lcNewFolder = "C:\Test\TEST2\test3\TeSt4"
? MakeDir(lcNewFolder)
 
*---------------------------------------------------
* CreateFolder
FUNCTION MakeDir(tcNewFolder)
LOCAL laList[1], lcTemp, lnI, llOK, loFSO
loFSO = CreateObject("Scripting.FileSystemObject")
 
= ALINES(laList, tcNewFolder, "\") 
lcTemp = laList[1]  
llOK = .T.
FOR lnI=2 TO ALEN(laList)
	lcTemp = lcTemp + "\"+ laList[lnI]
	IF NOT DIRECTORY(lcTemp)
		TRY
			loFSO.CreateFolder(lcTemp)
		CATCH TO oExp	
			llOK = .F.
		ENDTRY
		IF NOT llOK 
			EXIT
		ENDIF	
	ENDIF	
ENDFOR
 
RETURN llOK AND DIRECTORY(tcNewFolder)
* Shell
lcNewFolder = "C:\Test\TEST2\test3\TeSt4"
? MakeDir(lcNewFolder)
 
*---------------------------------------------------
* NewFolder
FUNCTION MakeDir(tcNewFolder)
LOCAL loShellApp, lcRoot, loRoot, llOK
llOK = .T.
loShellApp = CREATEOBJECT("Shell.Application")
lcRoot = LEFT(tcNewFolder,3)
loRoot = loShellApp.NameSpace(lcRoot)
TRY
	loRoot.NewFolder(SUBSTR(tcNewFolder,4))
CATCH TO oExp	
	llOK = .F.
ENDTRY
RETURN llOK 
No votes yet