Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do i rename a file in vb script 1

Status
Not open for further replies.

vikita

Technical User
Oct 1, 2005
6
KE
Hi i am not a programmer but this site has been very helpful to me.

I need to rename a file that is created by a process called end of day.
The file is as follows after creation
LOA070402.zip
where 07-year, 04-month, 02- date.

So the date flag changes by each day...

The file should then be renamed to
POSSLSTRS_70_7030_070402082535.zip

again there is the date flag that is picked from the previous file name. The problem with picking the date flag from the system is that the end of day process could take long and jump to the next day, hence the filename would be wrong. The 082535 is just a counter but does not change.

I need a vb script that will do this so that i can automate the renaming process.

I tried using a batch file as follows;

ren "C:\LOA??????.zip" POSSLSTRS_70_7030_??????082535.zip

but it does not work.

Any help would be greatly appreciated!
 
you really don't have a "rename" method in vbscript. you have to use "move" method to accomplish this.
move "old_file" to "new_file" will do it.
Code:
overwrite = 1
fso.Movefile oldfile, newfile, overwrite
 
You can rename it with something like this.

Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFile : Set objFile = objFSO.GetFile("C:\temp\somefile.txt")
objFile.Name = "somefile2.txt"

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
This worked for me :
ren LOA??????.zip POSSLSTRS_70_7030_??????082535.zip

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
dm4ever, your code would work only if the filename wasnt changing. but in this scenario it changes depending on the date flag the file is given by the process.

PHV, do i run this code as a batch file. When i do,
there is no difference as i think what happens is that it
counts from the 4th character and starts placing the variables from there.But since there are distinct values already from the 4th character it does not work.
however something like this would work

ren LOA??????.zip POS??????SLSTRS_70_7030_082535.zip

but that is not what we want.
Or could there be anything wrong with my environment?

jfdabiri kindly elaborate more.

 
Code:
Dim objFSO 
Dim objFile 
old_file = "c:\my_folder\some_file.txt"
new_file = "c:\my_folder\some_yyyymmdd_file.txt"
Set fso  = CreateObject("Scripting.FileSystemObject")
fso.Movefile old_file, new_file
tested.
sorry, overwrite is not a parm for movefile method.
the yyyymmdd part can be constructed from date.
 
jfdabiri, the code can rename a file alright but...
from the scenario i gave...this file changes the name with every new date...so it would not give the expected results.

I did some research and landed on using regular expressions in VBS. as this would replace the string LOA with the string POSSLSTRS_70_7030_... but i have no idea of how to go about this.
any ideas?
 
ren LOA??????.zip POS??????SLSTRS_70_7030_082535.zip
when you say the file name changes, what is the pattern?

what does the ? represent? date? or what?
in any case, you can construct file name just like any other string. you have to know the file name. or its pattern.
 
LOA??????.zip POS??????SLSTRS_70_7030_082535.zip
here's the code:
Code:
y = right(year(date()), 2) 
m = right("0" & month(date()), 2)
d = right("0" & day(date()), 2)
' this will give you 2 digit day, month, year

the_date = Y & m & d
' this is your 6 char date

old_file = "LOA" & the_date & ".zip"
new_file = "POS" & the_date & "SLSTRS_70_7030_082535.zip"

old_file = "c:\my_folder\" & old_file
new_file = "c:\my_folder\" & new_file
Set fso  = CreateObject("Scripting.FileSystemObject")
fso.Movefile old_file, new_file



 
? is a special character.

the following code renames the file but just a small hick up on results:

strComputer = "."

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

Set colFiles = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='C:\Folder'} Where " _
& "ResultClass = CIM_DataFile")

For Each objFile In colFiles
strDate = Right(objFile.Name, 10)
strNewName = objFile.Drive & objFile.Path & "POSSLSTRS_70_7030_" & strDate &

"082535.zip"
errResult = objFile.Rename(strNewName)
Next

It works fine but the output of the filename is
POSSLSTRS_70_7030_070402.zip082535.zip

I dont know which correct function would trim the .zip string from LOA070402.zip string

Any help


 
whole_file_name = "LOA070402.zip"

this would trim that:


each_part = split(file_name, ".") ' ===> LOA070402 & zip
short_file_name = each_part(0) ' ===> LOA070402
file_extention = each_part(1) ' ===> zip
the_date = right(short_file_name, 6) ' ====> 070402
 
jfdabiri, thanx a bunch worked perfectly.....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top