Results 1 to 13 of 13

Thread: [RESOLVED] Remove temp document

  1. #1

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Resolved [RESOLVED] Remove temp document

    Hi guys

    In my desktop, I have folder containing some word document files. I want the filenames of the documents in that folder. So, I used:
    Code:
     Dim strFiles() As String = IO.Directory.GetFiles("X:\Documents and Settings\XXX\Desktop\vvvvv", "*.doc")
    All filenames are stored in that array.

    But it would also include the temp document that Word generates. I am somewhat confused on how to remove this from array. I only need full filenames of the actual documents, which excludes the temp document.

    Any ideas ?

    Thanks in advance

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Remove temp document

    Don't the temp docs all start with "~$"? A simple LINQ query will give you the items from the list where the file doesn't start with that. You'll obviously have to do that after getting the full list. There's no way to avoid getting the full list in the first place.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Remove temp document

    Thanks

    I have tried doing this query:
    Code:
            Dim temp() As String = IO.Directory.GetFiles("X:\Documents and Settings\XXX\Desktop\vvvvv", "*.doc")
    
            Dim allFiles = From str In temp
                            Where Not str.ToString.StartsWith("~$")
                            Select str
    But it gives an error:
    Code:
    Error	1	Expression of type '1-dimensional array of String' is not queryable. Make sure you are not missing an assembly reference and/or namespace import for the LINQ provider.
    This is the first time I am playing with LINQ.

    Edit:
    Is LINQ available only in FW3.5+ ?

    I am targeting at FW2.0
    Last edited by akhileshbc; Nov 28th, 2011 at 03:44 AM. Reason: added more info that I found on searching

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Remove temp document

    worked for me with some modifications:

    vb Code:
    1. Dim temp() As String = IO.Directory.GetFiles("X:\Documents and Settings\XXX\Desktop\vvvvv", "*.doc")
    2.  
    3. Dim allFiles() As String = (From str In temp
    4.                 Where Not str.ToString.StartsWith("~$")
    5.                 Select str).ToArray

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Remove temp document

    Yes, LINQ is .NET 3.5 and later. Unless you have a requirement for .NET 2.0, I'd suggest upgrading.

    If you do then your query will be a little different to that. First, there's not much point calling ToString on an element from a String array. Chances are that it is already a String. Also, the array contains full file paths, while it's the file name that starts with that substring. You can use IO.Path.GetFileName to get just the file name.
    vb.net Code:
    1. Dim wordDocs = IO.Directory.GetFiles("folder path here", "*.doc").Where(Function(s) Not IO.Path.GetFileName(s).StartsWith("~$")).ToArray()
    You can drop the ToArray if you only intend to loop through the list. Also, that uses function syntax but query syntax would obviously be just as valid. It just doesn't feel as natural in this particular scenario to me.

    If you want to stick with .NET 2.0 then you'll use a loop. Create a new List from the array and then use a For loop to go backwards through the file paths. When you find a temp file path you remove it from the List. At the end, you can convert to an array again if desired.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Remove temp document

    Quote Originally Posted by akhileshbc View Post
    Edit:
    Is LINQ available only in FW3.5+ ?

    I am targeting at FW2.0
    yes you're correct. it would've helped to know you're using .net 2.0

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    4.         Dim temp As New List(Of String)(IO.Directory.GetFiles("X:\Documents and Settings\XXX\Desktop\vvvvv", "*.doc"))
    5.         temp.RemoveAll(AddressOf isTemp)
    6.     End Sub
    7.  
    8.     Private Function isTemp(s As String) as Boolean
    9.        Return IO.Path.GetFileName(s).StartsWith("~$")
    10.     End Function
    11.  
    12. End Class

    edited
    Last edited by .paul.; Nov 28th, 2011 at 04:00 AM.

  7. #7

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Remove temp document

    Quote Originally Posted by .paul. View Post
    yes you're correct. it would've helped to know you're using .net 2.0

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    4.         Dim temp As New List(Of String)(IO.Directory.GetFiles("X:\Documents and Settings\XXX\Desktop\vvvvv", "*.doc"))
    5.         temp.RemoveAll(AddressOf isTemp)
    6.     End Sub
    7.  
    8.     Private Function isTemp(s As String)
    9.        Return IO.Path.GetFileName(s).StartsWith("~$")
    10.     End Function
    11.  
    12. End Class
    Thanks Paul

    That worked. I have added to get the FileName as I was trying check whether the FullPath is starting with "~$".

    vb.net Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim temp As New List(Of String)(IO.Directory.GetFiles("X:\Documents and Settings\XXX\Desktop\vvvvv", "*.doc"))
    3.         temp.RemoveAll(AddressOf isTemp)
    4.  
    5.         For i As Integer = 0 To temp.Count - 1
    6.             ListBox1.Items.Add(temp.Item(i))
    7.  
    8.         Next
    9.     End Sub
    10.  
    11.     Private Function isTemp(ByVal s As String)
    12.         Return IO.Path.GetFileName(s).StartsWith("~$")
    13.     End Function


    Edit:
    I didn't saw that you have edited the code.
    Last edited by akhileshbc; Nov 28th, 2011 at 04:02 AM. Reason: didn't saw the "GetFileName" edit

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Remove temp document

    You should be either binding that List to the ListBox or else calling AddRange. Either way, not adding items one by one.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Remove temp document

    i edited too (twice ). i didn't test it

  10. #10

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Remove temp document

    Quote Originally Posted by jmcilhinney View Post
    Yes, LINQ is .NET 3.5 and later. Unless you have a requirement for .NET 2.0, I'd suggest upgrading.

    If you do then your query will be a little different to that. First, there's not much point calling ToString on an element from a String array. Chances are that it is already a String. Also, the array contains full file paths, while it's the file name that starts with that substring. You can use IO.Path.GetFileName to get just the file name.
    vb.net Code:
    1. Dim wordDocs = IO.Directory.GetFiles("folder path here", "*.doc").Where(Function(s) Not IO.Path.GetFileName(s).StartsWith("~$")).ToArray()
    You can drop the ToArray if you only intend to loop through the list. Also, that uses function syntax but query syntax would obviously be just as valid. It just doesn't feel as natural in this particular scenario to me.

    If you want to stick with .NET 2.0 then you'll use a loop. Create a new List from the array and then use a For loop to go backwards through the file paths. When you find a temp file path you remove it from the List. At the end, you can convert to an array again if desired.
    Thanks

    Yeah I missed the GetFileName() in my previous code. The reason for using toString() was that it didn't had the StartsWith() method. So, I thought it has to be casted to string.

    My target machines contains XP SP2 users. So, I think I can't use FW3.5 as it requires SP3 or high.

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  11. #11

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Remove temp document

    Quote Originally Posted by jmcilhinney View Post
    You should be either binding that List to the ListBox or else calling AddRange. Either way, not adding items one by one.
    Thanks

    I was just testing whether it worked or not. This Items will be used one by one for another purpose. So, FOR Loop will be the one that I am going to use for the next operation, that's why I included in displaying also.
    I will use the method that you mentioned, in the next time when I need to output to listbox.

    Can you guys tell me how the RemoveAll() works with the isTemp() ? A basic idea of how it functions, so that I could understand.

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Remove temp document

    it's basically a for each loop (behind the scenes).
    each element in the list is passed to the isTemp function. if it returns true it removes the element + doesn't if it return false

  13. #13

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Remove temp document

    ok thanks

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width