I am trying to delete the oldest file in a directory. I found a way to do it using a batch script, but when I put the batch code inside system, it doesn't works!

string dir = "C:\\testfolder\\";
	string system_s = "FOR /F \"delims=!\" %%i IN ('DIR /A-D /B /O-D \""+dir+"\"') DO del \"%%i\"";
	const char *system_c = system_s.c_str();
	system(system_c);

On the other hand, if I place the code bellow inside a .bat file and execute it it works:

FOR /F "delims=!" %%i IN ('DIR /A-D /B /O-D "C:\testfolder\"') DO del %%i

Any help is welcome!

Put all that code in a *.bat file then call it from your c program, or just write the c program to do it by calling functions FindFirstFile() and FindNextFile(), then call delete() to remove the files.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.