Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account


renaming a file using VBS?

- - - - -

  • Please log in to reply
15 replies to this topic

#1
Guest_joelee_*

Guest_joelee_*
  • Guests
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"



How to remove advertisement from MSFN

#2
DiGGiTY

DiGGiTY

    Member

  • Member
  • PipPip
  • 159 posts
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
Posted Image

#3
Guest_joelee_*

Guest_joelee_*
  • Guests
I got the same code from http://msdn.microsof...and_folders.asp as well.

But to just rename a single file takes half a dozen lines of code?
Thanks any way...

#4
Guest_joelee_*

Guest_joelee_*
  • Guests
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?

#5
IcemanND

IcemanND

    MSFN Junkie

  • Super Moderator
  • 3,266 posts
  • OS:Windows 7 x64
  • Country: Country Flag
Dim fso

Set fso = CreateObject("Scripting.FileSystemObject")



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


#6
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,418 posts
  • OS:none specified
  • Country: Country Flag
Thanks That Was Usefull To Me


GunSmokingMan



#7
Guest_joelee_*

Guest_joelee_*
  • Guests

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

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

<{POST_SNAPBACK}>


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...

#8
Tensity

Tensity
  • Member
  • 3 posts
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, 13 July 2005 - 05:26 AM.


#9
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,418 posts
  • OS:none specified
  • Country: Country Flag
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, 13 July 2005 - 09:17 PM.



GunSmokingMan



#10
Tensity

Tensity
  • Member
  • 3 posts
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:

#11
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,418 posts
  • OS:none specified
  • Country: Country Flag
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




GunSmokingMan



#12
Fredledingue

Fredledingue

    MSFN Expert

  • Member
  • PipPipPipPipPipPip
  • 1,267 posts
  • OS:98SE
  • Country: Country Flag
Is also working to move or rename a file

'--------------------
Set fso = CreateObject("Scripting.FileSystemObject")
Set aFile = fso.GetFile("output.dat")
aFile.Move "intput.bak"

HTASoft.com

superchargedwindows9xig1.png
Still Using W98SE+++ ...Daily.

#13
RainGigel

RainGigel
  • Member
  • 2 posts
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, 14 August 2008 - 02:04 AM.


#14
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,418 posts
  • OS:none specified
  • Country: Country Flag
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"




GunSmokingMan



#15
Yzöwl

Yzöwl

    Wise Owl

  • Super Moderator
  • 4,527 posts
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

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"


#16
RainGigel

RainGigel
  • Member
  • 2 posts
Thanks a bunch dude.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users



How to remove advertisement from MSFN