Welcome to MSFN

Register now to gain access to all of our features. Once registered and logged in, you will be able to contribute to this site by submitting your own content or replying to existing content. You'll be able to customize your profile, receive reputation points as a reward for submitting content, while also communicating with other members via your own private inbox, plus much more! This message will be removed once you have signed in.


Guest joelee

renaming a file using VBS?

16 posts in this topic

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"

0

Share this post


Link to post
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

0

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

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

0

Share this post


Link to post
Share on other sites
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...

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
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
0

Share this post


Link to post
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:

0

Share this post


Link to post
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

0

Share this post


Link to post
Share on other sites

Is also working to move or rename a file

'--------------------

Set fso = CreateObject("Scripting.FileSystemObject")

Set aFile = fso.GetFile("output.dat")

aFile.Move "intput.bak"

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
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"

0

Share this post


Link to post
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"

0

Share this post


Link to post
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.