loop through folder and delete txt files

  • Hi,

    I have a for each loop container that results a variable 'USER::Path' which holds a server path (\\serverA\folder1\folder2). So for each iteration the value of the 'USER::Path' changes, meaning for each iteration, variable gets a new server path .

    My question is how do I delete text files in those folders.

    thank you...

  • I have vb script to do the above task but I get the following errors

    Public Sub Main()

    Dim Path As String

    Dim FSO

    Path = CStr(Dts.Variables("User::Path").Value)

    FSO = CreateObject("Scripting.FileSystemObject")

    FSO.DeleteFile(Path & "Denials_*.txt")

    FSO = Nothing

    Dts.TaskResult = Dts.Results.Success

    End Sub

    Error:Option Strict On requires all variable declarations to have an 'As' clause. Line Text: Dim FSO

    Error : Option Strict On disallows late binding. FSO.DeleteFile(Path & "Denials_*.txt")

    Any help will be appreciated

  • FSO needs a datatype in the Dim statement ...

    The absence of evidence is not evidence of absence.
    Martin Rees

    You can lead a horse to water, but a pencil must be lead.
    Stan Laurel

  • what should be the datatype...

  • If you want to delete every text file in the folder you can:

    Insert another foreach loop container inside of the one you have now

    Set your "Enumerator" = Foreach File Enumerator

    Open the expressions editor and set the "directory" property to your variable being filled by the previous loop.

    Set the "Files" Textbox in the Enumeration Configuration to "*.txt"

    add a variable with a name like "varFileToDel"

    Set the variable mapping in the new "Foreach Loop Container" to varFileToDel

    Add a File System Task to the Foreach Loop Container

    Set Operation to Delete File

    Set "Is source path variable" = True

    Set "Source Variable" = varFileToDel

  • Actually, make sure you've got an

    Imports System.IO

    and then you should be able to

    File.Delete(filespec)

    without the need for the FSO.

    The absence of evidence is not evidence of absence.
    Martin Rees

    You can lead a horse to water, but a pencil must be lead.
    Stan Laurel

  • Thanks,I followed the steps in your reply, but I get following error

    Validation error. File System Task File System Task: Variable "VarFileDel" is used as a source or destination and is empty.

    any idea..

  • right click on the filesystems task and set the property "delay validation" = true

  • Hey Thanks for that, Now I do not see any errors, but when I execute the package the files wont get deleted.

    I left the folder field in the enumerator configuration of For Each loop container empy.

    i get following warning..

    Validation warning. Foreach Loop Container : The path is empty.

    coluld this be the reason for not deleting the files. If yes please let me know what should be there in that field.

    I put a brkpoint on for each loop container to check to see if vardelfile variable is receiving any variable, it is not receiving any value in any iteration.

    Please let me know if there anything I can do to make it work..

    Thanks....

  • Everything that I gave you is based off of your first foreach loop,

    put in a breakpoint and see if the mapped variable in the first loop is being filled

  • I left the folder field in the enumerator configuration of For Each loop container empy.

    It is okay for this to be empty, by setting the directory property with the variable from your first foreach loop container, you are filling this value.

  • yes, all the variables are being populated except vardelfile.

  • I left the folder field in the enumerator configuration of For Each loop container empy.

    It is okay for this to be empty, by setting the directory property with the variable from your first foreach loop container, you are filling this value.

  • My understanding is that your user variable "Path" is being filled with a value like: "\\serverA\folder1\folder2"

    and your user variable "Path" is the variable being used in the second foreach loop as an expression for the property "Directory".

    The second for each loop is being mapped to varFileDel and should be seeing values

    like "\\serverA\folder1\folder2\SomeFile.txt"

    This is all correct right?

  • yes thats correct

Viewing 15 posts - 1 through 15 (of 15 total)

You must be logged in to reply to this topic. Login to reply