From: "Dave "Crash" Dummy" on
Oleg wrote:
> Can someone tell if its possible to replace the basename of a number
> of .jpg files in specified folder to match their date stamp?
> file.DateCreated can see that date, but how can I actually rename the
> files? Thanks!

The script below will do what you want. I recommend using
DateLastModified instead of DateCreated. That is what is displayed in
Explorer. The DateCreated changes every time the file is copied.

The date-time string contains illegal characters for a filename (/,:), so
those have to be replaced. Another possible trouble spot, one that is
not dealt with in this script, is two files having the same date-time.

'=================rename.vbs================
set fso=CreateObject("Scripting.FileSystemObject")
set objFolder=fso.getFolder("d:\pics")

for each file in objFolder.files
filename=LCase(file.name)
if right(filename,4)=".jpg" then
newname=file.dateLastModified
newname=replace(newname,"/","-")
newname=replace(newname,":","-")
file.name=newname & ".jpg"
end if
next
'=========================================
--
Crash

"Something there is that doesn't love a wall, that wants it down."
~ Robert Frost ~
From: Oleg on
Thanks, that works !:) I didnt know the code that told the script to actually
do the job, now its clear.
However, it runs into the problem when 2 files have the identical timestamp,
then the script terminates with an error.
Is it possible to use fso.MoveFile for the collection of files? So that the
script first renames and moves file to the destination folder, then processes
another.
Sorry for the newby questions, im pretty new to VBScript...

Oleg

"Dave "Crash" Dummy" wrote:

> Oleg wrote:
> > Can someone tell if its possible to replace the basename of a number
> > of .jpg files in specified folder to match their date stamp?
> > file.DateCreated can see that date, but how can I actually rename the
> > files? Thanks!
>
> The script below will do what you want. I recommend using
> DateLastModified instead of DateCreated. That is what is displayed in
> Explorer. The DateCreated changes every time the file is copied.
>
> The date-time string contains illegal characters for a filename (/,:), so
> those have to be replaced. Another possible trouble spot, one that is
> not dealt with in this script, is two files having the same date-time.
>
> '=================rename.vbs================
> set fso=CreateObject("Scripting.FileSystemObject")
> set objFolder=fso.getFolder("d:\pics")
>
> for each file in objFolder.files
> filename=LCase(file.name)
> if right(filename,4)=".jpg" then
> newname=file.dateLastModified
> newname=replace(newname,"/","-")
> newname=replace(newname,":","-")
> file.name=newname & ".jpg"
> end if
> next
> '=========================================
> --
> Crash
>
> "Something there is that doesn't love a wall, that wants it down."
> ~ Robert Frost ~
> .
>
From: James Watkins on

"Dave "Crash" Dummy" <invalid(a)invalid.invalid> wrote in message
news:cAqOn.91072$rE4.32891(a)newsfe15.iad...
> Oleg wrote:
>> Can someone tell if its possible to replace the basename of a number
>> of .jpg files in specified folder to match their date stamp?
>> file.DateCreated can see that date, but how can I actually rename the
>> files? Thanks!
>
> The script below will do what you want. I recommend using
> DateLastModified instead of DateCreated. That is what is displayed in
> Explorer. The DateCreated changes every time the file is copied.
>
> The date-time string contains illegal characters for a filename (/,:), so
> those have to be replaced. Another possible trouble spot, one that is
> not dealt with in this script, is two files having the same date-time.
>
> '=================rename.vbs================
> set fso=CreateObject("Scripting.FileSystemObject")
> set objFolder=fso.getFolder("d:\pics")
>
> for each file in objFolder.files
> filename=LCase(file.name)
> if right(filename,4)=".jpg" then
> newname=file.dateLastModified
> newname=replace(newname,"/","-")
> newname=replace(newname,":","-")
> file.name=newname & ".jpg"
> end if
> next
> '=========================================
> --
> Crash
>
> "Something there is that doesn't love a wall, that wants it down."
> ~ Robert Frost ~

The OP's question was "Can someone tell if its possible to replace the
basename of a number of .jpg files in specified folder to match their date
stamp?". Your comprehensive reply reminds me of the saying "Give a man a
fish; you have fed him for today. Teach a man to fish; and you have fed him
for a lifetime".


From: Pegasus [MVP] on
Moving the file before renaming it would mitigate the problem but not
eliminate it. To really eliminate it you need to expand the file name with a
numerical suffix that you keep increasing until it becomes unique. A better
alternative would be not to rename the file but to add the date stamp to the
existing file name. In this way you would not get any duplicate names at
all.

"Oleg" <Oleg(a)discussions.microsoft.com> wrote in message
news:DA63C844-D044-437C-92FD-3C395C7E02DC(a)microsoft.com...
> Thanks, that works !:) I didnt know the code that told the script to
> actually
> do the job, now its clear.
> However, it runs into the problem when 2 files have the identical
> timestamp,
> then the script terminates with an error.
> Is it possible to use fso.MoveFile for the collection of files? So that
> the
> script first renames and moves file to the destination folder, then
> processes
> another.
> Sorry for the newby questions, im pretty new to VBScript...
>
> Oleg
>
> "Dave "Crash" Dummy" wrote:
>
>> Oleg wrote:
>> > Can someone tell if its possible to replace the basename of a number
>> > of .jpg files in specified folder to match their date stamp?
>> > file.DateCreated can see that date, but how can I actually rename the
>> > files? Thanks!
>>
>> The script below will do what you want. I recommend using
>> DateLastModified instead of DateCreated. That is what is displayed in
>> Explorer. The DateCreated changes every time the file is copied.
>>
>> The date-time string contains illegal characters for a filename (/,:), so
>> those have to be replaced. Another possible trouble spot, one that is
>> not dealt with in this script, is two files having the same date-time.
>>
>> '=================rename.vbs================
>> set fso=CreateObject("Scripting.FileSystemObject")
>> set objFolder=fso.getFolder("d:\pics")
>>
>> for each file in objFolder.files
>> filename=LCase(file.name)
>> if right(filename,4)=".jpg" then
>> newname=file.dateLastModified
>> newname=replace(newname,"/","-")
>> newname=replace(newname,":","-")
>> file.name=newname & ".jpg"
>> end if
>> next
>> '=========================================
>> --
>> Crash
>>
>> "Something there is that doesn't love a wall, that wants it down."
>> ~ Robert Frost ~
>> .
>>
From: "Dave "Crash" Dummy" on
Oleg wrote:
> Thanks, that works !:) I didnt know the code that told the script to
> actually do the job, now its clear. However, it runs into the problem
> when 2 files have the identical timestamp, then the script terminates
> with an error. Is it possible to use fso.MoveFile for the collection
> of files? So that the script first renames and moves file to the
> destination folder, then processes another.

Pegasus' second suggestion, appending the date-time string to the
original name, is the simplest. His first suggestion, adding a numerical
suffix, can be implemented using a dictionary object. The reworked
script below does that.

'=================rename.vbs================
set fso=CreateObject("Scripting.FileSystemObject")
set objFolder=fso.getFolder("e:\pics")

Set d=CreateObject("Scripting.Dictionary")

for each file in objFolder.files
filename=LCase(file.name)
if right(filename,4)=".jpg" then
newname=file.dateLastModified
newname=replace(newname,"/","-")
newname=replace(newname,":","-")
if d.Exists(newname) then
d.Item(newname)=d.Item(newname)+1
else
d.Add newname,1
end if
file.name=newname & "-" & d.Item(newname) & ".jpg"
end if
next
'=========================================
--
Crash

"The real question is not whether machines think but whether men do."
~ B. F. Skinner ~
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4
Prev: Read Text file, display output
Next: VBScript & Lotus Notes