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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

renaming files 1

Status
Not open for further replies.

7Star

Technical User
Nov 17, 2005
13
SE
Hi,

I was trying to create a script that renames .jpg files based on the last modified date. I found a script at microsofts scriptcenter:
Code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set FileList = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='e:\bilder\test'} Where " _
        & "ResultClass = CIM_DataFile")

For Each objFile In FileList
    strDate = Left(objFile.CreationDate, 12)
    strNewName = objFile.Drive & objFile.Path & _
       strDate & "." & "jpg"
    strNameCheck = Replace(strNewName, "\", "\\")

    i = 1
    Do While True
        Set colFiles = objWMIService.ExecQuery _
            ("Select * from Cim_Datafile Where Name = '" & strNameCheck & "'")
        If colFiles.Count = 0 Then
            errResult = objFile.Rename(strNewName)
            Exit Do
        Else
            i = i + 1
            strNewName = objFile.Drive & objFile.Path & _
                strDate & "_" & i & "." & "jpg"
            strNameCheck = Replace(strNewName, "\", "\\")
        End If
    Loop
Next
But this script renames all files in the specified folder and based on the creation date. Could you give me some suggestions to modify this script or how to make a new one that rename my jpg files based on the last modified date.
 
Use objFile.LastModified and objFile.Extension

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I'm not so good at vbscript, how do I use objfile.extension and where should I use it in the script? Instead of the * in the select query?
 
If you use high-power component, you should always check its properties and methods spec. I have not looked into too much your script. I would consider these.
[1]
>[tt]strDate = Left(objFile.CreationDate, 12)[/tt]
[tt]strDate = Left(objFile.[blue]LastModified[/blue], 12)[/tt]
[2]
If the directory contains all sorts of files with different extension and you want to target only to jpg file, you have to tighten up the control.
[tt]
For Each objFile In FileList
[blue]if strcomp(objFile.extension,"jpg",1)=0 then[/blue]
'the rest unchanged
[blue]end if[/blue]
Next
[/tt]
 
Thanks a lot, it works just fine now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top