Simple C Program - Copying Files

Soldato
Joined
22 Oct 2005
Posts
2,801
Location
Moving...
Hi all.

My problem is that I want a way to take a playlist file(.m3u), and copy all the songs in this playlist to my desktop.

I've managed to open the file, strip out the rubbish, and be left with a list of file names e.g. "F:/Music/ABC.mp3".

All I need to do then is copy this file into a folder on the desktop, but I can't work out how to do it.

If it was a .txt file I'd just open it up and copy it line by line to the new location, but I don't think I can do it this way here. Therefore I was looking for a function that will allow me to copy the file as a whole. I found CopyFile(), but I can't get it to work, even when I hard code the names in like below nothing is copied.

Code:
CopyFile("C:/Documents and Settings/M/Desktop/test.mp3", 
	"C:/Documents and Settings/M/Desktop/tempMusic", 
	0);


It doesn't even work with a .txt file so I don't think it's the file extension causing issues.

Any idea what I'm doing wrong?

I'm open to using any other methods btw, don't have to use this function, I'm happy to use anything quick and dirty if needs be!

Thanks for any help.
 
Soldato
OP
Joined
22 Oct 2005
Posts
2,801
Location
Moving...
Yea just straight C. Just because it's the language I know best (obviously still a noob though!). Could try use something else though if need be, I don't really know any other languages though.

Just tried it with filenames without spaces and still no success.
 
Soldato
Joined
7 Apr 2004
Posts
4,212
Try:

Code:
CopyFile("C:\\Documents and Settings\\M\\Desktop\\test.mp3", 
	"C:\\Documents and Settings\\M\\Desktop\\tempMusic", 
	0);

If that doesn't work, call GetLastError() and check the result to see why its failing.
 
Soldato
OP
Joined
22 Oct 2005
Posts
2,801
Location
Moving...
Try:

.....

If that doesn't work, call GetLastError() and check the result to see why its failing.

Still no luck. I'm not sure exactly how to use it but I tried calling the the following directly after the CopyFile() call:

Code:
DWORD dw = GetLastError();

That returns a value of 5.....sorry I have no idea what that means:confused:.
I saw some sample error codes but they were all minus numbers???




**edit** Ahh, does 5 have something to do with access rights?
Just seen a couple of examples that seem to indicate this.

How would I go about getting round this? I'm on winXP, I don't have any restrictions setup that I know of....
 
Last edited:
Soldato
Joined
28 Aug 2006
Posts
3,003
Still no luck. I'm not sure exactly how to use it but I tried calling the the following directly after the CopyFile() call:

Code:
DWORD dw = GetLastError();

That returns a value of 5.....sorry I have no idea what that means:confused:.
I saw some sample error codes but they were all minus numbers???




**edit** Ahh, does 5 have something to do with access rights?
Just seen a couple of examples that seem to indicate this.

How would I go about getting round this? I'm on winXP, I don't have any restrictions setup that I know of....

That maybe to do with NTFS permissions on the folder. Make sure the user account that executes the C code has Modify and write permissions to where your copying. If its the desktop then that will be in your user settings folder.
 
Soldato
OP
Joined
22 Oct 2005
Posts
2,801
Location
Moving...
That maybe to do with NTFS permissions on the folder. Make sure the user account that executes the C code has Modify and write permissions to where your copying. If its the desktop then that will be in your user settings folder.

Sorry for being a simpleton, but how would I do that?

I've even tried copying something from within the program directory so surely it must have permissions in there?

And if it makes a difference, I will actually be copying from another NTFS hard drive onto the desktop, but like I say, I don't think I have any restrictions in place for anything.
 
Associate
Joined
14 Nov 2007
Posts
85
Error code 5 is Access Denied, easiest way to find out is to have a function to format an error code & display in english what the problem is a little more.

Code:
    DWORD MsgID = GetLastError();
    char *TextSize;
    std::cerr << "Error has occured: ";
    
    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, MsgID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR) &TextSize, 0, 0);

    std::cout << TextSize << "\n";

    LocalFree(TextSize);
Obviously the cerr & cout are C++ just change them to printf or whatever form of output you like. When copying files specify the file name for each file you copy. The above lines of code I had set as a seperate function, whenever an error occured I'd call that function to display it.
 
Soldato
Joined
25 Mar 2004
Posts
15,742
Location
Fareham
I would probably use VBScript for this

Open the .m3u file as a text file (yeah it's not a text file but if you can read it using notepad OK i'm pretty sure that it will open without issue), read the content line by line, then you can simply compile and run a batch file or do whatever you need.

Code:
Const ForReading = 1, ForWriting = 2, ForAppending = 8

Set objFSO = createobject("Scripting.FileSystemObject")

M3UPath = "D:\Music\Pendulum\Pendulum-Hold_Your_Colour\00-pendulum_-_hold_your_colour.m3u"

Set M3UContent = ObjFSO.OpenTextFile(M3UPath, ForReading)

Do until M3UContent.AtEndOfStream

	M3ULine = Trim(M3UContent.ReadLine)

	Wscript.echo M3ULine

Loop
 
Associate
Joined
9 Jun 2004
Posts
423
It's probably what tntcoder suggested above:
The documentation on CopyFile (the MSDN page is the first result on Google)
says that the second parameter should be the destination/new file name. You're providing a directory, so it fails.
 
Soldato
OP
Joined
22 Oct 2005
Posts
2,801
Location
Moving...
The documentation on CopyFile (the MSDN page is the first result on Google) says that the second parameter should be the destination/new file name. You're providing a directory, so it fails.

Ah, that's it. If I specify the filename it works fine.

I'll just have to chop up the filename so I can specify an exact name for the new .mp3 file, rather than just specificy the destination folder.

Haven't done it yet but that should be fine!

Thanks for all your help.
 
Soldato
OP
Joined
22 Oct 2005
Posts
2,801
Location
Moving...
Arrggghh I suck at programming!!

I'm so close. I've got the following code: (Yes I know it's horrible, I told you I'm a noob!)

Code:
#include <stdio.h> 
#include <string.h>
#include <windows.h>

char* c;
char str[250];
char destStr[250];  
int strLength;
bool endOfFile = false;
bool foundSlash = false;
bool foundLF = false;
int i, j, x;



int main () {

	FILE *inFile; 
	inFile = fopen("F:/My Music/Playlist.m3u","r"); 

	while (endOfFile == false){

		c = fgets(str, 250, inFile);

		if (c == NULL){ //Found end of file so break 
			break;
		}

		else {
			if ( str[0] == '#'){  
				//Do Nothing becasue it's rubbish
			}
			else{

				//Remove line feed from end of string
				foundLF = false;
				x = 0;
				while (!foundLF){
					if (str[x] == 10) {
						str[x] = NULL;
						foundLF = true;
					}
					else
						x++;

				}

				strLength = strlen(str);
				strLength = strLength ;	//  Minus 1 to go to array position [0], 
											

				foundSlash = false;
				i = strLength;
				j = 0;

				while (!foundSlash){ //Look for first backslash from the right, stuff after this slash is the filename

					if (str[i] == 92){ //92 = ascii value of backslash
						//Create destination filename

						while (i<=strLength){
							destStr[j] = str[i+1];  //Plus 1 as we don't want to include the backslash in the filename
							i++;
							j++;
						}

						//Copy the file
						destStr = strcat("C:/Documents and Settings/M/Desktop/tempMusic/", destStr); 
						CopyFile(str, destStr, 0);

						foundSlash = true;
					}

					else {
						//Keep searching
						i--;
					}
				}
			}
		}
	}

	fclose(inFile);

	return 0;
}


It's the line:

Code:
destStr = strcat("C:/Documents and Settings/M/Desktop/tempMusic/", destStr);

That's causing me the problem. What I'm trying to do is simply put in a location for it to put the mp3's in, so it's an absolute path. The program will run without this line, but it just saves the mp3's it in the program directory.

The line is telling me I "cannot convert from 'const char [52]' to 'char []'". I kind of understand why, I just don't know how to get around it! Any tips?

Thanks.
 
Associate
Joined
14 Nov 2007
Posts
85
Use a pointer! :)

char *ptr = destStr;
ptr = strcat //so on then use ptr in Copyfile instead. Since you're working with directories & windows API then perhaps use MAX_PATH as the size of the arrays, destStr[MAX_PATH] as an example. (doesn't save from those out of bounds problems that can crop up but atleast the array will be the max directory size for windows on your implementation)

Admittedly I don't know how it'll work out, I didn't actually test it myself although I did compile it (albeit in a C++ compiler) without receiving errors, so perhaps it'll work in the right direction for you.
 
Back
Top Bottom