I have a big collection of MP4 files and an unstable internet connection so I use this batch file to check for the files integrity.
Having posted this primitive code and received valuable reviews, I decided to take into consideration as much as I can, especially not mixing C code unless when using Windows API and implement the said batch file using that code as the basis.
This program works in CMD it takes a path to MP4 location as argument.
#include <iostream>
#include <string>
#include <windows.h>
#ifndef INVALID_FILE_ATTRIBUTES
#define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
#endif
bool DirectoryExists(const std::string& path)
{
DWORD present = GetFileAttributesA(path.c_str());
if (present == INVALID_FILE_ATTRIBUTES)
return false;
if (present & FILE_ATTRIBUTE_DIRECTORY)
return true;
return false;
}
std::string replace (const std::string& path)
{
std::string in = path;
std::replace(in.begin(), in.end(), '\\', '/');
return in;
}
void findVideos (std::string& fspath)
{
while(*(fspath.rbegin()) =='/') //to get rid of trailing '/' like a user inputs path/to/file//////\\\
fspath.pop_back();
size_t i=1;
WIN32_FIND_DATA FindFileData;
std::string destpath = fspath + std::string("/")+ std::string("*.mp4");
std::string ffmpegPath = "ffmpeg.exe -v error -f null - -i ";
HANDLE hFind = FindFirstFile(destpath.c_str(), &FindFileData);
if (hFind != INVALID_HANDLE_VALUE)
{
do
{
std::string fullpath = std::string(fspath)+std::string("/")+std::string(FindFileData.cFileName);
std::cout<<i<<"-"<<"Checking "<<fullpath<<" for errors"<<std::endl;
std::string command = ffmpegPath +std::string("\"")+fullpath+std::string("\"");
system(command.c_str());
i++;
}
while(FindNextFile(hFind, &FindFileData));
}
FindClose(hFind);
}
int main(int argc, char**argv)
{
const char* path = argv[1];
if (path == nullptr)
{
std::cout<<"No path provided"<<std::endl;
return 0;
}
else
if ( DirectoryExists(path) )
std::cout<<"Provided path is: "<<path<<std::endl;
else
{
std::cout<<"Path doesn't exist"<<std::endl;
return 0;
}
std::string fspath;
fspath = replace(path);
findVideos (fspath);
return 0;
}
The code is working, I am open to criticism and any way to improve it especially if I work in a professional company and asked to implement such program.
EDIT: Please forgive my ignorance about indentation and other programming paradigms, I graduated a year ago and failed to get an internship or a job in the domain. I tried to go with the comments as much as I can after reading some articles about indentations.