Help - Search - Members - Calendar
Full Version: renaming a file using VBS?
MSFN Forums > Coding, Scripting and Servers > Programming (C++, Delphi, VB, etc.)

   
Google Internet Forums Unattended CD/DVD Guide
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 !

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

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

Name SomeFile As "testfile_new.txt"
DiGGiTY
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
joelee
I got the same code from http://msdn.microsoft.com/library/default....and_folders.asp as well.

But to just rename a single file takes half a dozen lines of code?
Thanks any way...
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?
IcemanND
CODE
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

fso.MoveFile "testfile.txt", "myfile.txt"
gunsmokingman
Thanks That Was Usefull To Me
joelee
QUOTE (IcemanND @ Apr 11 2005, 05:04 PM)
CODE
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...
Tensity
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,

CODE
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]
gunsmokingman
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
QUOTE


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
Tensity
Hey Gunsmokingman, thank you for your quick reply.

QUOTE
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?

welcome.gif
gunsmokingman
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
QUOTE
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
Fredledingue
Is also working to move or rename a file

'--------------------
Set fso = CreateObject("Scripting.FileSystemObject")
Set aFile = fso.GetFile("output.dat")
aFile.Move "intput.bak"
RainGigel
I have a small and quick question....

CODE
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.
gunsmokingman
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.
QUOTE
CODE
  Dim fso, D1
  Set fso = CreateObject("Scripting.FileSystemObject")
   D1 = Replace(Date,"/",".")
   fso.MoveFile "testfile.txt", "myfile_" & D1 & ".txt"
Yzöwl
Just in case of date format differences you may need
CODE
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"
RainGigel
Thanks a bunch dude.
Google Internet Forums Unattended CD/DVD Guide
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.