Windows Vista Forums

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


    williamkow Guest

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

    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 System SpecsSystem Spec

  2. #2


    Pegasus [MVP] Guest

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



    "williamkow" <williamkow@newsgroup> wrote in message
    news:3875F133-6BBD-4293-982F-5A09C88C084E@newsgroup

    > 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 System SpecsSystem Spec

  3. #3


    Paul Randall Guest

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


    "Dave "Crash" Dummy" <invalid@newsgroup> wrote in message
    news:TLvGn.8716$TL5.3404@newsgroup

    > 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 System SpecsSystem Spec

  4. #4


    Paul Randall Guest

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


    "Dave "Crash" Dummy" <invalid@newsgroup> wrote in message
    news_zGn.3525$yx.3173@newsgroup

    > Paul Randall wrote:

    >> "Dave "Crash" Dummy" <invalid@newsgroup> wrote in message
    >> news:TLvGn.8716$TL5.3404@newsgroup

    >>> 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:

    >>>> ofile2.close
    that:
    ofile2.name = "INdata.txt"
    won't work?

    -Paul Randall



      My System SpecsSystem Spec

  5. #5


    Dave \Crash\ Dummy Guest

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

    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/libr...bt(VS.85).aspx
    http://msdn.microsoft.com/en-us/libr...af(VS.85).aspx
    --
    Crash

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

      My System SpecsSystem Spec

  6. #6


    Paul Randall Guest

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


    "Dave "Crash" Dummy" <invalid@newsgroup> wrote in message
    news:WwHGn.3657$yx.3406@newsgroup

    > 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/libr...bt(VS.85).aspx
    > http://msdn.microsoft.com/en-us/libr...af(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 System SpecsSystem Spec

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Cant delete, rename, ect. tried admin rights, moveonboot, ect., and the file is empty dedicateddoug Vista file management 5 09 Oct 2009
how to delete/rename log file use by process Ajay Kumar VB Script 1 26 Dec 2008
Can't delete/rename file being previewed in Windows Explore Arlene Vista General 0 18 Nov 2008
Can't open, rename, delete, or move a file.... Paul MacFarlane Vista General 4 08 Aug 2007
Can't Delete/rename/move a file.. Paul MacFarlane Vista security 10 25 Jul 2007