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!

Time format 1

Status
Not open for further replies.

gmagerr

Technical User
Aug 11, 2001
323
US
I am trying to rename files in folders with the name of the folders in which they reside, the date and time and some other things. I would like the time to read 05.03.22 AM and it's coming up 5.3.22 AM here's the code, can someone assist with the time portion?

Thanks

Code:
Call RenameAllFiles("C:\Test")

Sub RenameAllFiles(strFolder)
  Dim fso, file, folder
  Set fso = CreateObject("Scripting.FileSystemObject")

  ' Check for AVIs to rename.
  For Each file in fso.GetFolder(strFolder).Files
    'If Right(file.Name, 4) = ".avi" Then
      Call RenameFile(strFolder & "\" & file.Name)
    'End If
  Next

  ' Check for SubFolders to recurse into.
  For Each folder in fso.GetFolder(strFolder).SubFolders
    Call RenameAllFiles(strFolder & "\" & folder.Name)
  Next
End Sub

Sub RenameFile(strFileName)
  Dim fso, strExt, strFolder, strNewFileName, t, p, f
  Set fso = CreateObject("Scripting.FileSystemobject")

  t = Month(Date) & "." & Day(Date) & "." & Year(Date) & "  " & Hour(Time) & "." & Minute(Time) & "." & Second(Time)
  p = "_PPW-OFU_"
  f = fso.GetFileName(strFileName)
  
  ' Note the extension (should be avi)
  strExt = fso.GetExtensionName(strFileName)

  ' Derive the full path to the folder.
  strFolder = fso.GetParentFolderName(strFileName)
  
  ' Derive the new filename.
  strNewFileName = strFolder & "\" & t & p & fso.GetBaseName(strFolder) & "-" & f '& "." & strExt

  ' Do the rename.
  If strFileName <> strNewFileName Then
    WScript.Echo "Renaming " & strFileName & " to " & strNewFileName
    fso.MoveFile strFileName, strNewFileName
  End If
End Sub
 
Hi,

Check out the assignment of variable t.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
A starting point:
Right("0" & Hour(Time), 2)

Do the same for Month, Day, Minute and Second

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Skip,

I'm not sure what you mean by check out the assignment of variable t? Variable t is producing the 5.3.22 AM and I need it to read 05.03.22 AM. How can I format the Hour(Time) Minute(Time) and Second(Time) to do that?

Thanks
 
PHV,

Thanks, that did it. I also needed to format the date the same way. Your suggestion worked on the date too.

Thanks
 
I have another problem. If I run the script twice, it doubles all of the stuff i'm adding the first time. Anyway to check if PPW-OFU is in the filename, if it is just skip renaming that file? I want to rename only files with their original filename

Thanks
 
Ok, i figured out how to do an instr so I don't double name the files. Last question
In my t variable, how can I add AM or PM to the time?

Thanks
 
If you were to implement my VBscript formatting function as per thread329-1570966 then your code could become:

Code:
t = vbsFormat(Now, "MM.dd.yyyy hh.mm.ss tt")

(.Net formatting capabilities, which this uses, are somewhat more extensive than that of VB or VBA. The Related Topics section at the bottom of this page provides links to all the custom formatting you can use)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top