Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

rename part of a file 1

Status
Not open for further replies.

marshyrob

Technical User
Jan 20, 2004
137
GB
Hi all

i have a script that im trying to run that looks in a folder and renames the files in there based on the first 9 characters of their existing filename. For example the files are called:

ABCDEFG-2006-09-09.xxx

I want just the first 7 characters (ABCDEFG) and then the file extension .xxx ive tried the following but it does not seem to do anything?

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='C:\test'} Where " _
& "ResultClass = CIM_DataFile")

For Each objFile In colFiles
strStart = left(objFile.Name, 9)
strNewName = objFile.Drive & objFile.Path & strStart & ".ucs"
errResult = objFile.Rename(strNewName)
Next

The folder c:\test does exist. Any help would be appreciated but im a newbie to VBscript so be gentle!

Regards

Rob
 
Sometimes the simpler way is better.

create a dos batch file with the following


cd c:\test
rename *.ucs ???????.ucs



Sorted!
 
> strStart = left(objFile.Name, 9)
But you said (ABCDEFG) meaning 7 characters? In any case, the correct property is filename.
[tt] strStart = left(objFile.[red]filename[/red], [blue]7[/blue])[/tt]
 
Hi taupirho

That was a consideration of mine but the problem i have is that each file is named differently, so for example:

ABCDEGF-2006-09-09.xxx
HIJKLMN-2006-09-09.xxx
OPQRSTU-2006-09-09.xxx

All i want to keep is the letter (alphabet) part of the file name which is unique. If they were all to be renamed the same then your batch option would work.

Any other ideas you can think may help?

Rob
 
Hi tsuji

Thats done it! Thanks a lot. I did say 9 characters but in my example i used 7 sorry wasnt really thinking then. 9 was what i wanted and 9 is what will do the job.

Thanks

Problem sorted.

Rob

 
Yes, I understood your request. My DOPS batch file would have done what you wanted i.e after running it you would have had - as per your example - the following files:

ABCDEGF.xxx
HIJKLMN.xxx
OPQRSTU.xxx


Did you try it??
 
Can you rename on a remote m/c? That's the point.
 
i assumed that renaming *.ucs (every file with the extension of .ucs) to ?????.ucs woul name all .ucs files with what you choose instead of ????? but then all files would be named the same and that cant happen!

Anyhow tsuji's method was what i was looking for, and it worked a treat as im trying to learn VBscript.

I appreciate your help though and your method may come in handy on another occaision.

Anyone know why im getting this error when trying to run my ammended script on my machine, it works on another machine perfectly.

Line 4
Char 1
Error Library not registered
Code 8002801D
Source (null)

Done some searching and it seems its either a reg permissions thing or a version of WSH (which i have the latest!!)

Cheers

Rob

 
.Name is a read/write property of a File object so

aFile.Name = "newname"

will do
 
Hey Rob,

I was getting the same error on wshshell.run commands - and stumbled upon your post. I tried unregistering and re-registering the vbscript.dll to no avail.

Finally, as a stab in the dark, I downloaded and reinstalled WSH 5.6 (even though it was already installed), rebooted, and it resolved the problem.

just a thought; ymmv.


Braino!


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top