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

Remove specific items from file name in folder

14 views
Skip to first unread message

cjake2299

unread,
Dec 17, 2009, 12:14:25 PM12/17/09
to
Greetings all-

I have a directory that contains image files (lots of them) that all
end with "_STD.jpg". I need to remove the "_STD" portion of all the
file names, as well as return a count of all files processed. Any
help would be much appreciated.

Pegasus [MVP]

unread,
Dec 17, 2009, 12:43:29 PM12/17/09
to

"cjake2299" <cjak...@gmail.com> screv in
news:7db38b3f-38db-4d67...@f18g2000prf.googlegroups.com...

There are numerous free bulk rename utilities that will perform exactly this
task. Just put the words "windows bulk rename" into a Google search box. If
you prefer to roll your own then the File System Object has the appropriate
methods to tackle the job. Let's see what you have so far so that someone
can help you complete the script.

cjake2299

unread,
Dec 17, 2009, 12:57:10 PM12/17/09
to
On Dec 17, 9:43 am, "Pegasus [MVP]" <n...@microsoft.com> wrote:
> "cjake2299" <cjake2...@gmail.com> screv innews:7db38b3f-38db-4d67...@f18g2000prf.googlegroups.com...

Thanks for the prompt reply. I have already begun the process using
the following script (modified from another post):

const strSourceDir = "C:\ProductImages"
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")


Set colFiles = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='" & strSourceDir _
& "'} Where ResultClass = CIM_DataFile")


For Each objFile In colFiles
p = InStr(objFile.FileName, "_")
if p > 2 Then
strName = Left(objFile.FileName, p-1) & "."
strNewName = objFile.Drive & objFile.Path _
& strName & objFile.extension
errResult = objFile.Rename(strNewName)
end if
Next

It did not return a count of the objects, but I was able to get that
information anyways.

Pegasus [MVP]

unread,
Dec 17, 2009, 1:56:14 PM12/17/09
to

"cjake2299" <cjak...@gmail.com> schrieb im Newsbeitrag
news:4ecce8a2-19ed-4027...@u36g2000prn.googlegroups.com...

If you need to process *lots* of files (your words) then WMI is the perfect
method to slow things down to a snail's pace. The File System Object runs
circles around WMI and can easily do this particular job.

Pegasus [MVP]

unread,
Dec 17, 2009, 3:51:05 PM12/17/09
to

"cjake2299" <cjak...@gmail.com> schrieb im Newsbeitrag
news:4ecce8a2-19ed-4027...@u36g2000prn.googlegroups.com...

To satisfy my own curiosity I ran three quick tests. Each test script read
the names of 10,000 files, located in a single folder, into an array. To
avoid disk caching effects I used three separate folders and rebooted the
machine before commencing the test. Here are the methods I used and the test
results:

Method a): Use the FSO folder.files method. Time taken = 8.6 seconds.

Method b): Shell out to the command processor to invoke the command "dir /b
> dir.txt", then read dir.txt into an array using the FSO ReadAll method.
Time taken = 0.7 seconds. This is a very fast approach but not popular with
purists because it is a hybrid solution.

Method c): Use your own WMI program. Time taken: 450 seconds. This is
typical: WMI programs tend to run at least 40 times slower than their FSO
equivalents.

I rest my case about WMI.

Dave "Crash" Dummy

unread,
Dec 17, 2009, 9:52:33 PM12/17/09
to

Maybe I'm missing something, but wouldn't this work?

Set fso = CreateObject("Scripting.FileSystemObject")
set fldr=fso.getFolder("d:\directory")
filecount=0
for each file in fldr.files
if instr(ucase(file.name),"_STD.JPG") then
dname=replace(file.path,"_STD","")
file.move dname
filecount=filecount+1
end if
next

--
Crash

Committed to the search for intraterrestrial intelligence.

Paul Randall

unread,
Dec 18, 2009, 12:54:26 AM12/18/09
to

"Dave "Crash" Dummy" <inv...@invalid.invalid> wrote in message
news:umtye04f...@TK2MSFTNGP02.phx.gbl...

> cjake2299 wrote:
>> Greetings all-
>>
>> I have a directory that contains image files (lots of them) that all
>> end with "_STD.jpg". I need to remove the "_STD" portion of all the
>> file names, as well as return a count of all files processed. Any
>> help would be much appreciated.
>
> Maybe I'm missing something, but wouldn't this work?
>
> Set fso = CreateObject("Scripting.FileSystemObject")
> set fldr=fso.getFolder("d:\directory")
> filecount=0
> for each file in fldr.files
> if instr(ucase(file.name),"_STD.JPG") then
> dname=replace(file.path,"_STD","")
> file.move dname
> filecount=filecount+1
> end if
> next

The name property of a FSO file object has both read and write capabilities,
doesn't it?
Maybe omething like the following for changing the name?
file.name = replace(file.name, "_STD","")


Stefan Kanthak

unread,
Dec 18, 2009, 5:40:36 AM12/18/09
to
"Paul Randall" <paul...@cableone.net> wrote:
>
> "Dave "Crash" Dummy" <inv...@invalid.invalid> wrote in message
> news:umtye04f...@TK2MSFTNGP02.phx.gbl...
>> cjake2299 wrote:
>>> Greetings all-
>>>
>>> I have a directory that contains image files (lots of them) that all
>>> end with "_STD.jpg". I need to remove the "_STD" portion of all the
>>> file names, as well as return a count of all files processed. Any
>>> help would be much appreciated.
>>
>> Maybe I'm missing something, but wouldn't this work?

[ "bloated" VBScript ]

Why not a simple *.CMD?

ChDir /D "<directory>"
For %! In (*_STD.jpg) Do Rename "%!" "%!:_STD="

The count is left as exercise for the reader.

Stefan

Dave "Crash" Dummy

unread,
Dec 18, 2009, 9:54:24 AM12/18/09
to

Yes, it would. I adopted my script from another application, and there
was some reason (which I forget) that I couldn't do it directly. The two
lines

dname=replace(file.path,"_STD","")
file.move dname

could be replaced with

file.name = replace(file.name, "_STD","")

--
Crash

"When you get to a fork in the road, take it."
~ Yogi Berra ~

Dave "Crash" Dummy

unread,
Dec 18, 2009, 10:18:20 AM12/18/09
to
> I adopted my script from another application, and there
> was some reason (which I forget) that I couldn't do it directly.

Ah! Now I remember! I didn't want to change the name, exactly, I wanted
to change the case. I can't do that directly because VBS doesn't
distinguish case in file names. For example, if I try to change "_STD"
to "_std,"

This won't work:
file.name = replace(file.name, "_STD","_std")

This will:
dname=replace(file.path,"_STD","_std")
file.move dname

--
Crash

Life is short. Eat dessert first.

Evertjan.

unread,
Dec 18, 2009, 3:36:05 PM12/18/09
to
Dave "Crash" Dummy wrote on 18 dec 2009 in
microsoft.public.scripting.vbscript:

> > I adopted my script from another application, and there
> > was some reason (which I forget) that I couldn't do it directly.
>
> Ah! Now I remember! I didn't want to change the name, exactly, I wanted
> to change the case. I can't do that directly because VBS doesn't
> distinguish case in file names. For example, if I try to change "_STD"
> to "_std,"
>
> This won't work:
> file.name = replace(file.name, "_STD","_std")

this will, methinks:

file.name = replace(file.name, "_STD", "_stdTMPtmp")
file.name = replace(file.name, "_stdTMPtmp", "_std")



> This will:
> dname=replace(file.path,"_STD","_std")
> file.move dname
>

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

0 new messages