|
-
Nov 28th, 2011, 02:56 AM
#1
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,...
-
Nov 28th, 2011, 03:22 AM
#2
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.
-
Nov 28th, 2011, 03:34 AM
#3
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,...
-
Nov 28th, 2011, 03:46 AM
#4
Re: Remove temp document
worked for me with some modifications:
vb Code:
Dim temp() As String = IO.Directory.GetFiles("X:\Documents and Settings\XXX\Desktop\vvvvv", "*.doc")
Dim allFiles() As String = (From str In temp
Where Not str.ToString.StartsWith("~$")
Select str).ToArray
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 28th, 2011, 03:52 AM
#5
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:
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.
-
Nov 28th, 2011, 03:53 AM
#6
Re: Remove temp document
 Originally Posted by akhileshbc
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:
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim temp As New List(Of String)(IO.Directory.GetFiles("X:\Documents and Settings\XXX\Desktop\vvvvv", "*.doc"))
temp.RemoveAll(AddressOf isTemp)
End Sub
Private Function isTemp(s As String) as Boolean
Return IO.Path.GetFileName(s).StartsWith("~$")
End Function
End Class
edited
Last edited by .paul.; Nov 28th, 2011 at 04:00 AM.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 28th, 2011, 04:00 AM
#7
Re: Remove temp document
 Originally Posted by .paul.
yes you're correct. it would've helped to know you're using .net 2.0
vb Code:
Public Class Form1 Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load Dim temp As New List(Of String)(IO.Directory.GetFiles("X:\Documents and Settings\XXX\Desktop\vvvvv", "*.doc")) temp.RemoveAll(AddressOf isTemp) End Sub Private Function isTemp(s As String) Return IO.Path.GetFileName(s).StartsWith("~$") End Function 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:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim temp As New List(Of String)(IO.Directory.GetFiles("X:\Documents and Settings\XXX\Desktop\vvvvv", "*.doc")) temp.RemoveAll(AddressOf isTemp) For i As Integer = 0 To temp.Count - 1 ListBox1.Items.Add(temp.Item(i)) Next End Sub Private Function isTemp(ByVal s As String) Return IO.Path.GetFileName(s).StartsWith("~$") 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,...
-
Nov 28th, 2011, 04:01 AM
#8
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.
-
Nov 28th, 2011, 04:02 AM
#9
Re: Remove temp document
i edited too (twice ). i didn't test it
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 28th, 2011, 04:05 AM
#10
Re: Remove temp document
 Originally Posted by jmcilhinney
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:
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,...
-
Nov 28th, 2011, 04:10 AM
#11
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,...
-
Nov 28th, 2011, 04:13 AM
#12
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 28th, 2011, 04:22 AM
#13
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|