Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Rename PDF files with VBScript

444 views
Skip to first unread message

[oTTo]

unread,
Feb 28, 2013, 2:36:28 AM2/28/13
to
Hi,
I have a folder with lot of PDF files and I have to rename all the files,
I need to maintain only the part of the namefile beetwen the eighth character
and the sixteenth character, is possible to create a VBScript to do it ?


Example that I have:

xxxxxx_namefile1_xxxxxxxxxx.pdf
xxxxxx_namefile2_xxxxxxxxxxxxxx.pdf
xxxxxx_namefile3_xxxxxx.pdf
xxxxxx_namefile4_xxxxxxxxxxxxx.pdf
xxxxxx_namefile5_xxxx.pdf
...


Example that I need:

namefile1.pdf
namefile2.pdf
namefile3.pdf
namefile4.pdf
namefile5.pdf
...

Regards

Evertjan.

unread,
Feb 28, 2013, 3:22:14 AM2/28/13
to
[oTTo] wrote on 28 feb 2013 in microsoft.public.scripting.vbscript:

> I have a folder with lot of PDF files and I have to rename all the
> files, I need to maintain only the part of the namefile beetwen the
> eighth character and the sixteenth character, is possible to create a
> VBScript to do it ?

Yes, it is.

> Example that I have:
>
> xxxxxx_namefile1_xxxxxxxxxx.pdf
> xxxxxx_namefile2_xxxxxxxxxxxxxx.pdf
> xxxxxx_namefile3_xxxxxx.pdf
> xxxxxx_namefile4_xxxxxxxxxxxxx.pdf
> xxxxxx_namefile5_xxxx.pdf
> ...
>
>
> Example that I need:
>
> namefile1.pdf
> namefile2.pdf
> namefile3.pdf
> namefile4.pdf
> namefile5.pdf
> ...


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[oTTo]

unread,
Feb 28, 2013, 3:59:02 AM2/28/13
to
Il 28/02/2013 9.22, Evertjan. ha scritto:
> [oTTo] wrote on 28 feb 2013 in microsoft.public.scripting.vbscript:
>
>> I have a folder with lot of PDF files and I have to rename all the
>> files, I need to maintain only the part of the namefile beetwen the
>> eighth character and the sixteenth character, is possible to create a
>> VBScript to do it ?
>
> Yes, it is.

:-) anybody can help me to create the script ?

Regards

Auric__

unread,
Feb 28, 2013, 4:35:45 AM2/28/13
to
[oTTo] wrote:

> I have a folder with lot of PDF files and I have to rename all the
> files, I need to maintain only the part of the namefile beetwen the
> eighth character and the sixteenth character, is possible to create a
> VBScript to do it ?

Not tested very thoroughly. YMMV.

Set FSO = CreateObject("Scripting.FileSystemObject")
For Each file In FSO.GetFolder(".").Files
If (".pdf" = LCase(Right(file.Name, 4))) And (Len(file.Name) > 13) Then
tgt = Mid(file.Name, 8, 9) & ".pdf"
FSO.MoveFile file.Name, tgt
End If
Next

There are almost certainly better ways to do this, but in 30 seconds of
testing, it seems to work okay.

--
We are all kings or pawns.

[oTTo]

unread,
Feb 28, 2013, 5:29:27 AM2/28/13
to
Il 28/02/2013 10.35, Auric__ ha scritto:

> Not tested very thoroughly. YMMV.
>
> Set FSO = CreateObject("Scripting.FileSystemObject")
> For Each file In FSO.GetFolder(".").Files
> If (".pdf" = LCase(Right(file.Name, 4))) And (Len(file.Name)> 13) Then
> tgt = Mid(file.Name, 8, 9)& ".pdf"
> FSO.MoveFile file.Name, tgt
> End If
> Next
>
> There are almost certainly better ways to do this, but in 30 seconds of
> testing, it seems to work okay.
>

Perfect ! it works fine !
Thanks a lot

Mayayana

unread,
Feb 28, 2013, 9:24:13 AM2/28/13
to
| Perfect ! it works fine !
| Thanks a lot
|

You might want to download the help files for Windows
Script Host:

http://www.microsoft.com/downloads/details.aspx?FamilyId=01592C48-207D-4BE1-8A76-1C4099D7BBB9

It's reasonably good documentation, and there are
not really a lot of VBScript methods to learn. Things
like Len, Right, Mid, InStr are basics that one can't
really write a script without.


Evertjan.

unread,
Feb 28, 2013, 10:11:38 AM2/28/13
to
Don't discount Regular Expressions, when using regex a fixed position is
not even necessary for replacing or deleting parts of a string.
It takes some learning, but you will enjoy this the rest of your life.

s1 = "xxxxxx_namefile1_xxxxxxxxxx.pdf"
Set regEx = New RegExp
regEx.Pattern = "^[^_]+_([^_]+)[^.]+(.*)$"
s3 = regEx.Replace(s1, "$1$2")
Set regEx = Nothing

Or use the split():

s1 = "xxxxxx_namefile1_xxxxxxxxxx.pdf"
s2 = split(s1,"_")
s3 = s2(1) & ".pdf"

[tested on IE10]

============================

Off topic, I know:
Javascript does the same, but looks more elegant:

var s1 = 'xxxxxx_namefile1_xxxxxxxxxx.pdf'
var s3 = s1.replace(/^[^_]+_([^_]+)[^.]+(.*)$/, '$1$2');

Or use the split():

var s1 = 'xxxxxx_namefile1_xxxxxxxxxx.pdf';
var s3 = s1.split('_')[1] + '.pdf';

[tested on Chrome]

Auric__

unread,
Feb 28, 2013, 1:57:47 PM2/28/13
to
Evertjan. wrote:

> Mayayana wrote on 28 feb 2013 in microsoft.public.scripting.vbscript:
>
>> You might want to download the help files for Windows
>> Script Host:
>>
>> http://www.microsoft.com/downloads/details.aspx?FamilyId=01592C48-207D-
>> 4BE1-8A76-1C4099D7BBB9
>>
>> It's reasonably good documentation, and there are
>> not really a lot of VBScript methods to learn. Things
>> like Len, Right, Mid, InStr are basics that one can't
>> really write a script without.
>
> Don't discount Regular Expressions, when using regex a fixed position is
> not even necessary for replacing or deleting parts of a string.
> It takes some learning, but you will enjoy this the rest of your life.

In this case, regexes aren't really needed, since the op wants "only the
part of the namefile beetwen the eighth character and the sixteenth
character", i.e. Mid(file.Name, 8, 9) as I posted earlier. IMO a regex is
overkill for this, especially since the string handling stuff is built into
the language (and has been for about a thousand years).

Plus, changing the Mid() line (in case the format of the filenames changes)
is pretty straightforward... but the regex looks like line noise to me.

> Or use the split():
>
> s1 = "xxxxxx_namefile1_xxxxxxxxxx.pdf"
> s2 = split(s1,"_")
> s3 = s2(1) & ".pdf"
>
> [tested on IE10]
>
> ============================
>
> Off topic, I know:
> Javascript does the same, but looks more elegant:
>
> var s1 = 'xxxxxx_namefile1_xxxxxxxxxx.pdf'
> var s3 = s1.replace(/^[^_]+_([^_]+)[^.]+(.*)$/, '$1$2');
>
> Or use the split():
>
> var s1 = 'xxxxxx_namefile1_xxxxxxxxxx.pdf';
> var s3 = s1.split('_')[1] + '.pdf';

More overkill, IMO. Why build an array when you only need one piece of it?

If the location of the underscores differed from file to file, this might
make sense, but not with the example names the op posted.

--
No! We stay and do our job!

Evertjan.

unread,
Feb 28, 2013, 5:04:17 PM2/28/13
to
Auric__ wrote on 28 feb 2013 in microsoft.public.scripting.vbscript:

> Evertjan. wrote:
>
>> Mayayana wrote on 28 feb 2013 in microsoft.public.scripting.vbscript:
>>
>>> You might want to download the help files for Windows
>>> Script Host:
>>>
>>> http://www.microsoft.com/downloads/details.aspx?FamilyId=01592C48-207
>>> D- 4BE1-8A76-1C4099D7BBB9
>>>
>>> It's reasonably good documentation, and there are
>>> not really a lot of VBScript methods to learn. Things
>>> like Len, Right, Mid, InStr are basics that one can't
>>> really write a script without.
>>
>> Don't discount Regular Expressions, when using regex a fixed position
>> is not even necessary for replacing or deleting parts of a string.
>> It takes some learning, but you will enjoy this the rest of your
>> life.
>
> In this case, regexes aren't really needed, since the op wants "only
> the part of the namefile beetwen the eighth character and the
> sixteenth character", i.e. Mid(file.Name, 8, 9) as I posted earlier.
> IMO a regex is overkill for this, especially since the string handling
> stuff is built into the language (and has been for about a thousand
> years).
>
> Plus, changing the Mid() line (in case the format of the filenames
> changes) is pretty straightforward... but the regex looks like line
> noise to me.

What nonsense, to call anything you do not understand an overkill.

To me, and I speak some Algol, Fortran and fluently ancient Basic,
regex is simpler than counting places.

Probably the OP did specify positions, because he did not understand the
ease of using the quoted underscores as delimiters, not needing to specify
the length of the numbers.

>> Or use the split():
>>
>> s1 = "xxxxxx_namefile1_xxxxxxxxxx.pdf"
>> s2 = split(s1,"_")
>> s3 = s2(1) & ".pdf"
>>
>> [tested on IE10]
>>
>> ============================
>>
>> Off topic, I know:
>> Javascript does the same, but looks more elegant:
>>
>> var s1 = 'xxxxxx_namefile1_xxxxxxxxxx.pdf'
>> var s3 = s1.replace(/^[^_]+_([^_]+)[^.]+(.*)$/, '$1$2');
>>
>> Or use the split():
>>
>> var s1 = 'xxxxxx_namefile1_xxxxxxxxxx.pdf';
>> var s3 = s1.split('_')[1] + '.pdf';
>
> More overkill, IMO. Why build an array when you only need one piece of
> it?

I would understand if it were your 'humble' opinion, but it is not, as a
slit works as fast as en inferred split, using the mid() function.

Mind, I don't say my examples are better, but let us give the OP to decide
what [s]he likes best.

> If the location of the underscores differed from file to file, this
> might make sense, but not with the example names the op posted.

Nonsense again, it makes a lot of sense,
the ways are just different, not better or worse.

Mayayana

unread,
Feb 28, 2013, 5:22:01 PM2/28/13
to
> tgt = Mid(file.Name, 8, 9) & ".pdf"
> var s3 = s1.replace(/^[^_]+_([^_]+)[^.]+(.*)$/, '$1$2');

| Nonsense again, it makes a lot of sense,
| the ways are just different, not better or worse.

I can never figure out why Regular Expression
fans are as irrationally enthusiastic as Apple addicts.
There must be some kind of kick in that orgy of
abstruseness that I'm just not getting.


Evertjan.

unread,
Feb 28, 2013, 5:48:40 PM2/28/13
to
Mayayana wrote on 28 feb 2013 in microsoft.public.scripting.vbscript:

So if I say "not better or worse",
I am "irrationally enthusiastic"?

Perhaps it is you who is phobic
of techniques you do not understand [yet?]?

Auric__

unread,
Feb 28, 2013, 6:34:52 PM2/28/13
to
Evertjan. wrote:

> Mind, I don't say my examples are better, but let us give the OP to
> decide what [s]he likes best.

Agreed. My post came off as way too argumentative.

(But I stand by my not-so-humble opinion!)

--
We're supposed to walk outta there with a hundred and
fifty million dollars in cash without getting stopped?

Evertjan.

unread,
Mar 1, 2013, 5:08:41 AM3/1/13
to
Auric__ wrote on 01 mrt 2013 in microsoft.public.scripting.vbscript:

> Evertjan. wrote:
>
>> Mind, I don't say my examples are better, but let us give the OP to
>> decide what [s]he likes best.
>
> Agreed. My post came off as way too argumentative.

Okay.

> (But I stand by my not-so-humble opinion!)

While striving for maximum anonymity?

Auric__

unread,
Mar 1, 2013, 7:38:01 AM3/1/13
to
Evertjan. wrote:

> Auric__ wrote on 01 mrt 2013 in microsoft.public.scripting.vbscript:
>
>> Evertjan. wrote:
>>
>>> Mind, I don't say my examples are better, but let us give the OP to
>>> decide what [s]he likes best.
>>
>> Agreed. My post came off as way too argumentative.
>
> Okay.
>
>> (But I stand by my not-so-humble opinion!)
>
> While striving for maximum anonymity?

...what?

--
Actually, the answer is Blue. Because ice cream has no bones.

Mayayana

unread,
Mar 1, 2013, 8:58:11 AM3/1/13
to
| > Okay.
| >
| >> (But I stand by my not-so-humble opinion!)
| >
| > While striving for maximum anonymity?
|
| ...what?
|

His first answer to the OP was a wisecrack and his
later postings are just throwing around unnecessary
complexity to wow the spectators and confuse the OP.

He only comes to argue, so if you try to be gracious
he'll just argue with that. "When a pickpocket meets a
Zen master, all he sees is pockets." :)


Todd Vargo

unread,
Mar 1, 2013, 1:22:30 PM3/1/13
to
So much aggravation just to offer spoon-fed code.

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)
0 new messages