Jump to content
  • Advertisement
Sign in to follow this  
limey

little app help

This topic is 4982 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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???

Share this post


Link to post
Share on other sites
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.tif
1001c0003rb.tif
1001c0003rb.tif
1001c0003rb.tif
1001c0003rb.tif
1001c0003rb.tif
1001c0003rb.tif
1001c0003rb.tif
1001c0003rb.tif
1001c0003rb.tif
1001c0003rb.tif
1001c0003rb.tif
1001c0003rb.tif
1001c0003rb.tif
1001c0003rb.tif
1001c0003rb.tif
1001c0003rb.tif
1001c0003rb.tif
1001c0003rb.tif
1001c0003rb.tif
1001c0003rb.tif
1001c0003rb.tif
1001c0003rb.tif
1001c0003rb.tif
1001c0003rb.tif
Press 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

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.tif
1000e0097r3.tif
1000e0190r2.tif
1000e0210r4.tif
1000e1000r0.tif
pspbrwse.jbf


095e5189r2.tif
1000e0097r3.tif
1000e0190r2.tif
1000e0210r4.tif
1000e1000r0.tif
Press any key to continue





- Drew

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!