खोज…


फ़ाइल / फ़ोल्डर / ड्राइव के अस्तित्व के लिए जाँच करना

उपयोग की जाने वाली विधियाँ:

.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 विधि के उपयोग को दिखाता है। प्रतिलिपि फ़ोल्डर विधि का उपयोग करके फ़ोल्डरों के लिए एक ही चीज प्राप्त की जा सकती है।

कोड:

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