Link to home
Start Free TrialLog in
Avatar of MediaMon
MediaMon

asked on

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

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
Avatar of coraxal
coraxal
Flag of United States of America image

Would you be open to using Powershell for this task?
Avatar of MediaMon
MediaMon

ASKER

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

Thanks
Avatar of Bill Prew
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
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
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
Correct. Thank you
Since you said: "...rename NAMBGE20130124001.psp* to NAMBGE20130124001.err..."

Would you have psp2 as well or you will only have just psp1?
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.
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

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
Sorry all been away.

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

Many Thanks
Sorry I should have been clearer in my initial question.
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
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.
Does this mean ps_temp has to be 30 minutes or more than EVERY psp? Or just psp1 will do?
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
sorry

so if ps_temp file is older than the corresponding psp1 file rename to err1 etc
sorry at least 30 mins older
So if psp2 is not older then the extension remains?
Can there be psp10 and larger?

~bp
Oh sorry there will only ever be psp1 to psp4.
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
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
billprew's code will work on that condition.
Thank you both.

I however get the following error.
23-01-2013-2-13-45-PM.jpg
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

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
Thank you !!!
Welcome.

~bp