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
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
Would you be open to using Powershell for this task?
ASKER
Sure, the box is windows 2003 R2 SP2 though I am pretty sure it does PS 1.0 & 2.0
Thanks
Thanks
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
~bp
ASKER
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
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
~bp
ASKER
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?
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:
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.
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
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
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
~bp
ASKER
Sorry all been away.
Actually there is *.psp1, *.psp2, *.psp3 & *.psp4.
Many Thanks
Actually there is *.psp1, *.psp2, *.psp3 & *.psp4.
Many Thanks
ASKER
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
Can there be psp10 and larger?
~bp
ASKER
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.
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
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
ASKER
sorry
so if ps_temp file is older than the corresponding psp1 file rename to err1 etc
so if ps_temp file is older than the corresponding psp1 file rename to err1 etc
ASKER
sorry at least 30 mins older
So if psp2 is not older then the extension remains?
Can there be psp10 and larger?
~bp
~bp
ASKER
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
~bp
ASKER
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
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.
ASKER
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you !!!
Welcome.
~bp
~bp