Photo Gallery
Member List
Search
Calendars
FAQ
Ticket List
Log Out
Forums
Register
Login
My Profile
Inbox
Address Book
My Subscription
My Forums
Replace function in VBSript
Logged in as: Guest
arrSession:exec spGetSession 2,2,40873
Active Users: There are
0
members and
0
guests.
Users viewing this topic: none
Printable Version
All Forums
>>
[Scripting]
>>
WSH & Client Side VBScript
>> Replace function in VBSript
Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page:
[1]
Login
Message
<< Older Topic
Newer Topic >>
Replace function in VBSript -
12/13/2006 4:21:53 AM
caryma77
Posts: 27
Score: 0
Joined: 9/19/2006
Status:
offline
I am trying to use a replace function but keeps getting an error file exist.
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(FolderDir)
Set fc = f.Files
For Each f1 in fc
f1.Name = Replace(f1.Name,".GZ","")
Next
Post #: 1
RE: Replace function in VBSript -
12/13/2006 5:07:08 AM
ginolard
Posts: 1082
Score: 21
Joined: 8/10/2005
Status:
offline
Replace is for replacing strings not for renaming files.
_____________________________
Author of ManagePC -
http://managepc.net
AD Query Template -
http://www.visualbasicscript.com/m_40609/tm.htm
Consolidated Scripting Framework -
http://www.visualbasicscript.com/m_59109/tm.htm
(in reply to
caryma77
)
Post #: 2
RE: Replace function in VBSript -
12/13/2006 5:43:41 AM
ehvbs
Posts: 2220
Score: 50
Joined: 6/22/2005
From: Germany
Status:
offline
Hi carym77,
this output:
=== RenFile: Rename files ===================== 0 test0.txt 1 test0 2 test0.txt -> test0 ******************** 0 test1.txt 1 test1 2 test1.txt -> test1 ******************** 0 test2 1 test2 ******************** 0 test2.txt 1 test2 2 test2.txt -> test2 *** Die Datei ist bereits vorhanden. ******************** 0 test3.txt 1 test3 2 test3.txt -> test3 ******************** === RenFile: 0 done (00:00:00) ================
and this code:
Dim sFSpec : sFSpec = ".\renametest" ' should exist and be empty Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" ) oFS.deleteFile sFSpec + "\*.*" Dim nIdx For nIdx = 0 To 3 With oFS.CreateTextFile( sFSpec + "\test" & nIdx & ".txt", True ) .WriteLine "rename me!" .Close End With Next If True Then ' True for trouble With oFS.CreateTextFile( sFSpec + "\test2", True ) .WriteLine "rename me!" .Close End With End If Dim oFld : Set oFld = oFS.GetFolder( sFSpec ) Dim oFil, sNewName For Each oFil in oFld.Files sNewName = oFil.Name WScript.Echo 0, sNewName sNewName = Replace( sNewName, ".txt", "" ) WScript.Echo 1, sNewName If sNewName <> oFil.Name Then WScript.Echo 2, oFil.Name, "->", sNewName On Error Resume Next oFil.Name = sNewName If 0 <> Err.Number Then WScript.Echo "*** ", Err.Description On Error GoTo 0 End If WScript.Echo String( 20, "*" ) Next
show that
(a) your technique to rename a file is sound in principle
(b) there must be a file with sNewName already in your folder
(in reply to
ginolard
)
Post #: 3
If you found our site useful please link to us
<a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>
.
All Forums
>>
[Scripting]
>>
WSH & Client Side VBScript
>> Replace function in VBSript
Page:
[1]
Jump to:
Select a Forum
All Forums
----------------------
[Welcome]
- - Forum Rules
- - Test Posting Messages
- - New Member Area/Introduction
[Scripting]
- - WSH & Client Side VBScript
- - WSH & Client Side VBScript Tutorial
- - Post a VBScript
- - Windows PowerShell
- - ASP
- - ASP.NET
- - Windows Script Components
[General Forum]
- - Other Programming/Scripting Languages
- - Suggestions & Feedback
- - Off-Topic Lounge
New Messages
No New Messages
Hot Topic w/ New Messages
Hot Topic w/o New Messages
Locked w/ New Messages
Locked w/o New Messages
Post New Thread
Reply to Message
Post New Poll
Submit Vote
Delete My Own Post
Delete My Own Thread
Rate Posts
Forum Software ©
ASPPlayground.NET
Advanced Edition
2.5.5 ANSI