Reading files from USB drive

I’d like to be able to input some video files to an oF project by swapping out different USB flash drives. The drives could be preset to have only the correct files, so really all I need is a way for oF to see the drive and download it to the data folder. Has anyone done this before?

I have and it depends on the system being used. For windows there’s a system function you can use called GetLogicalDriveStrings() which I can put some code together for if you need it. On OSX you can just call system(“ls /Volumes”); and that will return the string. Actually, it’s probably better to use popen, like this:

  
	FILE *fp = popen("ls /Volumes/", "r+" );  
	char buff[50];  
	  
	if (fp != NULL)  
	{  
		while ( fgets( buff, sizeof buff, fp ) != NULL )   
		{  
			cout << buff;  
		}  
	}  
	pclose( fp );  

Excuse the old school C-ness of that code. You can just poll intermittently to see if you have any new drives added and then go read what you need from them.

if you know the exact path to the USB, a more of’ish way : ) you could do:

  
  
ofDirectory dir("/Volumes/path");  
if(dir.exists()){  
    ofFile src("path_to_src");  
    ofFile dst("path_to_dst",ofFile::WriteOnly);  
    dst << src.getFileBuffer();  
}  
  
  

Yeah, sorry, I forgot to mention that my code is really only helpful when you don’t know what the device will be called, i.e. if you have a publicly accesible computer. if you know what it’s called, i.e, you’re only looking for a drive called “usbDrive” or something, then Arturos solution is way better. I overcomplicate things sometimes :slight_smile:

no, but i didn’t knew about popen it can be super useful for some things, i’ve always used plain system() but being able to access the output is really great, we should encapsulate that in some of class

Something like:

  
vector<string> ofUtils::ofSysCommand(string &command);  

could be helpful, though I have no idea how that would work on the different Windows variants.

it could be something like:

  
ofBuffer ofSysCommand(string &command);   

since ofBuffer already takes into account the different breakline types in windows/unix

That’s perfect, thanks guys!

In this particular case I have control over what’s on the drives, so I can make it simpler on the code side. But it’s great for a reference if I wanted to broaden the scope of it later.

I think it have it seeing the USB drive, but it’s not grabbing my file for some reason. I included the ofSetColor just to make sure that if statement was valid, but when I check the data folder nothing is there…

  
  
void testApp::setup(){  
    ofDirectory dir("/Volumes/Reel");  
    if(dir.exists()){  
        ofSetColor(45,20,255);  
        ofFile src("film.mov");  
        ofFile dst("/Developer/Library/Frameworks/of_preRelease_v007_osx/apps/examples/Videola/bin/data",ofFile::WriteOnly);  
        dst << src.getFileBuffer();   
    }  
}  
  

  
ofBuffer ofSysCommand(string &command);    

That’s awesome. I’ll add that in this weekend, if you don’t get to it first :slight_smile:

it should be something like:

  
  
void testApp::setup(){  
    ofDirectory dir("/Volumes/Reel");  
    if(dir.exists()){  
        ofSetColor(45,20,255);  
        ofFile src("/Volumes/Reel/film.mov");  
        ofFile dst("film.mov",ofFile::WriteOnly);  
        dst << src.getFileBuffer();   
    }  
}  
  

where src is the path to the file in the usb and dst is the path to the file in data, since it’s in data you don’t need the absolute path just the name of the file or the relative path from data

@joshua, no, go for it : )

That makes more sense… Works great now, thanks!