From: ekrengel on
On Feb 18, 1:47 am, "Pegasus [MVP]" <n...(a)microsoft.com> wrote:
> "Marc M" <marc.manfr...(a)gmail.com> said this in news itemnews: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.

I agree. You will learn much better if you try and figure it out
yourself. Or at least show us that you have tried a couple things,
but were still unable to figure it out. Google is always my friend as
well when I need help.
From: Marc M on
On Feb 18, 1:30 pm, ekrengel <erickreng...(a)gmail.com> wrote:
> On Feb 18, 1:47 am, "Pegasus [MVP]" <n...(a)microsoft.com> wrote:
>
>
>
> > "Marc M" <marc.manfr...(a)gmail.com> said this in news itemnews: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.
>
> I agree.  You will learn much better if you try and figure it out
> yourself.  Or at least show us that you have tried a couple things,
> but were still unable to figure it out.  Google is always my friend as
> well when I need help.

Agreed. And thank you both for the help. I don't pretend to be a
programmer by any means, I'm trying to understand what the code is
doing but having trouble. I don't have a lot of time to devote to
learning VBscript since I never use it that why I resorted to this
newsgroup. I'll try and figure it out on my own from here on out.
Thanks again for your help.
From: Marc M on
On Feb 18, 1:47 am, "Pegasus [MVP]" <n...(a)microsoft.com> wrote:
> "Marc M" <marc.manfr...(a)gmail.com> said this in news itemnews: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.

Agreed. Thanks, I see the additional ' that was on the line. I've
removed it but it still doesn't rename the files. It seems that this
script is way out of my league and I'm not sure I can get it working.
Thanks again for your comments.

Regards,
Marc
From: Pegasus [MVP] on


"Marc M" <marc.manfredi(a)gmail.com> said this in news item
news:02b1b415-6ee8-4b86-8567-54a2ad17f8d7(a)28g2000vbf.googlegroups.com...
> On Feb 18, 1:47 am, "Pegasus [MVP]" <n...(a)microsoft.com> wrote:
>> "Marc M" <marc.manfr...(a)gmail.com> said this in news
>> itemnews: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.
>
> Agreed. Thanks, I see the additional ' that was on the line. I've
> removed it but it still doesn't rename the files. It seems that this
> script is way out of my league and I'm not sure I can get it working.
> Thanks again for your comments.
>
> Regards,
> Marc

When you need a script then you have two options:
a) You pay someone to write it for you. This gives you a quick result and
the author gets compensated for his expertise.
b) You get a large box of elbow grease, then make the effort to learn the
language. This takes time but is ultimately more rewarding because it
empowers you to do things by yourself.

It seems that you want the quick result without having to pay for it and
without making the effort to learn the script. You actually received 99.9%
of the code from two respondents but ultimately missed the remaining 0.1%
because you did not have the patience to implement the trouble shooting hint
that I gave you. Your script DID actually rename files but it did not rename
them correctly. The reason is embarassingly simple: You wrote

oFile.Name = Left(oFile.name, Len(oFile.Name) - 4) & "stdf"
(thus chopping 4 characters off the end of the file name)
instead of
oFile.Name = Left(oFile.name, Len(oFile.Name) - 8) & "stdf"
(now truncating 8 characters, because "stdf_tmp" is 8 characters long!)

Here is a fully tested version of your code. It works and I hope that it
encourages you to acquire the skill of programming yourself instead just
holding out your hand.

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
WScript.Echo "Rename", oFile.Name, "to", Left(oFile.name,
Len(oFile.Name) - 8) & "stdf"
On Error Resume Next
oFile.Name = Left(oFile.name, Len(oFile.Name) - 8) & "stdf"
If Err.Number <> 0 _
Then MsgBox "Cannot rename " & oFile.Name & vbLf & Err.Description
On Error Goto 0
End If
Next


From: Al Dunbar on


"Pegasus [MVP]" <news(a)microsoft.com> wrote in message
news:29AD924C-CF70-419F-832A-40D8915386C6(a)microsoft.com...
>
>
> "Marc M" <marc.manfredi(a)gmail.com> said this in news item
> news:02b1b415-6ee8-4b86-8567-54a2ad17f8d7(a)28g2000vbf.googlegroups.com...
>> On Feb 18, 1:47 am, "Pegasus [MVP]" <n...(a)microsoft.com> wrote:
>>> "Marc M" <marc.manfr...(a)gmail.com> said this in news
>>> itemnews: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.
>>
>> Agreed. Thanks, I see the additional ' that was on the line. I've
>> removed it but it still doesn't rename the files. It seems that this
>> script is way out of my league and I'm not sure I can get it working.
>> Thanks again for your comments.
>>
>> Regards,
>> Marc
>
> When you need a script then you have two options:
> a) You pay someone to write it for you. This gives you a quick result and
> the author gets compensated for his expertise.
> b) You get a large box of elbow grease, then make the effort to learn the
> language. This takes time but is ultimately more rewarding because it
> empowers you to do things by yourself.
>
> It seems that you want the quick result without having to pay for it and
> without making the effort to learn the script. You actually received 99.9%
> of the code from two respondents but ultimately missed the remaining 0.1%
> because you did not have the patience to implement the trouble shooting
> hint that I gave you. Your script DID actually rename files but it did not
> rename them correctly. The reason is embarassingly simple: You wrote
>
> oFile.Name = Left(oFile.name, Len(oFile.Name) - 4) & "stdf"
> (thus chopping 4 characters off the end of the file name)
> instead of
> oFile.Name = Left(oFile.name, Len(oFile.Name) - 8) & "stdf"
> (now truncating 8 characters, because "stdf_tmp" is 8 characters long!)
>
> Here is a fully tested version of your code. It works and I hope that it
> encourages you to acquire the skill of programming yourself instead just
> holding out your hand.
>
> 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
> WScript.Echo "Rename", oFile.Name, "to", Left(oFile.name,
> Len(oFile.Name) - 8) & "stdf"
> On Error Resume Next
> oFile.Name = Left(oFile.name, Len(oFile.Name) - 8) & "stdf"
> If Err.Number <> 0 _
> Then MsgBox "Cannot rename " & oFile.Name & vbLf & Err.Description
> On Error Goto 0
> End If
> Next

The only potential issue I see with the approach has to do with maintenance.
If, for example, it was necessary to adapt this code to rename
"*.std_tempor" files as "*.std", one would need to remember to change both
instances of "8" to "10". I would tend to use len(sFileType) instead of the
constant in order to avoid that problem.

Also, since the .getextensionname method is being used, I would tend to
create the resulting filename using the .buildpath method instead of simple
concatenation. I'm not saying that it is wrong to use concatenation, just
that switching between different ways of parsing filenames is an additional
level of complexity that can complicate maintenance.

/Al