473,473 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

read through files of a directory name passed as an argument

2 New Member
can you help me a to write a code using vs studio 8 to read a diretory name as argument and then open the directory and read each file one at a time and find the match for a string passed as an nother argument in the lines of each file. When it matches create a file and write in that output file the file name whetre the match found, the line number and line which matched.

My vis does not have dirent.h. so please use another ways to read directory call witha system acll and then then extract the fienames in a file(use some primitive way to open the the directory)
I cant do this parts
(1) read a directory
(2) create a file with only file names
(3) read file names ans open the files one at a time till the end of the file reached
May 3 '14 #1
3 1468
divideby0
131 New Member
If you're using VS, then you should be able to make use of CreateFile, FindFirstFile and FindNextFile for processing directories and files. The links have examples for doing what you're trying to do.

The only other thing I can think of is to build a command string for DIR, and pass the arugments using system. You can dump the result to a file instead of the console window, which you could then open, read, and process as needed.

Mill over that stuff and you should be able to post some code if you need additional help. Good luck
May 3 '14 #2
mmajumdar
2 New Member
Somehow I am unable to make that command string for dir command which will dump just the file names to an output file.

system("dir > outfile")It will dump everything what is found in the directory with size, dates and everything along with file name.

what code will do that or what code to extract the file name from the outfile and open it one by one to read it.

findfirst file and find nextfile need dirent.h which visual studio does not have. I have version 8 and i am in a place I am unable to install. Can you provide me an ascii text file dirent.h or probably dir.h I can include in my code to have those dir command find first and find second can work
May 3 '14 #3
divideby0
131 New Member
With VS, you should be able to include windows.h and link kernel32.lib to your application for FindFirstFile, FindNextFile, etc.

Expand|Select|Wrap|Line Numbers
  1. #include <windows.h>
  2.  
  3. #pragma comment(lib, "kernel32")
  4.  
  5. // program code
  6.  
For the other, I cannot recall if you mentioned whether or not you were using c or c++, but you can build the dir command using sprintf or sprintf_s

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7.    char cmd[200] = { 0 };
  8.  
  9.    // make sure to check argc for
  10.    // expected number of cmd line args
  11.  
  12.    sprintf(cmd, "dir %s > outfile", argv[1]);
  13.    // if arg 1 was the full path
  14.  
  15.    system(cmd);
  16.    // other bits of code
  17. }
To process the outfile using default dir output, you'll need to use the spaces as a deliminator. strtok will be useful

Expand|Select|Wrap|Line Numbers
  1. char buffer[200];
  2. while(fgets(buffer, sizeof buffer, file_handle) != NULL)
  3. {
  4.    char *p = strtok(buffer, " ");
  5.  
  6.    while(p)
  7.    { 
  8.       // p has each word of current buffer
  9.       // test and process p
  10.  
  11.       p = strtok(NULL, " ");
  12.    }
  13.  
  14.    // other code
  15. }
Hope that helps.
May 3 '14 #4

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

Similar topics

0
by: Ronen | last post by:
My web application name is xyz--> so I have a virtual directory name xyz. The problem: I need each time to install it under different name! What would be the best way to do it and avoid all this...
1
by: Galbu | last post by:
Hi all. I know there's a thread 'bout windows programming, but I can't find it anymore. However.. Here's my problem: I need to copy some files in the program files directory in C:\ The problem is...
0
by: Ronen | last post by:
My web application name is xyz--> so I have a virtual directory name xyz. The problem: I need each time to install it under different name! What would be the best way to do it and avoid all this...
1
by: Vikram | last post by:
I have suddenly started getting this errorin all my ASP.NET applications when I try to open any aspx page. Compiler Error Message: CS0016: Could not write to output file...
0
by: Don | last post by:
I intermittently get a runtime Compilation Error that says 'The compiler failed with error code 2000'. It appears that a DLL cannot be found in the 'temporary asp.net files' directory. The...
9
by: sdb1031 | last post by:
I am trying to run an exe within a python script, but I'm having trouble with spaces in the directory name. The following example will display the usage statement of the program, so I know that...
10
by: tshad | last post by:
I want to access multiple arguments based on name passed. For example I have the following asp:textboxes: BillingAddress1 BillingAddress2 BillingCity ShippingAddress1 ShippingAddress2...
7
by: nicknack | last post by:
Hello. I would like to give my user an option to find a directory and choose it. Its just like the FileUpload control but the fileupload control choose only files (if you choose a directory and...
13
by: ogo796 | last post by:
hi everyone i upload file each an every day so i want to keep track of how many files i uploade a day by creating new directory everytime when i uploade base on the system date. example the new...
12
by: Hongyu | last post by:
Dear all: I am trying to write to a file with full directory name and file name specified (./outdir/mytestout.txt where . is the current directory) in C programming language and under Unix, but...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.