By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,475 Members | 954 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,475 IT Pros & Developers. It's quick & easy.

Verify if Two Files are in Same Directory, If So Process, If Not Go to Next Directory

P: 2
Hello everyone. This is essentially my first time trying to make a program, of any sorts, so there is a chance I am doing things the wrong way or over complicating things. Sorry if either of those happens.

Below is a small snippet of what I am trying to resolve. There is about 2,000 directories I need to go through and find two different files, "perform_log.txt" and "ship.txt." Then, I am pulling information from each of those files and exporting it to ares of an Excel worksheet. All of that works perfectly.

The only extra thing I would like it to do, is to check if each directory has BOTH files. Every directory will always have a ship.txt, but not all will have perform_log.txt. If perform_log.txt is not there, I would like my code to skip that directory and avoid writing anything...just move on to the next set.

Short Story: I would like to find a way to say, "If there is no perform_log.txt in directory with ship.txt ignore directory go to next directory."


Expand|Select|Wrap|Line Numbers
  1. Dim Files As String() = IO.Directory.GetFiles(browsebox.Text, "perform_log.txt", IO.SearchOption.AllDirectories)    
  2.  
  3. Dim Ships As String() = IO.Directory.GetFiles(browsebox.Text, "ship.txt", IO.SearchOption.AllDirectories)
  4.  
  5.  
  6.  
  7. For Each Ship In Ships
  8.     line9 = (GetLine(Ship, 9))
  9.  
  10.     ssline9 = line9.Substring(27, 9)
  11.     XLWs.Cells(i, currentCol) = ssline9
  12.  
  13.     i += 1
  14.  
  15. Next
  16.  
  17. For Each File In Files
  18.  
  19.                 line21 = (GetLine(File, 21))
  20.                 line25 = (GetLine(File, 25))
  21.                 line26 = (GetLine(File, 26))
  22.                 line52 = (GetLine(File, 52))
  23.                 line56 = (GetLine(File, 56))
  24.                 line57 = (GetLine(File, 57))
Jun 20 '13 #1

✓ answered by IronRazer

Hi,
You can search threw all the sub folders in a given directory and check if they contain both files like this. Just set the (SearchDir) string to the directory you want to search.
Expand|Select|Wrap|Line Numbers
  1. Imports System.IO
  2.  
  3. Public Class Form1
  4.     Dim SearchDir As String = "C:\TestFolder"
  5.  
  6.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  7.         Dim di As New DirectoryInfo(SearchDir)
  8.  
  9.         'Loop threw all directories in the (SearchDir) directory and find every folder that has both files in it.
  10.         For Each dir As DirectoryInfo In di.GetDirectories("*", IO.SearchOption.AllDirectories)
  11.             Dim file1 As String = Path.Combine(dir.FullName, "ship.txt")
  12.             Dim file2 As String = Path.Combine(dir.FullName, "perform_log.txt")
  13.             If File.Exists(file1) And File.Exists(file2) Then
  14.  
  15.                 'Here you can open and read the info from (file1 and file2) that you need to get from them
  16.                 'and export it to your Excel worksheet. Make sure you close the files when done reading them.
  17.  
  18.                 ListBox1.Items.Add(dir.FullName) 'Show the directories in a listbox if you want. Delete this line if not.
  19.             End If
  20.         Next
  21.     End Sub
  22. End Class
  23.  

Share this Question
Share on Google+
3 Replies


100+
P: 759
"Every directory will always have a ship.txt, but not all will have perform_log.txt."

Based on this affirmation, line 3 in your code is not necessary.
Now, For Each File in Files (by the way, why you don't use a suggestive name like "Performs" ?) the path for the "ship.txt" file must be the directory path for Perform file & "\ship.txt"

Hope this is a help for you
Jun 21 '13 #2

IronRazer
P: 83
Hi,
You can search threw all the sub folders in a given directory and check if they contain both files like this. Just set the (SearchDir) string to the directory you want to search.
Expand|Select|Wrap|Line Numbers
  1. Imports System.IO
  2.  
  3. Public Class Form1
  4.     Dim SearchDir As String = "C:\TestFolder"
  5.  
  6.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  7.         Dim di As New DirectoryInfo(SearchDir)
  8.  
  9.         'Loop threw all directories in the (SearchDir) directory and find every folder that has both files in it.
  10.         For Each dir As DirectoryInfo In di.GetDirectories("*", IO.SearchOption.AllDirectories)
  11.             Dim file1 As String = Path.Combine(dir.FullName, "ship.txt")
  12.             Dim file2 As String = Path.Combine(dir.FullName, "perform_log.txt")
  13.             If File.Exists(file1) And File.Exists(file2) Then
  14.  
  15.                 'Here you can open and read the info from (file1 and file2) that you need to get from them
  16.                 'and export it to your Excel worksheet. Make sure you close the files when done reading them.
  17.  
  18.                 ListBox1.Items.Add(dir.FullName) 'Show the directories in a listbox if you want. Delete this line if not.
  19.             End If
  20.         Next
  21.     End Sub
  22. End Class
  23.  
Jun 22 '13 #3

P: 2
Sorry, I ended up solving this about two days ago using a simple ".count" - Found the directories, then searched each directory to verify they had the required files, then continued on. I appreciate all of the help, though. Hopefully I can clean my whole thing up and make it a bit more efficient and IronRazers suggestions of the PathCombine might be a route for me to look at.

Again, thank you both for the suggestions.

Expand|Select|Wrap|Line Numbers
  1.         Dim dirs = My.Computer.FileSystem.GetDirectories(browsebox.Text, FileIO.SearchOption.SearchAllSubDirectories) 
  2.  
  3.         For Each d In dirs
  4.             If My.Computer.FileSystem.GetFiles(d, FileIO.SearchOption.SearchTopLevelOnly, {"perform_log.txt", "ship.txt", "*.seg"}).Count = 3 Then
  5.  
  6. 'do required tests
Jun 23 '13 #4

Post your reply

Sign in to post your reply or Sign up for a free account.