Jump to content

renaming a file using VBS?


Guest joelee

Recommended Posts

Guest joelee

Hi all

I'm new to scripting and I feel like a real id*** asking this really simple question.

But all i want to do is RENAME A FILE!

How do I bloody do this?

cheers in advance !

Dim fso, SomeFile, bolFileExists
Set fso = CreateObject("Scripting.FileSystemObject")

fileName = "testfile.txt"
Set SomeFile = fso.CreateTextFile(fileName)

Name SomeFile As "testfile_new.txt"

Link to comment
Share on other sites


Here you go:

Renames the file C:\Scripts\Toggle_Service.vbs to C:\Scripts\Toggle_Service.old.

strComputer = "."

Set objWMIService = GetObject _

("winmgmts:" & "!\\" & strComputer & "\root\cimv2")

Set colFiles = objWMIService.ExecQuery _

("Select * from Cim_Datafile where Name = " _

& "'c:\\scripts\\toggle_service.vbs'")

For Each objFile in colFiles

errResult = objFile.Rename("c:\scripts\toggle_service.old")

Wscript.Echo errResult

Next

Link to comment
Share on other sites

Guest joelee

In the referencing MSDN page with the above code, it says that I must put in the absolute path. Is there a way to rename a file using relative rather absolute referencing?

Link to comment
Share on other sites

Guest joelee
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

fso.MoveFile "testfile.txt", "myfile.txt"

And a quick tip, make sure you "somefile.close" the file if you've been writing to it earlier in the code otherwise you'll get a "permission denied" error...

Link to comment
Share on other sites

  • 3 months later...

Question..

How do you move more than one file to another dir,

for example

from

C:\dir\*.doc

to

c:\dir\documents\*.doc

[Edit]

Never mind already got it,

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
fso.MoveFile "*.mp3", "D:\music\mp3\"

But what to do when you want to check first if there is any mp3 file there?

[/Edit]

Edited by Tensity
Link to comment
Share on other sites

Here This Searches For All The WMA Files Then Copy Them To %systemDrive%\MusicTemp

To search For Other File Type Change The RED Text

To Change The Location Where The Files Goes Change The Green Text

Dim Fso : Set Fso = CreateObject("Scripting.FileSystemObject")

Dim Act : Set Act = CreateObject("Wscript.shell")

Dim Sd : Sd = Act.ExpandEnvironmentStrings("%systemDrive%")

Dim Name, Path, TheFile , INTA

INTA = 1-1

strComputer = "."

If Not Fso.FolderExists(Sd & "\MusicTemp") Then

Fso.CreateFolder(Sd & "\MusicTemp")

End If

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

Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile Where Extension = 'wma'")

If colFiles.Count = 0 Then

Act.popup "No File With That Extention Was Found",4,"InValid File Type", 0 + 32

    Wscript.Quit

End If

For Each objFile in colFiles

INTA = INTA + 1

  Name = Ucase(objFile.FileName & "." & objFile.Extension)

  Path = UCase(objFile.Drive & objFile.Path)

  TheFile = KbPath & Kbname

Fso.CopyFile(Path & Name), (Sd & "\MusicTemp\" & Name)

Act.Popup "Completed The Copy Of This" & vbCrLf & Path & Name & vbCrLf & "The File Count = " & INTA, 2, "Gsm Copy File", 0 + 32

Next

Act.Popup "Completed Search And Copy" & vbCrLf & "Total Amount Of Files Moved  = " & INTA, 5, "Gsm Search And Copy"

Fix A Extra quote I Had in The script

Edited by gunsmokingman
Link to comment
Share on other sites

Hey Gunsmokingman, thank you for your quick reply.

Here This Searches For All The WMA Files Then Copy Them To %systemDrive%\MusicTemp

It is a nice script, however I do not want to search the whole system, but only one specific location. How do I integrate that into your script. Also I need to replace copy for move, because I want to move the files and not copy them.

Any suggestions?

:hello:

Link to comment
Share on other sites

Red Is What You Will Have To Fill In

Blue Is A Check To Make Sure You Have A Folder To Copy To

Orange The Error Message

Green Is The First Checks Script

Dim Fso : Set Fso = CreateObject("Scripting.FileSystemObject")

Dim Act : Set Act = CreateObject("Wscript.shell")

Dim Sd : Sd = Act.ExpandEnvironmentStrings("%systemDrive%")

If Fso.FolderExists( "PLACE THE FOLDER LOCATION HERE") Then

If Not Fso.FolderExists(SD & "\MusicTemp") Then Fso.CreateFolder(SD & "\MusicTemp") Else On Error Resume Next End If

Fso.MoveFile( "PLACE THE FOLDER LOCATION HERE AND FILE ")  , (SD & "\MusicTemp")

Else

Act.popup "There Was No Folder To Copy From", 5, 0 + 32, "Missing"

End If

Link to comment
Share on other sites

  • 3 years later...

I have a small and quick question....

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

fso.MoveFile "testfile.txt", "myfile.txt"

first the script works is simple and does it's job pretty fine but... how can a rename a file after the current date and add the .rar extension (ex. 2008.08.10.rar) ?

Thank you.

Edited by RainGigel
Link to comment
Share on other sites

This add the Date to the file, as to the rar part you could just change the ".txt" to ".rar"

but this would not be a real rar file.

  Dim fso, D1
Set fso = CreateObject("Scripting.FileSystemObject")
D1 = Replace(Date,"/",".")
fso.MoveFile "testfile.txt", "myfile_" & D1 & ".txt"

Link to comment
Share on other sites

Just in case of date format differences you may need

Set objFSO = CreateObject("Scripting.FileSystemObject")
N=Now
strFNA = Right(Year(N),4) & "." & Right(100+Month(N),2) & "." & Right(100+Day(N),2)
objFSO.MoveFile "testfile.txt", strFNA & ".rar"

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...