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
MediaMonAsked:
Who is Participating?
 
Bill PrewConnect With a Mentor Commented:
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
 
coraxalCommented:
Would you be open to using Powershell for this task?
0
 
MediaMonAuthor Commented:
Sure, the box is windows 2003 R2 SP2 though I am pretty sure it does PS 1.0 & 2.0

Thanks
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
Bill PrewCommented:
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
 
MediaMonAuthor Commented:
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
 
Bill PrewCommented:
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
 
MediaMonAuthor Commented:
Correct. Thank you
0
 
foxvisionCommented:
Since you said: "...rename NAMBGE20130124001.psp* to NAMBGE20130124001.err..."

Would you have psp2 as well or you will only have just psp1?
0
 
foxvisionCommented:
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
 
foxvisionCommented:
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
 
Bill PrewCommented:
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
 
MediaMonAuthor Commented:
Sorry all been away.

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

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

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

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

I however get the following error.
23-01-2013-2-13-45-PM.jpg
0
 
MediaMonAuthor Commented:
Thank you !!!
0
 
Bill PrewCommented:
Welcome.

~bp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.