473,383 Members | 1,885 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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

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.  

3 1408
Mihail
759 512MB
"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
83 64KB
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
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

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

Similar topics

8
by: McGrull | last post by:
Sorry for my question, but I'm really a newbie on ASP. I need to make a simple page (for IIS 5.0 and up, but even for 2000 Professional IIS) that read and display the files contained in a...
2
by: John E. | last post by:
Some of our developers are using .NET 2005 already and other are using 2003. Since they are not backwards compatible, we were trying to devise a system where we have 2 different project files that...
3
by: lasmit | last post by:
I am having some troubles with a project I am working on not building correctly. I converted an old ASP project to .Net and then have been adding pages to the project in C#. This all works ok,...
9
by: TPS | last post by:
I have a virtual directory where all posted files are stored. The ASP app does not have rights to the share on the other server where the vir dir resides. What is the best way to give the asp...
5
by: Verane | last post by:
Hi, I have read the thread named "Could not copy temporary files to the output directory" on this newsgroup. And I have the same symptoms on my machine. But I didn't find any solution suitable for...
7
by: MB | last post by:
I keep getting this error now: Could not copy temporary files to the output directory. Now i searched the web and found the same two solutions: 1. Set copy local to true 2. Check for extra...
5
by: Frank | last post by:
I'm using VB .NET and I'm finding that CopyTo will not help me copy all the files in one directory to antoher directory at one time. Which command (if that's the word) do I use to copy ALL the...
23
by: **Developer** | last post by:
Is there an easy way to copies all files in a directory into another directory? What about coping subdirectories too? Thanks in advance for any info
3
by: tony | last post by:
Hello! When I build an exe file that use 6 class library dll I get this error. Could not copy temporary files to the output directory. The file 'MeltPracStorage.dll' cannot be copied to the run...
12
by: Antonio Maschio | last post by:
Hi, I need help. I want to read text files contained into a directory, but my program is unaware of how many files are contained into. In bash there's something similar to (figure out) for i...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.