Solved

Batch file to rename file extention when another file exists which meets a certain condition.

Posted on 2013-01-22
28
294 Views
Last Modified: 2013-01-23
Hi All

I am looking for a batch file / VB script which will check for the following.

These files exist in a folder.

NAMBGE20130124001.psp1             22/01/2013  03:24 PM
NAMBGE20130124001.ps_temp      22/01/2013  03:54 PM

If NAMBGE20130124001.ps_temp exists and is 30 mins (or more) older than
NAMBGE20130124001.psp1.

then rename NAMBGE20130124001.psp* to NAMBGE20130124001.err

Any ideas would be appreciated.

Many Thanks
0
Comment
Question by:MediaMon
  • 12
  • 9
  • 6
  • +1
28 Comments
 
LVL 5

Expert Comment

by:coraxal
ID: 38807694
Would you be open to using Powershell for this task?
0
 

Author Comment

by:MediaMon
ID: 38807705
Sure, the box is windows 2003 R2 SP2 though I am pretty sure it does PS 1.0 & 2.0

Thanks
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 38807710
Save this as a VBS file, and run it to perform the logic you described.  Edit the constants as needed.

Const BaseDir = "c:\temp\"
Const strCheckFile = "NAMBGE20130124001.ps_temp"
Const strPspFile = "NAMBGE20130124001.psp1"
Const strErrFile = "NAMBGE20130124001.psp1"

Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FileExists(BaseDir & strCheckFile) Then
    Set objCheckFile = objFSO.GetFile(BaseDir & strCheckFile)
    If DateDiff("n", objCheckFile.DateLastModified, Now) > 29 Then
        If objFSO.FileExists(BaseDir & strPspFile) Then
            objFSO.MoveFile BaseDir & strPspFile, BaseDir & strErrFile
        End If
    End If
End If

Open in new window

~bp
0
 

Author Comment

by:MediaMon
ID: 38807755
Thanks bp that works great when I specify the exact file name.

My issue is there are thousands of files names.

I tried to modify your script so it will look for a wildcard but it doesn't seem to like it.

Const BaseDir = "c:\temp\"
Const strCheckFile = "*.ps_temp"
Const strPspFile = "*.psp1"
Const strErrFile = "*.err"

Any ideas ?

Thanks
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 38807832
So, do you want to look for all *.ps_temp in a folder, and if it is older than 30 minutes, rename the corresponding *.psp1 to *.err?

~bp
0
 

Author Comment

by:MediaMon
ID: 38807849
Correct. Thank you
0
 

Expert Comment

by:foxvision
ID: 38807995
Since you said: "...rename NAMBGE20130124001.psp* to NAMBGE20130124001.err..."

Would you have psp2 as well or you will only have just psp1?
0
 

Expert Comment

by:foxvision
ID: 38808043
I will assume you do not have psp2 and just psp1, also I will use VB code instead of VBS:

    Dim ps_tempFileName As String
    Dim psp1Filename As String
    Dim errFilename As String
    Dim ps_tempDateTime As Date
    Dim psp1DateTime As Date
        
    ps_tempFileName = Dir("C:\Temp\im1\*.ps_temp")
        
    Do While ps_tempFileName > ""
        ps_tempFileName = "C:\Temp\im1\" & ps_tempFileName
        ps_tempDateTime = FileDateTime(ps_tempFileName)
        
        psp1Filename = Replace(ps_tempFileName, ".ps_temp", ".psp1", 1, -1, vbTextCompare)
        psp1DateTime = FileDateTime(psp1Filename)
        
        If DateDiff("n", psp1DateTime, ps_tempDateTime) > 29 Then
            Set objFSO = CreateObject("Scripting.FileSystemObject")

            If objFSO.FileExists(psp1Filename) Then
                errFilename = Replace(ps_tempFileName, ".ps_temp", ".err", 1, -1, vbTextCompare)
                objFSO.MoveFile psp1Filename, errFilename
            End If
        End If
            
        ps_tempFileName = Dir()
    Loop

Open in new window


Notice your requirement is to compare if PS_TEMP is older than PSP1 for 30 minutes or more, billprew solution above checks if PS_TEMP has been created for 30 minutes or more.

You can now convert this code to VBS.
0
 

Expert Comment

by:foxvision
ID: 38808206
And this is the VBS version:

  Dim fso, folder, files, sFolder, thisFilename, ps_tempDateTime, psp1DateTime
  
  Set fso = CreateObject("Scripting.FileSystemObject")
  sFolder = "C:\Temp\im1"
  
  Set folder = fso.GetFolder(sFolder)
  Set files = folder.files
  Set objFSO = CreateObject("Scripting.FileSystemObject")

  For Each folderIdx In files
    thisFilename = folderIdx.Name
    If Len(thisFilename) > 8 And Right(thisFilename, 8) = ".ps_temp" Then
        ps_tempFileName = sFolder & "\" & thisFilename
        
        If objFSO.FileExists(ps_tempFileName) Then
            Set objFile = objFSO.GetFile(ps_tempFileName)
            ps_tempDateTime = objFile.DateLastModified
        
            psp1Filename = Replace(ps_tempFileName, ".ps_temp", ".psp1", 1, -1, vbTextCompare)
            
            If objFSO.FileExists(psp1Filename) Then
                Set objFile = objFSO.GetFile(psp1Filename)
                psp1DateTime = objFile.DateLastModified
        
                If DateDiff("n", psp1DateTime, ps_tempDateTime) > 29 Then
                    If objFSO.FileExists(psp1Filename) Then
                        errFilename = Replace(ps_tempFileName, ".ps_temp", ".err", 1, -1, vbTextCompare)
                        objFSO.MoveFile psp1Filename, errFilename
                    End If
                End If
             End If
        End If
    End If
  Next

Open in new window

0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 38808280
Okay, I expanded my code to handle multiple files.

Const strBaseDir = "c:\temp"
Const strCheckExt = "ps_temp"
Const strPspExt = "psp1"
Const strErrExt = "err"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objBaseDir = objFSO.GetFolder(strBaseDir)

For Each objFile in objBaseDir.Files
    If LCase(objFSO.GetExtensionName(objFile.Path)) = LCase(strCheckExt) Then
        strName = objFSO.GetBaseName(objFile.Path)
        strPsp = strBaseDir & "\" & strName & "." & strPspExt
        If objFSO.FileExists(strPsp) Then
        If DateDiff("n", objCheckFile.DateLastModified, objFSO.GetFile(strPsp).DateLastModified) > 29 Then
            strErr = strBaseDir & "\" & strName & "." & strErrExt
            objFSO.MoveFile objFile.Path, strErr
        End If
    End If
Next

Open in new window

~bp
0
 

Author Comment

by:MediaMon
ID: 38808283
Sorry all been away.

Actually there is *.psp1, *.psp2, *.psp3 & *.psp4.

Many Thanks
0
 

Author Comment

by:MediaMon
ID: 38808284
Sorry I should have been clearer in my initial question.
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 38808299
So which one gets renamed to *.err then???  Or do the need to become .err1, .err2, .err3 etc?

Can there be psp10 and larger?

~bp
0
 

Author Comment

by:MediaMon
ID: 38808306
not just

psp1 -> err
psp2 -> err
psp3 -> err
psp4 -> err

if that not possible I could go with

psp1 -> err1
psp2 -> err2
psp3 -> err3
psp4 -> err4

appreciate your help here.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Expert Comment

by:foxvision
ID: 38808311
Does this mean ps_temp has to be 30 minutes or more than EVERY psp? Or just psp1 will do?
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 38808312
Can't rename multiple files to the same extension, you'd be losing all but one file.

And you only want to rename when the 30 minute age is present, right?  So some PSP files might get renamed, others might not, for a given PS_TEMP, right?

Can there be psp10 and larger?

~bp
0
 

Author Comment

by:MediaMon
ID: 38808357
sorry

so if ps_temp file is older than the corresponding psp1 file rename to err1 etc
0
 

Author Comment

by:MediaMon
ID: 38808358
sorry at least 30 mins older
0
 

Expert Comment

by:foxvision
ID: 38808363
So if psp2 is not older then the extension remains?
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 38808364
Can there be psp10 and larger?

~bp
0
 

Author Comment

by:MediaMon
ID: 38808368
Oh sorry there will only ever be psp1 to psp4.
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 38808376
Okay, give this a try.

Const strBaseDir = "c:\temp"
Const strCheckExt = "ps_temp"
Const strPspExt = "psp"
Const strErrExt = "err"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objBaseDir = objFSO.GetFolder(strBaseDir)

For Each objFile in objBaseDir.Files
    If LCase(objFSO.GetExtensionName(objFile.Path)) = LCase(strCheckExt) Then
        strName = objFSO.GetBaseName(objFile.Path)
        For i = 1 To 4
            strPsp = strBaseDir & "\" & strName & "." & strPspExt & i
            If objFSO.FileExists(strPsp) Then
            If DateDiff("n", objCheckFile.DateLastModified, objFSO.GetFile(strPsp).DateLastModified) > 29 Then
                strErr = strBaseDir & "\" & strName & "." & strErrExt & i
                objFSO.MoveFile objFile.Path, strErr
            End If
        Next
    End If
Next

Open in new window

~bp
0
 

Author Comment

by:MediaMon
ID: 38808380
If these files exist

23/01/2013  01:00 PM                 1 ADGE20130123001.psp1
23/01/2013  01:30 PM         3,705,878 ADGE20130123001.ps_temp

rename ADGE20130123001.psp1 to ADGE20130123001.err1

23/01/2013  01:00 PM                 1 LNGE20130123001.psp2
23/01/2013  01:30 PM         3,705,878 LNGE20130123001.ps_temp

rename LNGE20130123001.psp2 to LNGE20130123001.err2

23/01/2013  01:00 PM                 1 NNGE20130123004.psp3
23/01/2013  01:00 PM                 1 NNGE20130123004.ps_temp

rename NNGE20130123004.psp3 to NNGE20130123004.err3

23/01/2013  01:00 PM                 1 SECLGE20130123051.psp4
23/01/2013  01:30 PM                 1 SECLGE20130123051.ps_temp

rename SECLGE20130123051.psp4 to SECLGE20130123051.err4

Thanks
0
 

Expert Comment

by:foxvision
ID: 38808392
billprew's code will work on that condition.
0
 

Author Comment

by:MediaMon
ID: 38808403
Thank you both.

I however get the following error.
23-01-2013-2-13-45-PM.jpg
0
 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 38808407
Several errors in that code, sorry.  This should now work, adjust the CONST as needed.

Const strBaseDir = "c:\ee\EE28004797\files"
Const strCheckExt = "ps_temp"
Const strPspExt = "psp"
Const strErrExt = "err"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objBaseDir = objFSO.GetFolder(strBaseDir)

For Each objFile in objBaseDir.Files
    If LCase(objFSO.GetExtensionName(objFile.Path)) = LCase(strCheckExt) Then
        strName = objFSO.GetBaseName(objFile.Path)
        For i = 1 To 4
            strPsp = strBaseDir & "\" & strName & "." & strPspExt & i
            If objFSO.FileExists(strPsp) Then
                If DateDiff("n", objFile.DateLastModified, objFSO.GetFile(strPsp).DateLastModified) > 29 Then
                    strErr = strBaseDir & "\" & strName & "." & strErrExt & i
                    ''objFSO.MoveFile objFile.Path, strErr
                    Wscript.Echo objFile.Path & ", " & strErr
                End If
            End If
        Next
    End If
Next

Open in new window

~bp
0
 

Author Closing Comment

by:MediaMon
ID: 38808584
Thank you !!!
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 38809650
Welcome.

~bp
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now