수색…


파일 / 폴더 / 드라이브의 존재 여부 확인

사용 된 방법 :

.DriveExists(strDrive) returns (True/False)
.FileExists(strFile) returns (True/False)
.FolderExists(strFolder) returns (True/False)

다음 코드는 파일 시스템 개체의 " FileExists "메서드를 사용하여 파일의 존재 여부를 확인합니다. 폴더 또는 드라이브의 존재를 확인하기 위해 각각 " FolderExists "또는 " DriveExists "메소드를 사용할 수 있습니다.

암호:

Dim strPath, objFso
strPath = "C:\Users\GS\Desktop\tasks.txt"        'Enter the absolute path of the File/Folder/Drive
Set objFso = CreateObject("Scripting.FileSystemObject")

'Checking for the File's existence
If objFso.FileExists(strPath) then               'returns True if the file exists, else False
    Msgbox "File Exists!"
Else
    Msgbox "File does not Exist!"
End If
Set objFso = Nothing

기존 폴더 삭제 및 새 폴더 만들기

사용 된 방법 :

.DeleteFolder(FileSpec, Force (True/False))
.CreateFolder(Path)
.DeleteFile(FileSpec, Force (True/False))

다음 예제에서는 " DeleteFolder "및 " CreateFolder "메서드를 사용하여 폴더를 삭제하고 만드는 방법을 보여줍니다.

암호:

Dim strFolderPath, objFso
strFolderPath = "C:\Users\GS\Desktop\testFolder"
Set objFso = CreateObject("Scripting.Filesystemobject")

'Checking for the folder's existence and deleting it, if found
If objFso.FolderExists(strFolderPath) then
    objFso.DeleteFolder strFolderPath, True                   'True indicates forceful deletion
End If

'Creating a new Folder
objFso.CreateFolder strFolderPath

Set objFso = Nothing

마찬가지로 " DeleteFile "메소드를 사용하여 파일을 삭제할 수 있습니다.

Dim strFilePath:strFilePath = "C:\Users\GS\Desktop\tasks.txt"
If objFso.FileExists(strFilePath) then
    objFso.DeleteFile strFilePath, True                      'true indicates forceful deletion
End If

파일 / 폴더 복사

사용 된 방법 :

.CopyFile(Source, Dest [,Overwrite (True/False)]
.CopyFolder(Source, Dest [,Overwrite (True/False)]

다음 코드는 CopyFile 메서드를 사용하여 파일을 새 위치로 복사하는 방법을 보여줍니다. CopyFolder 메서드를 사용하여 폴더에 대해 동일한 작업을 수행 할 수 있습니다.

암호:

Dim objFso, strSourcePath, strDestPath
strSourcePath = "C:\Users\GS\Desktop\Source.txt"
strDestPath = "C:\Users\GS\Desktop\Dest.txt"
Set objFso = CreateObject("Scripting.FileSystemObject")
If objFso.FileExists(strSourcePath) then
    objFso.CopyFile strSourcePath, strDestPath, True              'True indicates the overwritting of the file at the destination path i.e, if the file already exists, it will be overwritten
End If
Set objFso = Nothing

파일 / 폴더 이동

사용 된 방법 :

.MoveFile(Source, Dest)
.MoveFolder(Source, Dest)

다음 코드는 MoveFile 메서드를 사용하여 파일을 새 위치로 이동하는 방법을 보여줍니다. MoveFolder 메서드를 사용하여 폴더에 대해 동일한 작업을 수행 할 수 있습니다.

암호:

Dim objFso, strSourcePath, strDestPath
strSourcePath = "C:\Users\GS\Desktop\Source.txt"
strDestPath = "C:\Users\GS\Desktop\Folder\Dest.txt"
Set objFso = CreateObject("Scripting.FileSystemObject")
If objFso.FileExists(strSourcePath) then
    objFso.MoveFile strSourcePath, strDestPath
End If
Set objFso = Nothing

참고 : 우리는 파일 이름을 바꿀 수있는 파일 시스템 객체의 메서드를 가지고 있지 않습니다. 그러나 MoveFile 메서드를 사용하면 파일을 아래와 같이 다른 이름으로 같은 위치로 옮길 수 있습니다.

Dim objFso, strSourcePath, strDestPath
strSourcePath = "C:\Users\GS\Desktop\OldName.txt"
strDestPath = "C:\Users\GS\Desktop\NewName.txt"       'Location is same but the name is different
Set objFso = CreateObject("Scripting.FileSystemObject")
If objFso.FileExists(strSourcePath) then
    objFso.MoveFile strSourcePath, strDestPath
End If
Set objFso = Nothing

폴더에 대한 객체 참조

사용 된 방법 :

.GetFolder(strPath) - Returns an object referring to the path

getFolder 메서드를 사용하여 폴더에 대한 객체 참조를 설정하고 다른 작업을 수행 할 수 있습니다.

암호:

Dim strFolderPath, objFso, objFolder
strFolderPath = "C:\Users\GS\Desktop\LogsFolder"
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFso.getFolder(strFolderPath)

'Accessing the Folder's Properties
Msgbox objFolder.Name                            'Returns the Folder's Name
Msgbox objFolder.Size                            'Returns the Folder's size in Bytes  
Msgbox objFolder.DateCreated                     'Returns the Folder's creation date 
Msgbox objFolder.DateLastModified                'Returns the Folder's last modified date
Msgbox objFolder.Path                            'Returns the Folder's Absolute Path

Dim objChildFolders
Set objChildFolders = objFolder.SubFolders       'Returns the collection of all subfolder 

Dim objChildFiles
Set objChildFiles = objFolder.Files              'Returns the collection of all files contained in the folder  

'Using the Folder's methods
objFolder.Copy strDestPAth, True                 'Copies the folder to path contained in strDestPath and overwrite Flag=True
objFolder.Delete True                            'Deletes the Folder; True indicates forceful Deletion
objFolder.Move strDestPath                       'Moves the Folder to the path contained in strDestPath variable 
objFolder.CreateTextFile strFileName, True       'Created a new text file inside the folder and overwrites the existing file(if it exists)
Set objChildFiles = Nothing
Set objChildFolders = Nothing
Set objFolder = Nothing
Set objFso = Nothing

파일에 대한 객체 참조

사용 된 방법 :

.GetFile(strPath) - Returns an object referring to a file.

우리는 getFile 메소드를 사용하여 파일에 대한 객체 참조를 설정하고 그에 대한 다른 연산을 수행 할 수 있습니다.

암호:

Dim strFilePath, objFso, objFile
strFilePath = "C:\Users\GS\Desktop\LogsFolder\file.txt"
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFile = objFso.getFile(strFilePath)

'Accessing the File's Properties
Msgbox objFile.Name                            'Returns the File's Name
Msgbox objFile.Size                            'Returns the File's size in Bytes  
Msgbox objFile.DateCreated                     'Returns the File's creation date 
Msgbox objFile.DateLastModified                'Returns the File's last modified date
Msgbox objFile.Path                            'Returns the File's absolute path

'Using the File's Methods
objFile.Delete True                            'Forcefully deletes the File
objFile.Copy strDestPath, True                 'Copies the file to path contained in variable strDestPath
objFile.Move strDestPath                       'Moves the file to the path contained in the variable strDestPath
objFile.OpenAsTextStream mode                  'Opens the file as a text stream in either Read mode(mode=1), write mode(mode=2) or Append mode(mode=8)
Set objFile = Nothing
Set objFso = Nothing


Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow