Discussions Search    Reviews    Search Aid    Buzzzz    Google@Omgili Add to iGoogle   Bookmark and Share

  Advanced Search



Reading filename from a file and deleting

On Thu, 3 Jul 2008 01:21:29 -0700 (PDT), Kuldeep <...@gmail.com

I have a file "tmp.txt" in a format like

word0, c:\1.txt
word2, c:\2.txt
word4, c:\Copy of tmp1.txt

I need to delete all the files listed in 2nd column.

I tried this script.

Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\tmp.txt", ForReading)
Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
arrServiceList = Split(strNextLine , ",")
Wscript.Echo "File Name to be deleted : " & arrServiceList(1)
strFilePath = "arrServiceList(1)"
set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFile(strFilePath)
Loop

It gives me Error on line "objFSO.DeleteFile(strFilePath)"

File Not found.

The files do exist in the specified path. If i hardcode the path in
the script, It works fine.

This is my first script.
Please help

Kuldeep

 Latest discussions from this group  (microsoft.public.scripting.vbscript)

Writing information to text file  
[03 Sep 2008]
On Wed, 3 Sep 2008 13:32:01 -0700, whiggins <whiggins@discuss ions.microsoft.com Hello...
I am trying to gather some patch information for several machines and I wrote a little ...
Extended stored procedures and WSC  
[03 Sep 2008]
On Wed, 3 Sep 2008 07:30:01 -0700, Robert <Robert@discussio ns.microsoft.com Hi I have...
seen an example of how to wrap a com object up using TSQL and extended stored procedures...
Microsoft VBScript runtime error: Object doesn't support this property or...  
[03 Sep 2008]
On Wed, 3 Sep 2008 11:50:58 -0500, "Mark Dougherty" <dougherty@dmi-st l.com I have a VBscript...
VBscript that uses the "WScript.Sleep 10000" command to pause briefly. When I run the VBscript...
More...


On Thu, 3 Jul 2008 02:08:01 -0700, Bishop <...@discussions.microsoft.com

try this:

Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\tmp.txt", ForReading)

Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
arrServiceList = Split(strNextLine , ",")
Wscript.Echo "File Name to be deleted : " & arrServiceList(1)
strFilePath = arrServiceList(1)
If objFSO.FileExists(strFilePath) then
objFSO.DeleteFile strFilePath,True
End if
Loop


Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
arrServiceList = Split(strNextLine , ",")
Wscript.Echo "File Name to be deleted : " & arrServiceList(1)
strFilePath = arrServiceList(1)

If objFSO.FileExists(strFilePath) then
objFSO.DeleteFile strFilePath,True
End if

Loop



>

On Thu, 3 Jul 2008 02:12:02 -0700, Bishop <...@discussions.microsoft.com

Sorry, my fault.
try this:

Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\tmp.txt", ForReading)

Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
arrServiceList = Split(strNextLine , ",")
Wscript.Echo "File Name to be deleted : " & arrServiceList(1)
strFilePath = arrServiceList(1)
If objFSO.FileExists(strFilePath) then
objFSO.DeleteFile strFilePath,True
End if
Loop




>

On Thu, 3 Jul 2008 03:08:23 -0700 (PDT), Kuldeep <...@gmail.com

HI,
Thanks for the reply.
Now the Error message is gone. However the files are not deleted.

Kuldeep


On Jul 3, 2:12 pm, Bishop <...@discussions.microsoft.com

On Thu, 3 Jul 2008 03:50:00 -0700, Bishop <...@discussions.microsoft.com

I find the problem.
You use blanks in the file "tmp.txt"

here is the solution with "Trim":

Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\tmp.txt", ForReading)

Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
arrServiceList = Split(strNextLine , ",")
Wscript.Echo "File Name to be deleted : " & arrServiceList(1)
strFilePath = Trim(arrServiceList(1))
If objFSO.FileExists(strFilePath) then
objFSO.DeleteFile strFilePath,True
End if
Loop





>

On Thu, 3 Jul 2008 04:52:23 -0700 (PDT), Kuldeep <...@gmail.com

It works!!!
Thanks Bishop.
Your help is very much appreciated.

Kuldeep


On Jul 3, 3:50 pm, Bishop <...@discussions.microsoft.com

On Sun, 6 Jul 2008 14:16:26 -0600, "Al Dunbar" <...@hotmail.com.nospaam

IMHO, the only space character that cannot be part of the filename is the
one following the comma. I would forget the trim, but change this:

arrServiceList = Split(strNextLine , ",")

to this:


arrServiceList = Split(strNextLine , ", ")


/Al

"Kuldeep" <...@25g2000hsx.googlegroups.com...
It works!!!
Thanks Bishop.
Your help is very much appreciated.

Kuldeep


On Jul 3, 3:50 pm, Bishop <...@discussions.microsoft.com

Discussion Title: Reading filename from a file and deleting
Title Keywords: Reading  filename  from  file  deleting