From: ekrengel on
On Feb 17, 1:39 am, "Pegasus [MVP]" <n...(a)microsoft.com> wrote:
> "Marc M" <marc.manfr...(a)gmail.com> said this in news itemnews:df8138e7-bb30-4c73-afe6-003d6969bde7(a)g19g2000yqe.googlegroups.com...
>
> > On Feb 16, 9:34 pm, Marc M <marc.manfr...(a)gmail.com> wrote:
>
> > The behavior that I'm trying to avoid is for the script to only rename
> > files with a stdf_tmp extension to stdf extension and not touch any
> > other files in the folder, basically ignore all other files. It seems
> > that the script will modify all files in the folder, not just the ones
> > with the stdf_tmp extension and it will rename files every time it
> > runs.
>
> You need to add an "if then" statement that tests your file name, e.g. like
> so:
>
> if {the 8 right-most characters of the file name are stdf_tmp} then
>   if the file is older than 36 hours
>     rename it
>   end if
> end if

How about this?

sFolder = "c:\stdf_tmp"
iAge = 36 'hours
sFileType = "stdf_tmp"

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(sFolder)

For Each oFile In oFolder.Files
If DateDiff("h", oFile.DateLastModified, Now()) > iAge _
AND oFSO.GetExtensionName(oFile.Name) = sFileType Then

On Error Resume Next
oFile.Name = Left(oFile.name, Len(oFile.Name) - 4) '& "stdf"
If (Err.Number <> 0) Then
On Error GoTo 0
MsgBox "Cannot rename " & oFile.Name & vbLF
& Err.Description
End If
On Error Goto 0

Next
From: Marc M on
On Feb 17, 10:45 am, ekrengel <erickreng...(a)gmail.com> wrote:
> On Feb 17, 1:39 am, "Pegasus [MVP]" <n...(a)microsoft.com> wrote:
>
>
>
> > "Marc M" <marc.manfr...(a)gmail.com> said this in news itemnews:df8138e7-bb30-4c73-afe6-003d6969bde7(a)g19g2000yqe.googlegroups.com...
>
> > > On Feb 16, 9:34 pm, Marc M <marc.manfr...(a)gmail.com> wrote:
>
> > > The behavior that I'm trying to avoid is for the script to only rename
> > > files with a stdf_tmp extension to stdf extension and not touch any
> > > other files in the folder, basically ignore all other files. It seems
> > > that the script will modify all files in the folder, not just the ones
> > > with the stdf_tmp extension and it will rename files every time it
> > > runs.
>
> > You need to add an "if then" statement that tests your file name, e.g. like
> > so:
>
> > if {the 8 right-most characters of the file name are stdf_tmp} then
> >   if the file is older than 36 hours
> >     rename it
> >   end if
> > end if
>
> How about this?
>
> sFolder = "c:\stdf_tmp"
> iAge = 36       'hours
> sFileType = "stdf_tmp"
>
> Set oFSO = CreateObject("Scripting.FileSystemObject")
> Set oFolder = oFSO.GetFolder(sFolder)
>
> For Each oFile In oFolder.Files
>   If DateDiff("h", oFile.DateLastModified, Now()) > iAge _
>   AND oFSO.GetExtensionName(oFile.Name) = sFileType Then
>
>      On Error Resume Next
>      oFile.Name = Left(oFile.name, Len(oFile.Name) - 4) '& "stdf"
>      If (Err.Number <> 0) Then
>         On Error GoTo 0
>         MsgBox "Cannot rename " & oFile.Name & vbLF
>          & Err.Description
>      End If
>      On Error Goto 0
>
> Next

sFolder = "c:\stdf_tmp"
iAge = 36 'hours
sFileType = "stdf_tmp"


Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(sFolder)


For Each oFile In oFolder.Files
If DateDiff("h", oFile.DateLastModified, Now()) > iAge _
AND oFSO.GetExtensionName(oFile.Name) = sFileType Then


On Error Resume Next
oFile.Name = Left(oFile.name, Len(oFile.Name) - 4) '& "stdf"
If (Err.Number <> 0) Then
On Error GoTo 0
MsgBox "Cannot rename " & oFile.Name & vbLF
& Err.Description
End If
On Error Goto 0

End If

Next


Thanks for your input, ekrengel. I added an "End If" at the bottom to
close out the second If statement. The only issue I encounter when
trying to run the script is at "& Err.Description" part. I get an
error - "Expected statement"

Best regards,
Marc
From: ekrengel on
On Feb 17, 4:21 pm, Marc M <marc.manfr...(a)gmail.com> wrote:
> On Feb 17, 10:45 am, ekrengel <erickreng...(a)gmail.com> wrote:
>
>
>
> > On Feb 17, 1:39 am, "Pegasus [MVP]" <n...(a)microsoft.com> wrote:
>
> > > "Marc M" <marc.manfr...(a)gmail.com> said this in news itemnews:df8138e7-bb30-4c73-afe6-003d6969bde7(a)g19g2000yqe.googlegroups.com...
>
> > > > On Feb 16, 9:34 pm, Marc M <marc.manfr...(a)gmail.com> wrote:
>
> > > > The behavior that I'm trying to avoid is for the script to only rename
> > > > files with a stdf_tmp extension to stdf extension and not touch any
> > > > other files in the folder, basically ignore all other files. It seems
> > > > that the script will modify all files in the folder, not just the ones
> > > > with the stdf_tmp extension and it will rename files every time it
> > > > runs.
>
> > > You need to add an "if then" statement that tests your file name, e.g.. like
> > > so:
>
> > > if {the 8 right-most characters of the file name are stdf_tmp} then
> > >   if the file is older than 36 hours
> > >     rename it
> > >   end if
> > > end if
>
> > How about this?
>
> > sFolder = "c:\stdf_tmp"
> > iAge = 36       'hours
> > sFileType = "stdf_tmp"
>
> > Set oFSO = CreateObject("Scripting.FileSystemObject")
> > Set oFolder = oFSO.GetFolder(sFolder)
>
> > For Each oFile In oFolder.Files
> >   If DateDiff("h", oFile.DateLastModified, Now()) > iAge _
> >   AND oFSO.GetExtensionName(oFile.Name) = sFileType Then
>
> >      On Error Resume Next
> >      oFile.Name = Left(oFile.name, Len(oFile.Name) - 4) '& "stdf"
> >      If (Err.Number <> 0) Then
> >         On Error GoTo 0
> >         MsgBox "Cannot rename " & oFile.Name & vbLF
> >          & Err.Description
> >      End If
> >      On Error Goto 0
>
> > Next
>
> sFolder = "c:\stdf_tmp"
> iAge = 36       'hours
> sFileType = "stdf_tmp"
>
> Set oFSO = CreateObject("Scripting.FileSystemObject")
> Set oFolder = oFSO.GetFolder(sFolder)
>
> For Each oFile In oFolder.Files
>   If DateDiff("h", oFile.DateLastModified, Now()) > iAge _
>   AND oFSO.GetExtensionName(oFile.Name) = sFileType Then
>
>      On Error Resume Next
>      oFile.Name = Left(oFile.name, Len(oFile.Name) - 4) '& "stdf"
>      If (Err.Number <> 0) Then
>         On Error GoTo 0
>         MsgBox "Cannot rename " & oFile.Name & vbLF
>          & Err.Description
>      End If
>      On Error Goto 0
>
>   End If
>
> Next
>
> Thanks for your input, ekrengel.  I added an "End If" at the bottom to
> close out the second If statement.  The only issue I encounter when
> trying to run the script is at "& Err.Description" part. I get an
> error - "Expected statement"
>
> Best regards,
> Marc

Oh sorry, just add "_" at the end of the msgbox line. So it looks
like this:

MsgBox "Cannot rename " & oFile.Name & vbLF _
& Err.Description

That will continue the msgbox onto the next line. Or you could just
combine the two, it's only split up to look pretty =)

MsgBox "Cannot rename " & oFile.Name & vbLF & Err.Description
From: Marc M on
On Feb 17, 4:33 pm, ekrengel <erickreng...(a)gmail.com> wrote:
> On Feb 17, 4:21 pm, Marc M <marc.manfr...(a)gmail.com> wrote:
>
>
>
> > On Feb 17, 10:45 am, ekrengel <erickreng...(a)gmail.com> wrote:
>
> > > On Feb 17, 1:39 am, "Pegasus [MVP]" <n...(a)microsoft.com> wrote:
>
> > > > "Marc M" <marc.manfr...(a)gmail.com> said this in news itemnews:df8138e7-bb30-4c73-afe6-003d6969bde7(a)g19g2000yqe.googlegroups.com...
>
> > > > > On Feb 16, 9:34 pm, Marc M <marc.manfr...(a)gmail.com> wrote:
>
> > > > > The behavior that I'm trying to avoid is for the script to only rename
> > > > > files with a stdf_tmp extension to stdf extension and not touch any
> > > > > other files in the folder, basically ignore all other files. It seems
> > > > > that the script will modify all files in the folder, not just the ones
> > > > > with the stdf_tmp extension and it will rename files every time it
> > > > > runs.
>
> > > > You need to add an "if then" statement that tests your file name, e..g. like
> > > > so:
>
> > > > if {the 8 right-most characters of the file name are stdf_tmp} then
> > > >   if the file is older than 36 hours
> > > >     rename it
> > > >   end if
> > > > end if
>
> > > How about this?
>
> > > sFolder = "c:\stdf_tmp"
> > > iAge = 36       'hours
> > > sFileType = "stdf_tmp"
>
> > > Set oFSO = CreateObject("Scripting.FileSystemObject")
> > > Set oFolder = oFSO.GetFolder(sFolder)
>
> > > For Each oFile In oFolder.Files
> > >   If DateDiff("h", oFile.DateLastModified, Now()) > iAge _
> > >   AND oFSO.GetExtensionName(oFile.Name) = sFileType Then
>
> > >      On Error Resume Next
> > >      oFile.Name = Left(oFile.name, Len(oFile.Name) - 4) '& "stdf"
> > >      If (Err.Number <> 0) Then
> > >         On Error GoTo 0
> > >         MsgBox "Cannot rename " & oFile.Name & vbLF
> > >          & Err.Description
> > >      End If
> > >      On Error Goto 0
>
> > > Next
>
> > sFolder = "c:\stdf_tmp"
> > iAge = 36       'hours
> > sFileType = "stdf_tmp"
>
> > Set oFSO = CreateObject("Scripting.FileSystemObject")
> > Set oFolder = oFSO.GetFolder(sFolder)
>
> > For Each oFile In oFolder.Files
> >   If DateDiff("h", oFile.DateLastModified, Now()) > iAge _
> >   AND oFSO.GetExtensionName(oFile.Name) = sFileType Then
>
> >      On Error Resume Next
> >      oFile.Name = Left(oFile.name, Len(oFile.Name) - 4) '& "stdf"
> >      If (Err.Number <> 0) Then
> >         On Error GoTo 0
> >         MsgBox "Cannot rename " & oFile.Name & vbLF
> >          & Err.Description
> >      End If
> >      On Error Goto 0
>
> >   End If
>
> > Next
>
> > Thanks for your input, ekrengel.  I added an "End If" at the bottom to
> > close out the second If statement.  The only issue I encounter when
> > trying to run the script is at "& Err.Description" part. I get an
> > error - "Expected statement"
>
> > Best regards,
> > Marc
>
> Oh sorry, just add "_" at the end of the msgbox line.  So it looks
> like this:
>
> MsgBox "Cannot rename " & oFile.Name & vbLF _
> & Err.Description
>
> That will continue the msgbox onto the next line.  Or you could just
> combine the two, it's only split up to look pretty  =)
>
> MsgBox "Cannot rename " & oFile.Name & vbLF & Err.Description

Thanks, I have this so far-

sFolder = "c:\stdf_tmp"
iAge = 36 'hours
sFileType = "stdf_tmp"

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(sFolder)

For Each oFile In oFolder.Files
If DateDiff("h", oFile.DateLastModified, Now()) > iAge _
AND oFSO.GetExtensionName(oFile.Name) = sFileType Then

On Error Resume Next
oFile.Name = Left(oFile.name, Len(oFile.Name) - 4) '& "stdf"
If (Err.Number <> 0) Then
On Error GoTo 0
MsgBox "Cannot rename " & oFile.Name & vbLF & Err.Description
End If
On Error Goto 0

End If
Next

The script runs and ends successfully now but doesn't rename the files!
From: Pegasus [MVP] on


"Marc M" <marc.manfredi(a)gmail.com> said this in news item
news:0e93d701-e12c-43d9-960b-af7a52f6af5a(a)x9g2000vbo.googlegroups.com...

> Thanks, I have this so far-
>
> sFolder = "c:\stdf_tmp"
> iAge = 36 'hours
> sFileType = "stdf_tmp"
>
> Set oFSO = CreateObject("Scripting.FileSystemObject")
> Set oFolder = oFSO.GetFolder(sFolder)
>
> For Each oFile In oFolder.Files
> If DateDiff("h", oFile.DateLastModified, Now()) > iAge _
> AND oFSO.GetExtensionName(oFile.Name) = sFileType Then
>
> On Error Resume Next
> oFile.Name = Left(oFile.name, Len(oFile.Name) - 4) '& "stdf"
> If (Err.Number <> 0) Then
> On Error GoTo 0
> MsgBox "Cannot rename " & oFile.Name & vbLF & Err.Description
> End If
> On Error Goto 0
>
> End If
> Next
>
> The script runs and ends successfully now but doesn't rename the files!

I did not run your script but here are a couple of observations.

The line
oFile.Name = Left(oFile.name, Len(oFile.Name) - 4) '& "stdf"
should probably read
oFile.Name = Left(oFile.name, Len(oFile.Name) - 4) & "stdf"
Do you see the subtle difference?

When you face a problem like this one then you either single-step through
the program (if you have an editor that gives you this facility) or you
place strategic debugging statements like so:
wscript.echo "Renaming " & oFile.Name & " to " & Left(oFile.name,
Len(oFile.Name) - 4) & "stdf"
oFile.Name = Left(oFile.name, Len(oFile.Name) - 4) & "stdf"
This method implies that you invoke your script from a Command Prompt.

This is much faster than waiting a few hours until you get a response in a
newsgroup. It is also highly educational when learning how to write scripts.