FileSystem

This page contains functions using the FileSystem in Windows Script Host.1

Table of Contents

Rename files

'VBScript written to copy and rename files (18/07/2008)
'NOTE parent folder must be on C drive
'*********

**'DECLARE VARIABLES**
Dim objFSO
Dim objCurFolder, objNewFolder
Dim objFiles, objFile
Dim objCurBaseName, objNewBaseName
Dim strCurFilePath, strNewFilePath
Dim sArray, sDelimiter

'*****************************************
**'SET THIS VARIABLE**
sDelimiter = "EDNA"
'*****************************************

Set objFSO = CreateObject("Scripting.FileSystemObject")

**'SET FOLDER DETAILS**
'*****************************************
Set objCurFolder = objFSO.Drives("C").RootFolder.SubFolders("ModifyFileName")
Set objFiles = objFSO.Drives("C").RootFolder.SubFolders("ModifyFileName").Files

**'CREATE NEW FOLDER**
If NOT objFSO.FolderExists (objCurFolder  & "\" & "NewFileNames") Then
    Set objNewFolder = objFSO.CreateFolder ("NewFileNames")
End if

**'GET EACH FILE IN FOLDER**
For each objFile in objFiles
    If objFile.Name <> "ModifyFileName.vbs" Then

**'GET BASENAME**
        objCurBaseName = objFSO.GetBaseName(objFile)
**'MODIFY BASENAME**
        If instr(1,objCurBaseName,sDelimiter) <> 0 then
            sArray = Split(objCurBaseName, sDelimiter)
                    objNewBaseName = sDelimiter& sArray(1)
        Else
                    objNewBaseName = objCurBaseName
        End if

**'CREATE NEW FILE PATH**
        strNewFilePath = objFSO.GetParentFolderName(objFile) & "\NewFileNames" & "\" & objNewBaseName & "." & objFSO.GetExtensionName(objFile) 

**'CHECKS FOR DUPLICATE FILE**
If objFSO.FileExists(strNewFilePath) Then
    Wscript.Echo("File " & objNewBaseName & "." & objFSO.GetExtensionName(objFile) & " already exists, filename not changed")
Else
**'COPY FILE WITH NEW NAME**
    objFile.Copy(strNewFilePath)

**'DELETE ORIGINAL FILE**
           If objFSO.FileExists(strNewFilePath) Then
                 objFile.Delete
            End If
End If
    End if
Next

page_revision: 11, last_edited: 1237829723|%e %b %Y, %H:%M %Z (%O ago)
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License