Question Locked

Deleting Files

0 helpful
8 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Const ForReading = 1
Hey I got a problem deleting files. I wrote a script to delete files, heres how it works...

It reads a txt file that contains a folder path in each line then attemps to delete the files in the folder. It works great wtih the exception of files that the name is to long. It just skips these like they dont exist. Also even though I have on error resume next enabled it wants to loop into its self forcing the script to never end.

Any ideas on how to fix this... heres what I have so far.

Set objTextFile = FSO.OpenTextFile ("Files to be Deleted.txt", ForReading)
Do Until objTextFile.AtEndOfStream
NextLine = objTextFile.Readline
Set Fol = FSO.GetFolder(NextLine)
For Each File in Fol.Files

FSO.DeleteFile File.path, True
Next
Loop
On error GoTo 0

8 replies

by himdownstairs

but from the programming class I took, it looks like your For loop needs a condition to execute while the loop is true.

by james.carver

Not sure what you mean. The loop is not for the For statement its fo the Do Until. And to be honest I dont think i need the True it can be inplyied if im not mistatken. What do u mean by a condition, can you give me an example?

-Thanks

Set objTextFile = FSO.OpenTextFile ("Files to be Deleted.txt", ForReading)
Do Until objTextFile.AtEndOfStream
NextLine = objTextFile.Readline
Set Fol = FSO.GetFolder(NextLine)
For Each File in Fol.Files
FSO.DeleteFile File.path, True
Next
Loop
On error GoTo 0

by dumphrey

at your lst of files/paths, is it posssible the longer names are being trunkated if they are multiple words?
example:
c:\folder1\folder2\file1.txt good
c:\folder1\folder2\file 2.txt bad

If so,
c:\folder1\folder2\"file 2.txt"

will fix the problem. I realize this is a basic route answer, but I write basic shell scripts as the extent of my programming, and it pops up a lot.

But, it brings up the point,
It is possible your code is good and the input is off...somehow.

by james.carver

I woudlnt say its that.

Although some of them have a space in their name not all of them do. Here is an example of the file name it is having trouble with. I did notice it does not have a file extention.

dref[1].reply%2526friendId%253D125460220%2526type%253DInbox%2526messageID%253D465653550%2526fed%253DTrue%2526MyToken%253D8535af40-9d86-414a-a89b-9bb6e6efdb4a

This is the path to the file.
C:\Documents and Settings\james.carver\Local Settings\Temporary Internet Files\Content.IE5\90ZXK9J4

by Member 25348569

However, the example that you've given above doesn't look too long.

If you were dealing with over-long paths (and have users like mine) you need something to trap the file path when the length of the combined folder and file name are too long - depending on the file type this can be as low as 219 characters. Then you can either skip the delete or map a drive to the folder - on a local drive, SUBST will work - on a Network drive you use the following

At the top

[b]Set oNet=CreateObject("WScript.Network")[/b]

Within the Do Loop and using your Fol object...
[b]
oNet.MapNetworkDrive "T:", Fol.Path
Set oFolder=FSO.GetFolder("T:\")
[/b]
And then carry on from where you left off and delete the files in the oFolder object...

Finish with [b]oNet.RemoveNetworkDrive "T:", True[/b] to clean up each iteration.

Neil :)

However, I will say again that your names don't look too long. There may be another reason why the files aren't being deleted.

by james.carver

There not locked files by any means. What vbs i have posted is only a small part of the main script but its the part with the problem. The main script will acually check for files in use and write their file name to a log. Also if I manually delete them it works fine. One other thing I notice is in the scrip I have it getting the path for each file and it acts like there not even there but when it does that it skips any other files in the folder...

This discussion is closed to new replies.