How to Rename Files With a VBS

Renaming files is a common practice with all development and programming tools. When saving a file it is usually a good idea to rename it. The renamed file will often carry a BAK extension, or have the date as part of the new file name. Whatever the reason, knowing how to rename a file is essential in VBS (or VBScript, which stands for Visual Basic Script) or any other programming language. To complete this task, you'll need to have VBS programming experience.

Instructions

    • 1

      Open the text editor you are using to write the code.

    • 2

      Locate the area in your code where you are going to add the line to rename the file.

    • 3

      Type in the following line of code:

      Rename ("FilePath\FileName")

      The FilePath is the full path to the file (i.e. "C:\windows\temp").
      The FileName is the new name of the file (i.e. "MyNewDoc.txt").

      So, the code would look like this:

      Rename ("C:\windows\temp \ MyNewDoc.txt")

Tips & Warnings

  • This article is intended for those with programming experience in VBScript.

  • A piece of sample code would look like the following:

  • strComputer = "."

  • Set objWMIService = GetObject("winmgmts:" & "!\\" & strComputer & "\root\cimv2")

  • Set colFiles = objWMIService.ExecQuery _

  • ("Select * from CIM_DataFile where Name = " & "'c:\\windows\\temp\\MyOldDoc.txt")

  • errResult = objFile.Rename("C:\windows\temp \ MyNewDoc.txt")

  • The full path of the file must be used or an error will result.

Related Searches:

References

Comments

You May Also Like

  • How do I Rename Files With a VBS Script?

    Visual Basic Scripting (VBS) is a language used by system administrators to automate tasks such as renaming files. The administrator creates a...

  • How to Check If a File Exists Using VBScript

    At times, when programming with VBScript, a programmer will want to know if a file exists prior to performing another function. This...

  • How to Create a File in VBScript

    VBScript is a lightweight programming language developed by Microsoft. It is very useful to know how to program in VBScript, since its...

  • How to Rename File Extensions

    A file extension is typically a three-letter code that follows a dot (.) in the file name. The extension allows you to...

  • What Is the File Extension VBS?

    The file extension VBS refers to a VBScript file. This is a computer file programmed in Visual Basic Scripting Edition, a subset...

  • VBS Script File Types

    VBS Script File Types. The VBS file extension is associated with Visual Basic. These are script files which are useful for programming...

  • How to Rename a File or Folder

    It's easy to rename a file or folder when its contents change. These instructions should work for most operating systems.

  • How to Write a VBS Script to Open a File

    Visual Basic Scripting (VBS) is a language based on the Visual Basic compiler. VBS files are written in plain text, and when...

Related Ads

Featured