Link to home
Start Free TrialLog in
Avatar of redmanjb
redmanjb

asked on

Need help writing VBScript to rename files to date/time stamp

Hello all!  I have a folder called "Data", which contain our "project folders", as well as some others.  All of the Project Folders are named something like "2205009".  Within each of these project folders are 5 folders "Contracts", "Correspondence", "Documents", "Graphics", and "Reports".  I have a batch file that will scan all of the inboxes on our mail server, and will move the actual email files ( the email files have a ".mai" extension) to the appropriate "Correspondence" folder.  

By the way, if I am sending an email to someone concerning Project #2205009, I will, in the email message, put in the CC field "2205009@mydomain.com", which will drop that email file into that project number's mailbox.  This way, all correspondence about a particular project will be easily accessible to everyone, and we don't have to worry about who wrote what about a particular project, all the correspondence will be in one folder.

The batch file works wonderfully, however, I'm still left with these email files that have names with absolutely no meaning whatsoever.  For instance, one of the email files is named "43B7E3E4C6744CC6AE23437B54233A.MAI".  I would like to have a script that will search within all of the Correspondence folders (which again are located in each of the Project Folders), and will rename only files that have the ".mai" extension to something like 040605_090354.mai, which would represent date_time in this format: mm/dd/yy_hh:mm:ss in 24-hour time.  Is this possible?  

I read in a PC Magazine article (http://www.pcmag.com/article2/0%2C1759%2C1386947%2C00.asp), where this can be done in a batch file.  I was going to try to incorporate what it says in that article into my existing batch file, but it will not display seconds.  And there would be times when multiple emails about a particular project are sent by multiple people, possibly within the same minute, so there would be some issues there.  So I would need either to show seconds in the file name, or which would rename the files incrementally such as 040605_0903a, 040605_0903b, 040605_0903c, etc.

Can anyone help?  :)
Avatar of Shane Russell
Shane Russell
Flag of United Kingdom of Great Britain and Northern Ireland image

ok here is a script that renames image files so basically all you need to do is ammend it so that it renames them to .mai type of files as well as assigning the different sections of the dates and times to variables and then using the variables in the renaming section of coding :

Here is the script

'=============================

Dim strFile, strRootFolder,strFilename, strNewFilename, objFSO, cnt

Set objFSO = CreateObject("Scripting.FileSystemObject")

'Get the folder object associated with the directory
Dim objFolder
Set objFolder = objFSO.GetFolder("D:\Test")  '<-- change this to the directory where your images are

WScript.Echo "The files found in " & objFolder.Name

strRootFolder = Left( WScript.ScriptFullName, InStr( WScript.ScriptFullName, WScript.ScriptName ) - 1)
WScript.Echo "Root Folder:  " & strRootFolder

Set objFSO = CreateObject( "Scripting.FileSystemObject" )

'Loop through the Files collection
Dim objFile

For Each objFile in objFolder.Files
  WScript.Echo objFile.Name
i = i + 1
If objFile.Name = "Image_" & i & Right(objFile.Name,3) Then
else
objFSO.MoveFile objFile, objFolder & "\" & "Image_" & i &".jpg"  '< -- change this to the file extension you
                                                                                                      'want to save your images as!
WScript.Echo "File renamed successfully!"
End If
Next

'Clean up!
Set objFolder = Nothing
Set objFile = Nothing
Set objFSO = Nothing

'=============================

I will work on the script now to adjust it to exactly what you want and will re post a mod of it in a while or later on

I hope this gets you started at least
Avatar of redmanjb
redmanjb

ASKER

Wonderful!  Thank you so much for the quick reply! :)

Now what could maybe be an issue is the locations of the Files.  I guess the script would have to search through every project folder for a folder called "correspondence", where the *.mai's would be located in.

There are mai files here--                              \\data\2205009-->Correspondence-->314MN0C0932JM093290F.mai
There are mai files here--                              \\data\2206122-->Correspondence-->42MG0930MDGM009JJJU8.mai
There are no mai files here--                         \\data\Marketing-->

So basically it would have to search through the data folder, and find the correspondence folder (if one exists), within the project folder, correct?
         \\data\*****\Correspondence

And the other potential issue is that it would not try to rename a file that has already been renamed.  Could VBScript handle those 2 issues?  And could it incrementally alter the names of files if one has the same time stamp (mm/dd/yy_hh:mm), or can VBScript actually pull out the seconds on a time stamp (mm/dd/yy_hh:mm:ss), in which case incremental numbering wouldn't be necessary.
Here is the altered script :)

'=====================

Dim strFile, strRootFolder,strFilename, strNewFilename, objFSO, cnt, FileExt

Set objFSO = CreateObject("Scripting.FileSystemObject")

'Get the folder object associated with the directory
Dim objFolder
Set objFolder = objFSO.GetFolder("c:\Test")  '<-- change this to the directory where your images are

WScript.Echo "The files found in " & objFolder.Name

strRootFolder = Left( WScript.ScriptFullName, InStr( WScript.ScriptFullName, WScript.ScriptName ) - 1)
WScript.Echo "Root Folder:  " & strRootFolder

Set objFSO = CreateObject( "Scripting.FileSystemObject" )

'Loop through the Files collection
Dim objFile

Dim time
Dim date

Dim altTime
Dim altDate

date = FormatDateTime(Now(), vbShortDate)


time = FormatDateTime(Now(), vbLongTime)

altTime = Replace(time, ":", "")
altDate = Replace(date, "/", "")
WScript.Echo altDate & " Date"
WScript.Echo altTime & " Time"


For Each objFile in objFolder.Files
  WScript.Echo objFile.Name
FileExt = Right(objFile.Name,3)
If FileExt = "mai" Then
objFSO.MoveFile objFile, objFolder & "\" & altDate & "_" & altTime &".mai"

else
WScript.Echo objFile.Name                                                                                            
WScript.Echo "File renamed successfully!"
End If
Next

'Clean up!
Set objFolder = Nothing
Set objFile = Nothing
Set objFSO = Nothing
You could use the functions in vbscript to get the seconds ie left or right or mid or something along those lines to actually pull out the seconds like you said. With regards to checking to see if it has been renamed already and not renaming the ones that have been re named , that would be harder.

Also with regards to finding that folder, would the folder be located on your local machine or is it located on a server or what ?
the script I posted will just go through every file and if it's extension is mai then it will rename it with todays date and time like you wanted, so you would obviously need some checking or testing before you renamed the files !!
Sorry for all the posts ! However going through each of the folders would require you to loop through all the sub folders within a particular directory.

Here is a url which is simmiliar to what you are wanting to do ( The URL shows you how to delete files that are older then X amount of days within one folder or one folder and all its sub folders depending on a variable that you set from what I could make of it )

http://www.codecomments.com/archive299-2005-2-402869.html

Then there is this on devx which shows you how to loop through sub directories in vbscript and basically do the same thing :

http://www.devx.com/getHelpOn/10MinuteSolution/20406/1763

Hopefully this points you in the correct direction with regards to looping through your folders and or sub folders to find the correspondance folder(s).

Also take a look here :

http://www.juicystudio.com/tutorial/asp/vbscript.asp#writing

Especially this one :

http://www.juicystudio.com/tutorial/asp/files.asp

The one above shows you how to use the FolderExists function that is apart of the FSO ( File System Object )

that will allow you to determine if the correspondance folder exists within the sub directorys :)
Wow!  Thanks again for your responses!  The script will reside on the same computer as the data is, and it could be on the same drive, doesn't really matter to me.  I pretty much know nothing about VBScript, or any other programming language for that matter, but I'll try to go through this all to see if I can come up with something.  I'm also going to search some more to see if anyone has done anything similar.
your welcome, I just dont have time right now to help you with the remainder of looping through the sub directories or checking to see if the file or files already have the type of naming convention and if so not to re name them and if not then to rename them.

Give me a day or two and if you have not figured it out by then , then I will go through it all and ammend my 2nd script to fit your requirements :)

Kind regards

Gecko

P.S Sorry I cant help you out right this minute, other then that I am sure another expert will post a more precise script for you or something along those lines :)
ok with regards to testing if it has been renamed already into the format you would like, you could test the length of the filename ie when you have something like below :

040605_090354.mai

The length will always be 17 in length including the . ("dot") and the file extension (mai) as well as the file name, so if it is longer then 17 chars, then to rename it. With regards to them being in different folders you would have to go through each of the folders to check to see if it was the Correspondance folder or not and if it was then to do the same for that folder as well.

I will attempt to work on it now, however I have found this site which gives you examples of how to go through folders, sub folders , etc. :

http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/filesfolders/folders/#UseBrowser.htm

Also if you can keep me updated on what is going on with regards to this question so that I am aware of what is going on , that would be very much appreciated !

Kind regards

Gecko
ok try this out and let me know how it goes ( It is a little messy ) :

'==================================

Dim NewSubFolder, corrFolders

Set FSO = CreateObject("Scripting.FileSystemObject")
Set oOutFile = FSO.CreateTextFile("c:\Test.txt")
ShowSubfolders FSO.GetFolder("C:\")

Sub ShowSubFolders(Folder)
    For Each Subfolder in Folder.SubFolders
            If IsNumeric(FSO.GetFileName(Subfolder.Path)) = True Then
            wscript.echo Subfolder.Path
            NewSubFolder = Subfolder.Path


Set objFolder = FSO.GetFolder(NewSubFolder)
Set colSubfolders = objFolder.Subfolders
For Each objSubfolder in colSubfolders
    Wscript.Echo objSubfolder.Name ', objSubfolder.Size
corrFolders = objSubfolder
'Looping through all the files
'Loop through the Files collection

Dim objFolder
Set objFolder = FSO.GetFolder(corrFolders)
Dim objFile

Dim time
Dim date

Dim altTime
Dim altDate

date = FormatDateTime(Now(), vbShortDate)


time = FormatDateTime(Now(), vbLongTime)

altTime = Replace(time, ":", "")
altDate = Replace(date, "/", "")
WScript.Echo altDate & " Date"
WScript.Echo altTime & " Time"


For Each objFile in objFolder.Files
  'WScript.Echo objFile.Name
FileExt = Right(objFile.Name,3)
If FileExt = "mai" And Len(objFile.Name) > 17 Then
wscript.echo Len(objFile.Name) & " Length of Files Name"
FSO.MoveFile objFile, objFolder & "\" & altDate & "_" & altTime &".mai"

else
WScript.Echo objFile.Name                                                                                            
WScript.Echo "File renamed successfully!"
End If
Next


Next

            End if
    Next
End Sub


'Clean up!
Set objFolder = Nothing
Set objFile = Nothing
Set FSO = Nothing

'===============================

Let me know how that goes or if that works for you , it obviously includes the date / time format as you requested ! ie

02042005_181158.mai


I tested that script out and it will need altering slightly just to change the Len check from > 17 to >19 or 20 or something like that , unless you can think of a better way to test if it has been re named already or not, then just use that instead of the Len check, the Len aka Length check is basically to test the length of the file name and if it is longer then a certain amount it will rename the ones in there provided they are of mai file types as well.

If you need further help with this post back and let me know :)

BTW Sorry for all of the posts made by myself !

Kind regards

Gecko
Hi Gecko!  Sorry I haven't responded in the last few days, I've been out of town.  Thank you so much for your hard work, you're awesome!

I apologize, but I'm a little confused.  Would I run this script on the root folder "data"?  And it just searches through all the folders, renaming the "mai" files to the date/time format correct?  And shouldn't the Len check of >17 characters be sufficient, since the original name of the file is something like 24 characters, and the new file name is 19?

Also, what does the statement below signify?
     Set oOutFile = FSO.CreateTextFile("c:\Test.txt")
     ShowSubfolders FSO.GetFolder("C:\")

Does it output the results to the test.txt file?
And what does the 2nd line represent?  Do I need to modify the script to show that it should look in the "c:\data" folder?

BTW, I'm setting up a test environment today.  Again, thank you so much for your help! :)
1.} I did it where by I had the script on the desktop and I created two folders one called 200 and one called 201, so basically the way I made it is it searches for folders which is made up of numbers so it will find all of the student id folders and then from there it looks underneath each of these folders and attains the mai file types out of the folders providing they are longer then 17 in length because :

02042005_181158.mai

is 19 in length, so I said if you change that so that it is greater then 19 or 20 in length, another way to check to see if you need to rename it would be to test the first part of the filesname to see if it is equal to todays date, if so then not to rename it and if it wasnt then to rename it. I can make that ammendment to the script for you if you would like ?

2}

     Set oOutFile = FSO.CreateTextFile("c:\Test.txt")
     ShowSubfolders FSO.GetFolder("C:\")

First one creates a text file called Test.txt on the root of the C drive
Second one calls the ShowSubfolders Function and sets the area of the drive to search as the C Drive, so basically it loops through all of the folders on the C drive. If you wanted you can alter it to C:\Data or where ever the other sub folders are and test it out.

3} Test it out but make sure the ones it is renaming are just some test ones that you create, ie make another folder called Test, create some blank text files and rename them so that there file type is .mai and then test it out and see what happens :) then from there once you have tested it you can see what you need to do with regards to where to put the script, where you put the script shouldnt really affect how the script works, as long as you have the coding in the script correct ie it knows where to search on your hard drive etc.

If you would like me to alter it to test for the date so that you know to rename it or not because of the date(s) of each of the mai files matching todays date or something along those lines , let me know :)

your welcome for the help

kind regards

gecko
Man you're good!  The script looks great!

We have about 140 gigs worth of data in that "data" folder.  Is there any way for the script to start in the "data" folder, to skip looking in the "project" folder directly underneath, then to read only in the "correspondence" folder.  Like this:

     ShowSubfolders FSO.GetFolder("C:\data\*\correspondence")

It would probably take awhile to search through the entire 140 gigs, plus may tie up a lot of resources while doing so, but the correspondence folders themselves do not have that much data...

Is this possible in vbscript?
You could make it so that it searched for every correspondance folder like so :


 ShowSubfolders FSO.GetFolder("C:\data\Project")  '<-- Or whatever the actual directory was !

Then in the search criteria make it so that it matched up the correspondance folders and went through the correspondance folders to rename the mai files.

If you have a lot of data to go through , not sure if this is viable in vbscript because im fairly certian vbscript does not support API's but if you have visual basic developement enviroment at your disposal, it would be better to actuall make a visual basic 6 or .NET program to do this via API calls using FindNextFile and FindFirstFile !!

Take a look on this site :

http://vbnet.mvps.org/index.html?code/fileapi/folderenumbasic.htm

You can search around on there to give you a better idea of things you can do with regards to the visual basic application, if you have it at your disposal.

If not then you would probably have to use something like this to loop through all the sub folders and check against all of the folders to determine which ones were actuall the correspondance folders (The example below is just to give you an idea ):

Set objFolder = FSO.GetFolder(NewSubFolder)
Set colSubfolders = objFolder.Subfolders
For Each objSubfolder in colSubfolders
    If objSubfolder.Name = "Correspondance" Then
wscript.echo objSubfolder.Name
Next

Anyway let me know how that goes :)

Well there are about 250 project folders, in the data folder, as well as about 100 or so non-project folders.  The project folders are named after the project #s, such as 2010501, 2051212, 4041304, etc., all with 7 digit numbers.  All the non-project folders have different names.  And there are new projects that are created, sometimes 4 or 5 per week.  So I really couldn't update the script each time a new project is created.  Below is the batch file that that copies all of the emails from the project mailboxes to the correspondence folder within the appropriate project folder:

'====================================================================
@echo off

set MailBoxPath=\\COLUMBO\mailroot
set ProjectBase=\\ADAM12\DATA

for /f "delims=" %%p in ('dir %ProjectBase%\* /ad /b') do (
     echo Synchronizing %%p...
     for /f "delims=" %%f in ('dir %MailBoxPath%\%%p\Inbox\*.mai /a-d-h-s /b') do (
         
       IF NOT EXIST "%ProjectBase%\%%p\correspondence\%%f" (
               xcopy "%MailBoxPath%\%%p\Inbox\%%f" "%ProjectBase%\%%p\correspondence\"
               
          )
     )
)

set MailBoxPath=
set ProjectBase=
'====================================================================

and this line here is what actually does the copying, and you can see how it pulls all the files from the "inbox" folders, and puts them in "correspondence":
xcopy "%MailBoxPath%\%%p\Inbox\%%f" "%ProjectBase%\%%p\correspondence\"

There's no way to do a "%%p" type of thing in vbscript?

And by the way, I had originally wanted to just update the batch file to accomplish this task, but found that you can't pull out "seconds" using batch.  I would like to use this vbscript if possible, mainly because you've worked so hard on it :).
I think there is something like that because I remember finding something where it used % in front of what you were looking for. Give me a few mins and I will see if I can find it again ! However here is what I have so far with regards to the script, It does not do the date or time check to see if it needs renamed though because I am not sure of how you want it to go through the files, I mean do you just want to check the current seconds of the file before it sees if it needs renaming or what ?

'--------------------------------------------

Dim NewSubFolder, corrFolders

Set FSO = CreateObject("Scripting.FileSystemObject")
Set oOutFile = FSO.CreateTextFile("c:\Test.txt")
ShowSubfolders FSO.GetFolder("C:\")

Sub ShowSubFolders(Folder)
    For Each Subfolder in Folder.SubFolders
            'If FSO.GetFileName(Subfolder.Path) = "Corr" Then
            'oOutFile.writeline "Corr"
            If IsNumeric(FSO.GetFileName(Subfolder.Path)) = True Then
            wscript.echo Subfolder.Path
            NewSubFolder = Subfolder.Path


Set objFolder = FSO.GetFolder(NewSubFolder)
Set colSubfolders = objFolder.Subfolders
For Each objSubfolder in colSubfolders
    Wscript.Echo objSubfolder.Name ', objSubfolder.Size
corrFolders = objSubfolder
'Looping through all the files
'Loop through the Files collection

Dim objFolder
Set objFolder = FSO.GetFolder(corrFolders)
Dim objFile

Dim time
Dim date

Dim altTime
Dim altDate

date = FormatDateTime(Now(), vbShortDate)


time = FormatDateTime(Now(), vbLongTime)

altTime = Replace(time, ":", "")
altDate = Replace(date, "/", "")
WScript.Echo altDate & " Date"
WScript.Echo altTime & " Time"


For Each objFile in objFolder.Files
  'WScript.Echo objFile.Name
FileExt = Right(objFile.Name,3)
If FileExt = "mai" And Len(objFile.Name) > 17 Then
wscript.echo Len(objFile.Name) & " Length of Files Name"
FSO.MoveFile objFile, objFolder & "\" & altDate & "_" & altTime &".mai"

else
WScript.Echo objFile.Name                                                                                            
WScript.Echo "File renamed successfully!"
End If
Next


Next

            End if
    Next
End Sub


'Clean up!
Set objFolder = Nothing
Set objFile = Nothing
Set FSO = Nothing
'------------------------------------------
I found this with regards to the wild cards thing you were asking :

http://www.visualbasicscript.com/topic.asp?TOPIC_ID=2330

Using Wildcards in a Folder Query

Returns a list of all the folders whose path begins with C:\S.

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFolders = objWMIService.ExecQuery _
    ("Select * from Win32_Directory where Name Like '%c:\\S%'")
For Each objFolder in colFolders
    Wscript.Echo "Name: " & objFolder.Name
Next

from website :

http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/filesfolders/folders/
Thank you much Gecko, you continue to amaze me! :)

I just wanted it to rename the mai files to date and time with seconds so that there will be only a very low possibility of having a duplicate.  It doesn't need to be renamed based on a date/time check, only renamed to the date and time.  It just needs to be renamed if the file name is an mai with over 20 characters...
so your happy with the script or does it need amendments, if so what ammendments ?
Oops, I guess I was typing when you were, and didn't see your post above concerning the wildcards.  The wildcard shown searches through folders beginning with "s", so can having just a "%" serve my purpose of looking past the "data" folder in the example:

c:\data\2021501
c:\data\2021505
c:\data\2021517
c:\data\2021528
c:\data\4031542
c:\data\5031441
Yes, because you could use it within the relevant folder or even if your data folder is within the root folder like you have shown whereby it is :

C:\data\2021501

etc

Then you would have the coding something similiar to what I have posted where it searches for any folder names that include numbers and that would cover you for the part where by it finds all your numerical folders ie

202101, 2021505, etc.The code your probably looking for is :

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFolders = objWMIService.ExecQuery _
    ("Select * from Win32_Directory where Name Like '%c:\\data%'") '<-- I just changed this line of code
For Each objFolder in colFolders
    Wscript.Echo "Name: " & objFolder.Name
Next

Let me know if that works for you ? :)

Kind regards

Gecko
Thank you so much for your patience Gecko, you're awesome!  And I am so sorry for being complicated and for asking so many questions, I'm pretty much clueless.

So the code you just posted will search for the folders that are named numerically, and with the rest of the code, will basically only search for the mai files that are located in the "correspondence" directories?

If so, where, within the main code you posted above, will the last code you posted fit in?  Will it replace some of the code already there?
The code above which has the % just searchs for any string within the folders name(s) and since it has data as the string it will hence find any folder(s) called data.

You would probably have it some where near the begining of the code because all of your paths start with data\{numerical folder}\correspondance\{mai files}

If you have any problems placing the code or would prefer me to re post the next revision of the script, please let me know.

Did you test out my script to see if it found the mai files ? Because looking at the path, I think I might of done some parts the wrong way around, because in my script ive made it search for the correspondance folder, then the numerical folders then from there find the mai files with regards to making sure that there length is correct and that they are a mai file before re naming them.

So make a test directory with the relevant sub directorys like you have posted ie :

 \\data\2205009-->Correspondence-->314MN0C0932JM093290F.mai

Then if my script does not work as you intended let me know.

If you could actually test the script out on a test directory and some made up mai files and let me know how it goes.

I inserted the % part into the script, and this is the script as it exists now...

'==================================

Dim NewSubFolder, corrFolders

Set FSO = CreateObject("Scripting.FileSystemObject")
Set oOutFile = FSO.CreateTextFile("c:\Test.txt")
ShowSubfolders FSO.GetFolder("C:\Data")

Sub ShowSubFolders(Folder)
    For Each Subfolder in Folder.SubFolders
          If IsNumeric(FSO.GetFileName(Subfolder.Path)) = True Then
          wscript.echo Subfolder.Path
          NewSubFolder = Subfolder.Path


Set objFolder = FSO.GetFolder(NewSubFolder)
Set colSubfolders = objFolder.Subfolders
For Each objSubfolder in colSubfolders
    Wscript.Echo objSubfolder.Name ', objSubfolder.Size
corrFolders = objSubfolder
'Looping through all the files
'Loop through the Files collection

Dim objFolder
Set objFolder = FSO.GetFolder(corrFolders)
Dim objFile

Dim time
Dim date

Dim altTime
Dim altDate

date = FormatDateTime(Now(), vbShortDate)


time = FormatDateTime(Now(), vbLongTime)

altTime = Replace(time, ":", "")
altDate = Replace(date, "/", "")
WScript.Echo altDate & " Date"
WScript.Echo altTime & " Time"

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFolders = objWMIService.ExecQuery _
    ("Select * from Win32_Directory where Name Like '%c:\\data%'")
For Each objFile in objFolder.Files
  WScript.Echo objFile.Name
FileExt = Right(objFile.Name,3)
If FileExt = "mai" And Len(objFile.Name) > 17 Then
wscript.echo Len(objFile.Name) & " Length of Files Name"
FSO.MoveFile objFile, objFolder & "\" & altDate & "_" & altTime &".mai"

else
WScript.Echo objFile.Name                                                                                            
WScript.Echo "File renamed successfully!"
End If
Next


Next

          End if
    Next
End Sub


'Clean up!
Set objFolder = Nothing
Set objFile = Nothing
Set FSO = Nothing

'===============================



I ran the script, and output the results to txt.  It seemed like it was working, but it didn't actually change any of the filenames.  It seems as if it's only looking in the project folders as it should.  In my test setup, I also created a few folders that are not project folders (such as "Test", and "Test 123"), and even included the Project subfolders ("Contracts", "Correspondence", "Documents", "Graphics", and "Reports"), and it only looked in the Correspondence folders, it seems.  So everything seems to be working ok, except for the actual renaming of the files.  A portion of the text output is below.  I only included up to the first 2 project folders that it processed, and it didn't even list the non-project folders (the Test and Test 123 folders).  So it's looking good.




Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

C:\data\2010501
CONTRACTS
4112005 Date
42421 PM Time
CORRESPONDENCE
4112005 Date
42422 PM Time
04042005_ 838.MAI
04042005_ 838.MAI
File renamed successfully!
2942EC6543674953AFA6202D3C66B7.MAI
2942EC6543674953AFA6202D3C66B7.MAI
File renamed successfully!
2A98228462D943038F398B7647721B.MAI
2A98228462D943038F398B7647721B.MAI
File renamed successfully!
43B7E3E4C6744CC6AE23437B54233A.MAI
43B7E3E4C6744CC6AE23437B54233A.MAI
File renamed successfully!
794684108F4B48F9AADBC24B51541.MAI
794684108F4B48F9AADBC24B51541.MAI
File renamed successfully!
A954A0BE6FCA4BF98D942DE24AD486.MAI
A954A0BE6FCA4BF98D942DE24AD486.MAI
File renamed successfully!
B171AD03BE344E7096D573F14C3D70.MAI
B171AD03BE344E7096D573F14C3D70.MAI
File renamed successfully!
C9046123958F411A848546EBBF3B6.MAI
C9046123958F411A848546EBBF3B6.MAI
File renamed successfully!
CE95F50FAC8446BD89D1221D551FB9.MAI
CE95F50FAC8446BD89D1221D551FB9.MAI
File renamed successfully!
DC9EB43164B94EB8B12FBF7D97DBDD.MAI
DC9EB43164B94EB8B12FBF7D97DBDD.MAI
File renamed successfully!
DOCUMENTS
4112005 Date
42422 PM Time
GRAPHICS
4112005 Date
42422 PM Time
REPORTS
4112005 Date
42423 PM Time
C:\data\2011502
CONTRACTS
4112005 Date
42423 PM Time
CORRESPONDENCE
4112005 Date
42423 PM Time
04042005_ 838.MAI
04042005_ 838.MAI
File renamed successfully!
2942EC6543674953AFA6202D3C66B7.MAI
2942EC6543674953AFA6202D3C66B7.MAI
File renamed successfully!
2A98228462D943038F398B7647721B.MAI
2A98228462D943038F398B7647721B.MAI
File renamed successfully!
43B7E3E4C6744CC6AE23437B54233A.MAI
43B7E3E4C6744CC6AE23437B54233A.MAI
File renamed successfully!
794684108F4B48F9AADBC24B51541.MAI
794684108F4B48F9AADBC24B51541.MAI
File renamed successfully!
A954A0BE6FCA4BF98D942DE24AD486.MAI
A954A0BE6FCA4BF98D942DE24AD486.MAI
File renamed successfully!
B171AD03BE344E7096D573F14C3D70.MAI
B171AD03BE344E7096D573F14C3D70.MAI
File renamed successfully!
C9046123958F411A848546EBBF3B6.MAI
C9046123958F411A848546EBBF3B6.MAI
File renamed successfully!
CE95F50FAC8446BD89D1221D551FB9.MAI
CE95F50FAC8446BD89D1221D551FB9.MAI
File renamed successfully!
DC9EB43164B94EB8B12FBF7D97DBDD.MAI
DC9EB43164B94EB8B12FBF7D97DBDD.MAI
File renamed successfully!
DOCUMENTS
4112005 Date
42423 PM Time
GRAPHICS
4112005 Date
42423 PM Time
REPORTS
4112005 Date
42423 PM Time
when it runs the script for these lines of code :

If FileExt = "mai" And Len(objFile.Name) > 17 Then
wscript.echo Len(objFile.Name) & " Length of Files Name"
FSO.MoveFile objFile, objFolder & "\" & altDate & "_" & altTime &".mai"

What does the wscript.echo return with regards to the length of each of the file(s) ??

Also what happens when you make it wscript.echo altDate and altTime ie :

wscript.echo altDate
wscript.echo altTime

??

Also change the check so that it checks for MAI as apposed to mai, because the lower case may effect it ie in the if statement I mean so instead of :

If FileExt = "mai" And Len(objFile.Name) > 17 Then

Try

If FileExt = "MAI" And Len(objFile.Name) > 17 Then
Hi Gecko!  I had left work for the day yesterday, right after my last post.  I just tried running the script again, and for some reason it's kind of working now.  I did change the "mai" to "MAI", then I changed it back.  When I type in on the cmd line "cscript.exe test.vbs >scripttest.txt" it replies:

C:\test.vbs(54, 1) Microsoft VBScript runtime error: File already exists

I'm not sure what changed since yesterday, but it at least starts renaming files.  Here is the txt output:

Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

C:\data\2010501
CORRESPONDENCE
4122005 Date
85802 AM Time
4122005_84817 AM.mai
4122005_84817 AM.mai
File renamed successfully!
4122005_84933 AM.mai
4122005_84933 AM.mai
File renamed successfully!
4122005_85239 AM.mai
4122005_85239 AM.mai
File renamed successfully!
4122005_85554 AM.mai
4122005_85554 AM.mai
File renamed successfully!
A954A0BE6FCA4BF98D942DE24AD486.MAI
34 Length of Files Name
B171AD03BE344E7096D573F14C3D70.MAI
34 Length of Files Name




It's looking at the date and time of the folders.  One of the folders did have the same date and time (but probably not in seconds) as one of the emails, but after deleting that folder to see if that was the culprit, no other files/folders have the same data/time, yet what I pasted above is as far as it goes before saying "file already exists".  Any idea?
Here's the files as they now exist in the first folder that is processed :  "C:\data\2010501\CORRESPONDENCE"

4122005_84817 AM.mai                 3/2/2005   5:15 PM
4122005_84933 AM.mai                 1/27/2005 10:51 AM
4122005_85239 AM.mai                 3/1/2005   8:11 AM
4122005_85554 AM.mai                 3/1/2005   7:34 PM
4122005_85802 AM.MAI                 3/2/2005  11:39 AM
B171AD03BE344E7096D573F14C3D70.MAI   2/28/2005  2:12 PM
C9046123958F411A848546EBBF3B6.MAI    3/2/2005   5:14 PM
DC9EB43164B94EB8B12FBF7D97DBDD.MAI   2/28/2005  8:42 AM

Looking at this more carefully, I see that it is renaming the files based on the current time, and not the data/time stamp of the files as it should.  This would be reason it was stopping before, because it tried to process two files in the same second.
I am looking into seeing if the date time stamp thing can be done, I will post back shortly or later on with my findings !

Kind regards

Gecko
Here are a few URL's to show you how to get the date last modified property and possibly a few other things so maybe look into that :)

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/jsprodatelastmodified.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/jsprodatelastmodified.asp

From what I am finding it only looks like you can get date last modified, so not sure if that is of any use to you ? However I only had a quick glimpse so I will come back to this later on tonight as I have a few things that need doing before I sit down and have a closer look into this :)
Sounds great!  I'll take a look at the article, and elsewhere as well.  Thank you so much for your patience and persistence, it is greatly appreciated! :)
That is fine, I just wanted to make sure that you have exactly what you are after !! :) I mean after all this is a paid site ;)

I am in between doing things right now so like I said, I will take a better look at it in a few hours :)
So is it ok that the files are renamed based on the last date they were modified or how exactly do you want this doing taking into consideration those 2 websites I have posted ?? !! ??

Kind regards

Gecko
Hi Gecko!  Well, both of the links you posted were for the same website.  I believe I need the creation date, the date/time stamp of the original "mai" file, as the "last modified date" would probably indicate when the file was renamed, which would be a problem because it would rename files with duplicate names.


Dim NewSubFolder, corrFolders

Set FSO = CreateObject("Scripting.FileSystemObject")
Set oOutFile = FSO.CreateTextFile("c:\Test.txt")
ShowSubfolders FSO.GetFolder("C:\Data")

Sub ShowSubFolders(Folder)
    For Each Subfolder in Folder.SubFolders
          If IsNumeric(FSO.GetFileName(Subfolder.Path)) = True Then
          wscript.echo Subfolder.Path
          NewSubFolder = Subfolder.Path


Set objFolder = FSO.GetFolder(NewSubFolder)
Set colSubfolders = objFolder.Subfolders
For Each objSubfolder in colSubfolders
    Wscript.Echo objSubfolder.Name ', objSubfolder.Size
corrFolders = objSubfolder
'Looping through all the files
'Loop through the Files collection

Dim objFolder
Set objFolder = FSO.GetFolder(corrFolders)
Dim objFile

Dim time
Dim date

Dim altTime
Dim altDate
'FormatDateTime() , vbShortDate) , vbLongTime)
date = Left(ShowFileAccessInfo(filespec),10)


time = Right(ShowFileAccessInfo(filespec),8)

altTime = Replace(time, ":", "")
altDate = Replace(date, "/", "")
WScript.Echo altDate & " Date"
WScript.Echo altTime & " Time"

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFolders = objWMIService.ExecQuery _
    ("Select * from Win32_Directory where Name Like '%c:\\data%'")
For Each objFile in objFolder.Files
  WScript.Echo objFile.Name
FileExt = Right(objFile.Name,3)
If FileExt = "mai" And Len(objFile.Name) > 17 Then
wscript.echo Len(objFile.Name) & " Length of Files Name"
FSO.MoveFile objFile, objFolder & "\" & altDate & "_" & altTime &".mai"

else
WScript.Echo objFile.Name                                                                                            
WScript.Echo "File renamed successfully!"

End If
Next


Next

          End if
    Next
End Sub


'Clean up!
Set objFolder = Nothing
Set objFile = Nothing
Set FSO = Nothing

Function ShowFileAccessInfo(filespec)
   Dim fso, f, s
   Set fso = CreateObject("Scripting.FileSystemObject")
      filespec = "C:\acc.exe"
   Set f = fso.GetFile(filespec)
   's = UCase(filespec) & vbcrlf
   s =  f.DateCreated

   ShowFileAccessInfo = s
End Function
I altered it using that function from the microsoft site which gets the date created , however the message box shows you the name of the file as being weird, yet when you go to see the name of what it actually called the file, it is correct in how it names it from what I can tell , so basically just use the last script I posted unless you have other things included because you said you used that % thing, in that case just ammed your script with what you need from that one above !
Unless you post the whole of your script that you have so far and I can alter it for you with the inclusions that I made in the last script !
Hi there!  Here's the script as I had it before, without the new information you just posted:

'==================================

Dim NewSubFolder, corrFolders

Set FSO = CreateObject("Scripting.FileSystemObject")
Set oOutFile = FSO.CreateTextFile("c:\Test.txt")
ShowSubfolders FSO.GetFolder("C:\Data")

Sub ShowSubFolders(Folder)
    For Each Subfolder in Folder.SubFolders
          If IsNumeric(FSO.GetFileName(Subfolder.Path)) = True Then
          wscript.echo Subfolder.Path
          NewSubFolder = Subfolder.Path


Set objFolder = FSO.GetFolder(NewSubFolder)
Set colSubfolders = objFolder.Subfolders
For Each objSubfolder in colSubfolders
    Wscript.Echo objSubfolder.Name ', objSubfolder.Size
corrFolders = objSubfolder
'Looping through all the files
'Loop through the Files collection

Dim objFolder
Set objFolder = FSO.GetFolder(corrFolders)
Dim objFile

Dim time
Dim date

Dim altTime
Dim altDate

date = FormatDateTime(Now(), vbShortDate)


time = FormatDateTime(Now(), vbLongTime)

altTime = Replace(time, ":", "")
altDate = Replace(date, "/", "")
WScript.Echo altDate & " Date"
WScript.Echo altTime & " Time"

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFolders = objWMIService.ExecQuery _
    ("Select * from Win32_Directory where Name Like '%c:\\data%'")
For Each objFile in objFolder.Files
  WScript.Echo objFile.Name
FileExt = Right(objFile.Name,3)
If FileExt = "MAI" And Len(objFile.Name) > 20 Then
wscript.echo Len(objFile.Name) & " Length of Files Name"
FSO.MoveFile objFile, objFolder & "\" & altDate & "_" & altTime &".MAI"

else
WScript.Echo objFile.Name                                                                                            
WScript.Echo "File renamed successfully!"
End If
Next


Next

          End if
    Next
End Sub


'Clean up!
Set objFolder = Nothing
Set objFile = Nothing
Set FSO = Nothing

'===============================
Dim NewSubFolder, corrFolders

Set FSO = CreateObject("Scripting.FileSystemObject")
Set oOutFile = FSO.CreateTextFile("c:\Test.txt")
ShowSubfolders FSO.GetFolder("C:\Data")

Sub ShowSubFolders(Folder)
    For Each Subfolder in Folder.SubFolders
          If IsNumeric(FSO.GetFileName(Subfolder.Path)) = True Then
          wscript.echo Subfolder.Path
          NewSubFolder = Subfolder.Path


Set objFolder = FSO.GetFolder(NewSubFolder)
Set colSubfolders = objFolder.Subfolders
For Each objSubfolder in colSubfolders
    Wscript.Echo objSubfolder.Name ', objSubfolder.Size
corrFolders = objSubfolder
'Looping through all the files
'Loop through the Files collection

Dim objFolder
Set objFolder = FSO.GetFolder(corrFolders)
Dim objFile

Dim time
Dim date

Dim altTime
Dim altDate

date = Left(ShowFileAccessInfo(filespec),10)


time = Right(ShowFileAccessInfo(filespec),8)

altTime = Replace(time, ":", "")
altDate = Replace(date, "/", "")
WScript.Echo altDate & " Date"
WScript.Echo altTime & " Time"

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFolders = objWMIService.ExecQuery _
    ("Select * from Win32_Directory where Name Like '%c:\\data%'")
For Each objFile in objFolder.Files
  WScript.Echo objFile.Name
FileExt = Right(objFile.Name,3)
If FileExt = "MAI" And Len(objFile.Name) > 20 Then
wscript.echo Len(objFile.Name) & " Length of Files Name"
FSO.MoveFile objFile, objFolder & "\" & altDate & "_" & altTime &".MAI"

else
WScript.Echo objFile.Name                                                                                            
WScript.Echo "File renamed successfully!"
End If
Next


Next

          End if
    Next
End Sub


'Clean up!
Set objFolder = Nothing
Set objFile = Nothing
Set FSO = Nothing

Function ShowFileAccessInfo(filespec)
   Dim fso, f, s
   Set fso = CreateObject("Scripting.FileSystemObject")
     filespec = "C:\acc.exe"
   Set f = fso.GetFile(filespec)
   's = UCase(filespec) & vbcrlf
   s =  f.DateCreated

   ShowFileAccessInfo = s
End Function
Ok there is the script altered, however the message box's say that it renamed the files funny ie it would take part of the date and put it at the beggining of where the time is ie :

140420_ 05 122735.mai

^^^ Something like that, yet when you actually go to the file, it has renamed it correctly ie :

14042005_122735.mai

^^ Basically what ever the creation date and time was without the spaces and numbers being in the wrong places, which I thought was weird, so you might want to take the wscript.echo's out. The 2 that display them are :

date = Left(ShowFileAccessInfo(filespec),10)


time = Right(ShowFileAccessInfo(filespec),8)

altTime = Replace(time, ":", "")
altDate = Replace(date, "/", "")
WScript.Echo altDate & " Date"  '< -- THAT ONE
WScript.Echo altTime & " Time" '<-- THAT ONE

Those are the 2 wscripts you need to remove if you dont want those messages to appear !

Kind regards

Gecko
hmmm...i tried the script (using cscript.exe in the command prompt) as it was above from your 12:53pm post, and I get the reply
c:\test.vbs (77, 4) Microsoft VBScript runtime error: File not found

Any idea what that is referring to?
One of two things :

Either the script called test.vbs does not exist and you called it something simmiliar or its in a different location

OR

The files you are trying to process dont exist

Check the path in the script to make sure you have the correct path assigned ie in this line :

ShowSubfolders FSO.GetFolder("C:\Data")

If that does not help, post back and I will try and help you further :)
I found something else btw :

in the function :

Function ShowFileAccessInfo(filespec)
   Dim fso, f, s
   Set fso = CreateObject("Scripting.FileSystemObject")
     filespec = "C:\acc.exe" '<-- I made it point to this file so you will obviously need to alter this so that it relates to each individual file ie all of your mai files one at a time :) Let me look into this for you :)
   Set f = fso.GetFile(filespec)
   's = UCase(filespec) & vbcrlf
   s =  f.DateCreated

   ShowFileAccessInfo = s
End Function
Hows about this :

'--------------------------------------------------------

Dim NewSubFolder, corrFolders
Dim time
Dim date
Dim altTime
Dim altDate
Dim f, s
Dim objFile
Dim objFolder

Set FSO = CreateObject("Scripting.FileSystemObject") '10
ShowSubfolders FSO.GetFolder("C:\Data")

Sub ShowSubFolders(Folder)
    For Each Subfolder in Folder.SubFolders
          If IsNumeric(FSO.GetFileName(Subfolder.Path)) = True Then
          wscript.echo Subfolder.Path
          NewSubFolder = Subfolder.Path

Set objFolder = FSO.GetFolder(NewSubFolder)
Set colSubfolders = objFolder.Subfolders '20
For Each objSubfolder in colSubfolders
    Wscript.Echo objSubfolder.Name ', objSubfolder.Size
corrFolders = objSubfolder
'Looping through all the files
'Loop through the Files collection

Set objFolder = FSO.GetFolder(corrFolders)

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
   & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFolders = objWMIService.ExecQuery _
   ("Select * from Win32_Directory where Name Like '%c:\\Data%'")

For Each objFile in objFolder.Files


FileExt = Right(objFile.Name,3)
If FileExt = "MAI" And Len(objFile.Name) > 20 Then
   Set f = fso.GetFile(objFolder & "\" & objFile.Name)
wscript.echo f
   s =  f.DateCreated

wscript.echo s & " Date Created "
'40
date = Left(s,10)

time = Right(s,8)

altTime = Replace(time, ":", "")
altDate = Replace(date, "/", "")
WScript.Echo altDate & " Date"
WScript.Echo altTime & " Time"

'wscript.echo Len(objFile.Name) & " Length of Files Name"

FSO.MoveFile objFile, objFolder & "\" & altDate & "_" & altTime &".MAI"

else
WScript.Echo objFile.Name                                                                                            
WScript.Echo "File renamed successfully!"
End If
Next


Next

          End if
    Next
End Sub


'Clean up!
Set objFolder = Nothing
Set objFile = Nothing
Set FSO = Nothing


'----------------------------------------------------------------------

It only found files that had a file extension of MAI not mai ie the extensions that were not in capitals were not renamed to the data / time that they were created , do you want this fixed ?
I had ran it as "cscript.exe test.vbs >scripttest.txt"

The "scripttest.txt" file says
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

C:\data\2010901
CORRESPONDENCE

So it is finding the "test.vbs" file, and is finding the "C:\data" folder like it should.

In regards to your 2:27pm post, there is no way  that I could alter it "so that it relates to each individual file", one at a time, because there would be new files added all the time, with drastically different names.
This script will only find files with extension MAI and will ignore any with the file extension mai or any other file extension for that matter, so not sure if you want that altered or not ?

Don't worry about that comment I put there with regards to relating to each individual file !
That last script I posted is what you are after as far as I am aware !  Please give me an update on if you are still having the same issue or not  ?

Kind regards

Gecko
I'm still having the issue as before:

c:\test.vbs (77, 4) Microsoft VBScript runtime error: File not found

I had ran it as "cscript.exe test.vbs >scripttest.txt"

The "scripttest.txt" file says
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

C:\data\2010901
CORRESPONDENCE

So it is finding the "test.vbs" file in the C root, and is finding the "C:\data" folder like it should.
If you just double click the vb script, what line does it say it has an error with ?
Windows Script Host
Script:    C:\test1.vbs
Line:      77
Char:     4
Error:     File not found
Code:     800A0035
Source:  Microsoft VBScript runtime error

Line 77 is:
filespec = "C:\acc.exe"

Forgot about that.  Should I change that to something else?  What exactly is that line for?
You had made the comment above about

"<-- I made it point to this file so you will obviously need to alter this so that it relates to each individual file ie all of your mai files one at a time :) Let me look into this for you :)"

How should I change it?
Here is the script so far ( without the filespec because I replaced that )

'===============================================

Dim NewSubFolder, corrFolders
Dim time
Dim date
Dim altTime
Dim altDate
Dim f, s
Dim objFile
Dim objFolder

Set FSO = CreateObject("Scripting.FileSystemObject") '10
ShowSubfolders FSO.GetFolder("C:\Data")

Sub ShowSubFolders(Folder)
    For Each Subfolder in Folder.SubFolders
          If IsNumeric(FSO.GetFileName(Subfolder.Path)) = True Then
          wscript.echo Subfolder.Path
          NewSubFolder = Subfolder.Path

Set objFolder = FSO.GetFolder(NewSubFolder)
Set colSubfolders = objFolder.Subfolders '20
For Each objSubfolder in colSubfolders
    Wscript.Echo objSubfolder.Name ', objSubfolder.Size
corrFolders = objSubfolder
'Looping through all the files
'Loop through the Files collection

Set objFolder = FSO.GetFolder(corrFolders)

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
   & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFolders = objWMIService.ExecQuery _
   ("Select * from Win32_Directory where Name Like '%c:\\Data%'")

For Each objFile in objFolder.Files


FileExt = Right(objFile.Name,3)
If FileExt = "MAI" And Len(objFile.Name) > 20 Then
   Set f = fso.GetFile(objFolder & "\" & objFile.Name) '<-- I replaced filespec with this line and some lines below as shown
wscript.echo f                                                        
   s =  f.DateCreated                                                

wscript.echo s & " Date Created "
'40
date = Left(s,10)                       '<-- This line

time = Right(s,8)                       '<-- And this line were altered from the following :

                                                'date = Left(ShowFileAccessInfo(filespec),10)


                                                 'time = Right(ShowFileAccessInfo(filespec),8)



altTime = Replace(time, ":", "")
altDate = Replace(date, "/", "")
WScript.Echo altDate & " Date"
WScript.Echo altTime & " Time"

'wscript.echo Len(objFile.Name) & " Length of Files Name"

FSO.MoveFile objFile, objFolder & "\" & altDate & "_" & altTime &".MAI"

else
WScript.Echo objFile.Name                                                                                            
WScript.Echo "File renamed successfully!"
End If
Next


Next

          End if
    Next
End Sub


'Clean up!
Set objFolder = Nothing
Set objFile = Nothing
Set FSO = Nothing
Does that make sense now ?
Man you're good!  We're getting there! :)

Ok, I got a "file already exists" error because its renaming the files without "seconds" being involved.  The first file got renamed to "4112005 _1426 PM.MAI", showing a time of "14:26", but we need seconds to deal with duplicates.  However, it's using another date/time stamp, and not the creation date/time stamp.  The same file above, based on the creation date and time, should be "03012005_081123", as the creation date/time is 03/01/2005 at 8:11:23 AM.  The files need to be named after the creation date and time so that users will know what the date and time was that an email was generated.  A "last modified" date/time will not work for our purposes.

Thank you so much for your patience and persistence!  You've been awesome! :)
The message box's do not display the correct format, but if you look at what the files have been renamed as and take into consideration the code below :

   s =  f.DateCreated         '<--- I AM USING THE DATE CREATED !!!                                      

wscript.echo s & " Date Created "
'40
date = Left(s,10)                       '<-- This line takes s which is basically the date created and gets the date

time = Right(s,8)                      '<-- This line takes s which is basically the date created and gets the time

So it is using date created as far as I can tell :)

check the actual filenames them selves !
ASKER CERTIFIED SOLUTION
Avatar of Shane Russell
Shane Russell
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Can this be made to use seconds as well?
use seconds in what way, if you could please ellaborate ?
Like I had said in the original question:

"I would like to have a script that will search within all of the Correspondence folders (which again are located in each of the Project Folders), and will rename only files that have the ".mai" extension to something like 040605_090354.mai, which would represent date_time in this format: mm/dd/yy_hh:mm:ss in 24-hour time.  Is this possible?  
I read in a PC Magazine article (http://www.pcmag.com/article2/0%2C1759%2C1386947%2C00.asp), where this can be done in a batch file.  I was going to try to incorporate what it says in that article into my existing batch file, but it will not display seconds.  And there would be times when multiple emails about a particular project are sent by multiple people, possibly within the same minute, so there would be some issues there.  So I would need either to show seconds in the file name, or which would rename the files incrementally such as 040605_0903a, 040605_0903b, 040605_0903c, etc."
And that's the issue that's going on now, it's giving me an error because it's trying to rename a file to a name that already exists; more than 1 file can be created within one minute, but it will be fairly unlikely that more than one file gets created within one second.
When I was testing out the script it gave the exact time including the minutes ( 09042005_60241 PM.MAI ) hence the 09/04/2005_6:02:41 PM.MAI the 41 being the seconds , obviously :) So that last script I posted does the seconds part as well , I had to change the length of the Right function to 10 ie on this line :

time = Right(s,10)  

So hence it does show seconds in the filename, from the testing I have done at least !

So give the last script I posted here :


Comment from gecko_au2003
Date: 04/19/2005 01:26PM PDT

A go !!
That is the script that I had used, and this is the filename of the one file that was renamed "4112005_1426 PM.MAI".  And I had

Here is the result of the scripttest.txt file;

'-------------================---------------------
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

C:\data\2011502
CONTRACTS
CORRESPONDENCE
C:\data\2011502\CORRESPONDENCE\2942EC6543674953AFA6202D3C66B7.MAI
4/11/2005 4:14:26 PM Date Created
4112005  Date
1426 PM Time
C:\data\2011502\CORRESPONDENCE\2A98228462D943038F398B7647721B.MAI
4/11/2005 4:14:26 PM Date Created
4112005  Date
1426 PM Time
'-------------================---------------------

Now for some reason though, it still isn't showing the seconds in the file name, as in "4112005_1426 PM.MAI", and the date isn't showing up in mm/dd/yyyy, here it is m/dd/yyyy.  I see that on yours it did the renaming properly (09042005_60241 PM.MAI ).  Any ideas why it wouldn't on mine?  Do I need to change the "time = Right(s,10)".  Would that make a difference?

Here's the script I used:

'===============================================

Dim NewSubFolder, corrFolders
Dim time
Dim date
Dim altTime
Dim altDate
Dim f, s
Dim objFile
Dim objFolder

Set FSO = CreateObject("Scripting.FileSystemObject") '10
ShowSubfolders FSO.GetFolder("C:\Data")

Sub ShowSubFolders(Folder)
    For Each Subfolder in Folder.SubFolders
          If IsNumeric(FSO.GetFileName(Subfolder.Path)) = True Then
          wscript.echo Subfolder.Path
          NewSubFolder = Subfolder.Path

Set objFolder = FSO.GetFolder(NewSubFolder)
Set colSubfolders = objFolder.Subfolders '20
For Each objSubfolder in colSubfolders
    Wscript.Echo objSubfolder.Name ', objSubfolder.Size
corrFolders = objSubfolder
'Looping through all the files
'Loop through the Files collection

Set objFolder = FSO.GetFolder(corrFolders)

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
   & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFolders = objWMIService.ExecQuery _
   ("Select * from Win32_Directory where Name Like '%c:\\Data%'")

For Each objFile in objFolder.Files


FileExt = Right(objFile.Name,3)
If FileExt = "MAI" And Len(objFile.Name) > 20 Then
   Set f = fso.GetFile(objFolder & "\" & objFile.Name) '<-- I replaced filespec with this line and some lines below as shown
wscript.echo f                                                        
   s =  f.DateCreated                                                

wscript.echo s & " Date Created "
'40
date = Left(s,10)                       '<-- This line

time = Right(s,8)                       '<-- And this line were altered from the following :

                                                'date = Left(ShowFileAccessInfo(filespec),10)


                                                 'time = Right(ShowFileAccessInfo(filespec),8)



altTime = Replace(time, ":", "")
altDate = Replace(date, "/", "")
WScript.Echo altDate & " Date"
WScript.Echo altTime & " Time"

'wscript.echo Len(objFile.Name) & " Length of Files Name"

FSO.MoveFile objFile, objFolder & "\" & altDate & "_" & altTime &".MAI"

else
WScript.Echo objFile.Name                                                                                            
WScript.Echo "File renamed successfully!"
End If
Next


Next

          End if
    Next
End Sub


'Clean up!
Set objFolder = Nothing
Set objFile = Nothing
Set FSO = Nothing


'-------------================--------------------
You need to change this line in your code :

time = Right(s,8)

To

time = Right(s,10)
yes because the 10 tells the Right function how many characters to trim ie if you had a string that said :

I went to the park today

and you only wanted the word today from that string , you would use functions like Left, Right, Mid, InStr , etc to attain w/e value(s) you wanted from that string. So as an example using the Right function to get the word "today" from that string you would have something like :

Dim strSentance As String
Dim strResult As String

strSentance = "I went to the park today"

strResult = Right(strSentance, 5 )

MsgBox strResult '<-- That will display the word today from this example :)
oopps, this line :

strResult = Right(strSentance, 5 )

Should be :

strResult = Right(strSentance, 5) '<-- had a space between 5 and the last bracket :-S, not sure if that makes much difference in the scripting but I know if you did that in visual basic 6, it would correct it for you :)
You are a genius my friend!  It now renames the files to the date and time (although it names files like "4112005 _41431 PM.MAI" instead of "04112005_041431" with 2 digits for each field (mm/dd/yyyy_hh:mm:ss), but it does display seconds).

One last little thing; to make it not display a popup when running, which lines sould I remark out?  With a lot of files, and the script being run every hour, it should be automated and run in the background.
1.} You would have to use an if statement on date and time before it moves and renames the file(s) :

'==================== Code Snippet==================

If Left(date,1) <> 0 And Left(time,1) <> 0 Then
FSO.MoveFile objFile, objFolder & "\" & "0" & altDate & "_" &"0" & altTime &".MAI"
Else
FSO.MoveFile objFile, objFolder & "\" & altDate & "_" & altTime &".MAI"
End If

'===============End of code snippet================

^^ You would insert the above code below this coding in the script :

   s =  f.DateCreated                                                

wscript.echo s & " Date Created "
'40
date = Left(s,10)                       '<-- This line

time = Right(s,8)                       '<-- And this line were altered from the following :

                                                'date = Left(ShowFileAccessInfo(filespec),10)


                                                 'time = Right(ShowFileAccessInfo(filespec),8)



altTime = Replace(time, ":", "")
altDate = Replace(date, "/", "")
WScript.Echo altDate & " Date"
WScript.Echo altTime & " Time"

'wscript.echo Len(objFile.Name) & " Length of Files Name"

              'FSO.MoveFile objFile, objFolder & "\" & altDate & "_" & altTime &".MAI" '<-- Commented out and replaced with :
                                                                                                                         ' Code snippet from above !!

2.} You would remove any lines that have :

wscript.echo

Does that make sense to you ??
WOW!  You are a genius indeed! :)  It works like a charm!

gecko, I really appreciate your hard work over the past 2 weeks on this.  By the length of this thread, it's obvious you've really cared about following this to the end.  You are always extremely responsive, very clear and concise, and very quick to help.  You are truly an awesome person.  Thank you again for all your help.  I only wish I could give you a couple thousand more points!!!
Don't worry about the points ! Just glad to help out a fellow expert  ! The appreciation is more then enough of an award !! I just wish the feedback was up and running again cos I'm sure I would of gotten some good feedback there lol cough cough :)

Anyway, if you still get stuck on it or want little bits n pieces added , dont hesitate to ask ;)

Kind regards

Gecko
BTW - I didn't know charms worked lol , do they ? If so, I would love to get one of them lol :-" cos I could really use one right now !!! LOL