how to delete & rename file in vbs without using WMI class

W

williamkow

Can you advise me on how to delete a file and then rename a file using vbs,
but without using WMI class. Many thanks in advance.

set fsoA = WScript.CreateObject("Scripting.FileSystemObject")
set fsoB = WScript.CreateObject("Scripting.FileSystemObject")
set fsoC = WScript.CreateObject("Scripting.FileSystemObject")

set ifile1 = fsoA.OpenTextFile("C:\DATA\INdata.txt", 1, False)
set ofile1 = fsoB.CreateTextFile("C:\DATA\OUTFILE1.txt", True)
set ofile2 = fsoC.CreateTextFile("C:\DATA\OUTFILE2.txt", True)

If ofile1.FileExists(inFile) Then
'..... read the ifile1, then processing, output to ofile1 and ofile2
ofile1.writeline ....
ofile2.writeline ....
End If

' how to delete the INdata.txt
' how to rename the OUTFILE2.txt to INdata.txt

ifile1.Close
ofile1.close
ofile2.close
 

My Computer

P

Pegasus [MVP]

"williamkow" <[email protected]> wrote in message
news:[email protected]

> Can you advise me on how to delete a file and then rename a file using
> vbs,
> but without using WMI class. Many thanks in advance.
>
> set fsoA = WScript.CreateObject("Scripting.FileSystemObject")
> set fsoB = WScript.CreateObject("Scripting.FileSystemObject")
> set fsoC = WScript.CreateObject("Scripting.FileSystemObject")
>
> set ifile1 = fsoA.OpenTextFile("C:\DATA\INdata.txt", 1, False)
> set ofile1 = fsoB.CreateTextFile("C:\DATA\OUTFILE1.txt", True)
> set ofile2 = fsoC.CreateTextFile("C:\DATA\OUTFILE2.txt", True)
>
> If ofile1.FileExists(inFile) Then
> '..... read the ifile1, then processing, output to ofile1 and ofile2
> ofile1.writeline ....
> ofile2.writeline ....
> End If
>
> ' how to delete the INdata.txt
> ' how to rename the OUTFILE2.txt to INdata.txt
>
> ifile1.Close
> ofile1.close
> ofile2.close
You can use the Delete and the MoveFile methods of the File System Object.
Download the helpfile script56.chm from the Microsoft site to see the exact
syntax and some examples.
 

My Computer

P

Paul Randall

"Dave "Crash" Dummy" <[email protected]> wrote in message
news:[email protected]

> williamkow wrote:

>> Can you advise me on how to delete a file and then rename a file using
>> vbs, but without using WMI class. Many thanks in advance.
>>
>> set fsoA = WScript.CreateObject("Scripting.FileSystemObject")
>> set fsoB = WScript.CreateObject("Scripting.FileSystemObject")
>> set fsoC = WScript.CreateObject("Scripting.FileSystemObject")
>>
>> set ifile1 = fsoA.OpenTextFile("C:\DATA\INdata.txt", 1, False)
>> set ofile1 = fsoB.CreateTextFile("C:\DATA\OUTFILE1.txt", True)
>> set ofile2 = fsoC.CreateTextFile("C:\DATA\OUTFILE2.txt", True)
>>
>> If ofile1.FileExists(inFile) Then
>> '..... read the ifile1, then processing, output to ofile1 and ofile2
>> ofile1.writeline ....
>> ofile2.writeline ....
>> End If
>>
>> ' how to delete the INdata.txt
>> ' how to rename the OUTFILE2.txt to INdata.txt ifile1.Close
>> ofile1.close
>> ofile2.close
>
> First of all, you only need one File System Object. Then, after all files
> are closed,
>
> fso.deleteFile "C:\DATA\INdata.txt"
> fso.MoveFile "C:\DATA\OUTFILE2.txt","C:\DATA\INdata.txt"
Besides fso.MoveFile, you can also use something like:
ofile2.name = "INdata.txt"

The scripting help file says this about the name property:
Sets or returns the name of a specified file or folder. Read/write.
object.Name [= newname]

-Paul Randall
 

My Computer

P

Paul Randall

"Dave "Crash" Dummy" <[email protected]> wrote in message
news:[email protected]

> Paul Randall wrote:

>> "Dave "Crash" Dummy" <[email protected]> wrote in message
>> news:[email protected]

>>> williamkow wrote:
>>>> Can you advise me on how to delete a file and then rename a file using
>>>> vbs, but without using WMI class. Many thanks in advance.
>>>>
>>>> set fsoA = WScript.CreateObject("Scripting.FileSystemObject")
>>>> set fsoB = WScript.CreateObject("Scripting.FileSystemObject")
>>>> set fsoC = WScript.CreateObject("Scripting.FileSystemObject")
>>>>
>>>> set ifile1 = fsoA.OpenTextFile("C:\DATA\INdata.txt", 1, False)
>>>> set ofile1 = fsoB.CreateTextFile("C:\DATA\OUTFILE1.txt", True)
>>>> set ofile2 = fsoC.CreateTextFile("C:\DATA\OUTFILE2.txt", True)
>>>>
>>>> If ofile1.FileExists(inFile) Then
>>>> '..... read the ifile1, then processing, output to ofile1 and ofile2
>>>> ofile1.writeline ....
>>>> ofile2.writeline ....
>>>> End If
>>>>
>>>> ' how to delete the INdata.txt
>>>> ' how to rename the OUTFILE2.txt to INdata.txt ifile1.Close
>>>> ofile1.close
>>>> ofile2.close
>>> First of all, you only need one File System Object. Then, after all
>>> files are closed,
>>>
>>> fso.deleteFile "C:\DATA\INdata.txt"
>>> fso.MoveFile "C:\DATA\OUTFILE2.txt","C:\DATA\INdata.txt"
>>
>> Besides fso.MoveFile, you can also use something like:
>> ofile2.name = "INdata.txt"
>>
>> The scripting help file says this about the name property:
>> Sets or returns the name of a specified file or folder. Read/write.
>> object.Name [= newname]
>
> Not quite. You have set the file as an object, first:
>
> set oFile=fso.getFile("OUTFILE2.txt")
> oFile.name="INdata.txt"
Are you saying that after:

>>> fso.deleteFile "C:\DATA\INdata.txt"
and before:
that:
ofile2.name = "INdata.txt"
won't work?

-Paul Randall
 

My Computer

D

Dave \Crash\ Dummy

Paul Randall wrote:

> Are you saying that after:

>>>> fso.deleteFile "C:\DATA\INdata.txt"
> and before:
> that:
> ofile2.name = "INdata.txt"
> won't work?
Yes. You are confusing a TextStream object with a File object.

http://msdn.microsoft.com/en-us/library/312a5kbt(VS.85).aspx
http://msdn.microsoft.com/en-us/library/1ft05taf(VS.85).aspx
--
Crash

"It's easier to ask forgiveness than it is to get permission."
~ Grace Hopper (RADM, USNR) ~
 

My Computer

P

Paul Randall

"Dave "Crash" Dummy" <[email protected]> wrote in message
news:[email protected]

> Paul Randall wrote:
>

>> Are you saying that after:

>>>>> fso.deleteFile "C:\DATA\INdata.txt"
>> and before:

>>>>>> ofile2.close
>> that:
>> ofile2.name = "INdata.txt"
>> won't work?
>
> Yes. You are confusing a TextStream object with a File object.
>
> http://msdn.microsoft.com/en-us/library/312a5kbt(VS.85).aspx
> http://msdn.microsoft.com/en-us/library/1ft05taf(VS.85).aspx
> --
> Crash
You are right. I should have refreshed my memory from the scripting help
file. Thanks for correcting me.

-Paul Randall
 

My Computer

Top