Guest joelee Posted April 11, 2005 Share Posted April 11, 2005 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, bolFileExistsSet 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 More sharing options...
DiGGiTY Posted April 11, 2005 Share Posted April 11, 2005 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 errResultNext Link to comment Share on other sites More sharing options...
Guest joelee Posted April 11, 2005 Share Posted April 11, 2005 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... Link to comment Share on other sites More sharing options...
Guest joelee Posted April 11, 2005 Share Posted April 11, 2005 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 More sharing options...
IcemanND Posted April 11, 2005 Share Posted April 11, 2005 Dim fsoSet fso = CreateObject("Scripting.FileSystemObject")fso.MoveFile "testfile.txt", "myfile.txt" Link to comment Share on other sites More sharing options...
gunsmokingman Posted April 11, 2005 Share Posted April 11, 2005 Thanks That Was Usefull To Me Link to comment Share on other sites More sharing options...
Guest joelee Posted April 12, 2005 Share Posted April 12, 2005 Dim fsoSet 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... Link to comment Share on other sites More sharing options...
Tensity Posted July 13, 2005 Share Posted July 13, 2005 (edited) Question..How do you move more than one file to another dir,for examplefrom C:\dir\*.docto c:\dir\documents\*.doc[Edit]Never mind already got it,Dim fsoSet 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 July 13, 2005 by Tensity Link to comment Share on other sites More sharing options...
gunsmokingman Posted July 13, 2005 Share Posted July 13, 2005 (edited) Here This Searches For All The WMA Files Then Copy Them To %systemDrive%\MusicTempTo search For Other File Type Change The RED Text To Change The Location Where The Files Goes Change The Green TextDim Fso : Set Fso = CreateObject("Scripting.FileSystemObject")Dim Act : Set Act = CreateObject("Wscript.shell")Dim Sd : Sd = Act.ExpandEnvironmentStrings("%systemDrive%")Dim Name, Path, TheFile , INTAINTA = 1-1strComputer = "."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.QuitEnd IfFor Each objFile in colFilesINTA = INTA + 1 Name = Ucase(objFile.FileName & "." & objFile.Extension) Path = UCase(objFile.Drive & objFile.Path) TheFile = KbPath & KbnameFso.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 NextAct.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 July 14, 2005 by gunsmokingman Link to comment Share on other sites More sharing options...
Tensity Posted July 14, 2005 Share Posted July 14, 2005 Hey Gunsmokingman, thank you for your quick reply.Here This Searches For All The WMA Files Then Copy Them To %systemDrive%\MusicTempIt 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? Link to comment Share on other sites More sharing options...
gunsmokingman Posted July 14, 2005 Share Posted July 14, 2005 Red Is What You Will Have To Fill In Blue Is A Check To Make Sure You Have A Folder To Copy ToOrange The Error Message Green Is The First Checks ScriptDim 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") ThenIf Not Fso.FolderExists(SD & "\MusicTemp") Then Fso.CreateFolder(SD & "\MusicTemp") Else On Error Resume Next End IfFso.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 More sharing options...
Fredledingue Posted July 16, 2005 Share Posted July 16, 2005 Is also working to move or rename a file'--------------------Set fso = CreateObject("Scripting.FileSystemObject")Set aFile = fso.GetFile("output.dat")aFile.Move "intput.bak" Link to comment Share on other sites More sharing options...
RainGigel Posted August 14, 2008 Share Posted August 14, 2008 (edited) I have a small and quick question....Dim fsoSet 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 August 14, 2008 by RainGigel Link to comment Share on other sites More sharing options...
gunsmokingman Posted August 14, 2008 Share Posted August 14, 2008 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 More sharing options...
Yzöwl Posted August 14, 2008 Share Posted August 14, 2008 Just in case of date format differences you may needSet objFSO = CreateObject("Scripting.FileSystemObject")N=NowstrFNA = 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now