little app help

Started by
9 comments, last by unfinished 19 years, 1 month ago
Hey, Ok this is NOT a game related question.. but i know there are a lot of clever people in here that can help me out Im a noob when it comes to programming. I know the basics, loops etc but when it comes to larger 100+ lines i start getting over my head. so heres the score, i made a batch file that exports all the files in a directory into a txt file so i can compare them with a list in a database. The problem is that the dir list in the txt file has date, time and size before the file name which looks clutted and hard to read when it's copied into a spreadsheet and i dont wanna delete the front of 3000+ lines. What i need is a small app that will delete the first 37 or so characters in each line of the text file so it just leaves me with the file name. Heres a line from the txt file: 29/07/1997 11:12 AM 26,038 1001c0003rb.tif This is what i need: 1001c0003rb.tif If anyone can just chuck functions and code at me and maybe estimate how big the app will have to be, im sure i can piece something together. Thanks
Advertisement
Assuming the bit at the front that needs to be removed is always the same amount of characters, you could write a suitable app in VB in 10 mins (assuming you code slowly), using a for loop and mid$.

Mid$ allows you to take only part of a string, and put it in a new string. It takes the following parameters:
output_string = Mid$(input_string, first_char_as_an_int, number_of_chars_wanted_as_an_int)


As another option, could you possibly just generate the file without that information in it???

- Jason Astle-Adams

dir /B
?
What you need then is simple parsing function. Here's a quick example that should do the job:
#include <vector>#include <string>#include <iostream>std::vector<std::string> tokenize(const std::string & str, const std::string & delim){  using namespace std;  vector<string> tokens;  size_t p0 = 0, p1 = string::npos;  while(p0 != string::npos)  {    p1 = str.find_first_of(delim, p0);    if(p1 != p0)    {      string token = str.substr(p0, p1 - p0);      tokens.push_back(token);    }    p0 = str.find_first_not_of(delim, p1);  }  return tokens;}int main( int argc, char* argv[] ){	// Holder for tokens of each string	std::vector< std::string > tokens;	// Vector that has all the file lists in it	std::vector< std::string > str;	// Just for an example, 25 files with same info :)	str.resize( 25, "29/07/1997 11:12 AM 26,038 1001c0003rb.tif" );	// Loop through	for( int x = 0;x < 25;x++ )	{		// Tokenize		tokens = tokenize( str[x], " " );		// We know the format, so the filename is the 4th index of the array		std::cout << tokens[4] << std::endl;	}	return 0;}


Output:
1001c0003rb.tif1001c0003rb.tif1001c0003rb.tif1001c0003rb.tif1001c0003rb.tif1001c0003rb.tif1001c0003rb.tif1001c0003rb.tif1001c0003rb.tif1001c0003rb.tif1001c0003rb.tif1001c0003rb.tif1001c0003rb.tif1001c0003rb.tif1001c0003rb.tif1001c0003rb.tif1001c0003rb.tif1001c0003rb.tif1001c0003rb.tif1001c0003rb.tif1001c0003rb.tif1001c0003rb.tif1001c0003rb.tif1001c0003rb.tif1001c0003rb.tifPress any key to continue


Cheers to Oluseyi, for he is the one that made that code segment a few weeks back here. I keep it bookmarked cuz it's just unbeatable [smile].

- Drew
Ah yes, dir /b should do this, or eh... ls|awk '{print $5}' on most any unix machine.
This should get it done. This program assumes myFile.txt is name of your input file. Create output in out.txt

#include <iostream>#include <fstream>using namespace std;ifstream	inFile("myFile.txt");ofstream	outFile("out.txt",ios::out);int main(){	char buffer[30];	while(inFile)	{		// first 4 things you dont need		inFile >> buffer;		inFile >> buffer;		inFile >> buffer;		inFile >> buffer;		// getting file name		inFile >> buffer;		outFile << buffer << endl; // write that file name to new file	}	return 0;}
Go on an Intense Rampage
wow!

Thanks guys that was all a huge help

and so quickly too!
Ok so ive got my list and its all very cool.

except for one thing ;s

in the directories there are files that i dont want in my list.
thankfully they have very different names to the files i do want.

how would i get rid of the lines that i dont want..
all the file names i DO want start with a 1 or other number
so i need something that tells lines that start with a char to please leave without a fuss.

eg

095e5189r2.tif
1000e0097r3.tif
1000e0190r2.tif
1000e0210r4.tif
1000e1000r0.tif
pspbrwse.jbf <-----dont want that
1000m0060r3.tif
1000s0020r3.tif
1001000001ra.tif
pspbrwse.jbf <-----or that!
1001c0001rb.tif
1001c0002rb.tif
1001c0003rb.tif
1001c0004r0.tif
1001c0005r1.tif
1001c0007r0.tif

Thanks team.
If you were using stl vectors:
#include <vector>#include <string>#include <iostream>int main( int argc, char* argv[] ){	std::vector < std::string > filelist;	filelist.push_back( "095e5189r2.tif" );	filelist.push_back( "1000e0097r3.tif" );	filelist.push_back( "1000e0190r2.tif" );	filelist.push_back( "1000e0210r4.tif" );	filelist.push_back( "1000e1000r0.tif" );	filelist.push_back( "pspbrwse.jbf" );        // Output list fist	for( std::vector < std::string >::iterator itr = filelist.begin(); itr != filelist.end(); itr++)	{		std::cout << itr->c_str() << std::endl;	}	for( itr = filelist.begin(); itr != filelist.end(); )	{                // If the first element is not a number		if( !isdigit( itr->at(0) ) )		{                        // Erase that sucker! Note that itr is advanced, so we do not need to ++ it.			filelist.erase( itr );		}		else                        // We want to keep it, so advance the pointer			itr++;	}	std::cout << std::endl << std::endl;        // Output new list now	        for( itr = filelist.begin(); itr != filelist.end(); itr++)	{		std::cout << itr->c_str() << std::endl;	}	return 0;}


Which gives you an output of:

095e5189r2.tif1000e0097r3.tif1000e0190r2.tif1000e0210r4.tif1000e1000r0.tifpspbrwse.jbf095e5189r2.tif1000e0097r3.tif1000e0190r2.tif1000e0210r4.tif1000e1000r0.tifPress any key to continue


- Drew
Thanks drew

This topic is closed to new replies.

Advertisement