플밍_ASP
ASP - FileSystemObject 로 Unique 파일명 생성하기 function
잠수콩
2011. 4. 13. 10:34
Function GetUniqueName(byRef strFileName, DirectoryPath)
Dim strName, strExt
strName = Mid(strFileName, 1, InstrRev(strFileName, ".") - 1)
strExt = Mid(strFileName, InstrRev(strFileName, ".") + 1)
Dim fso
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Dim bExist : bExist = True
'우선 같은이름의 파일이 존재한다고 가정
Dim strFileWholePath : strFileWholePath = DirectoryPath & "\" & strName & "." & strExt
'저장할 파일의 완전한 이름(완전한 물리적인 경로) 구성
Dim countFileName : countFileName = 0
'파일이 존재할 경우, 이름 뒤에 붙일 숫자를 세팅함.
Do While bExist ' 우선 있다고 생각함.
If (fso.FileExists(strFileWholePath)) Then ' 같은 이름의 파일이 있을 때
countFileName = countFileName + 1 '파일명에 숫자를 붙인 새로운 파일 이름 생성
strFileName = strName & "(" & countFileName & ")." & strExt
strFileWholePath = DirectoryPath & "\" & strFileName
Else
bExist = False
End If
Loop
GetUniqueName = strFileWholePath
End Function