Today I was editing videos for my future uploads. I spent half of my day editing in CapCut. Sadly I made wrong name for the output file so the videos where separated inside subfolder. So what I got is 24 videos with same name inside different folders. Then I thought I could try to make a code to move all files from subfolders to the right folder..
So I came up with this code and it made all files disappear. Mosty trying to consult with artificial inteligence. Following thing is very bad and I hope we can somehow patiently get to the right output. So I thought this code will just move files. Unfortunately all files were formatted into all sorts of files. .ini, .crc, .meta, .string, .model, .jpg, .ini, .extra, .png, .html, .js, .css, .meta, .lua.
#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <sys/types.h>
#include <pwd.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
void moveFiles(const char *sourceDir, const char *destDir) {
DIR *dir = opendir(sourceDir);
if (dir == NULL) {
perror("Error opening source directory");
return;
}
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
char srcPath[256];
snprintf(srcPath, sizeof(srcPath), "%s/%s", sourceDir, entry->d_name);
struct stat fileStat;
if (stat(srcPath, &fileStat) == 0) {
if (S_ISREG(fileStat.st_mode)) {
char destPath[256];
snprintf(destPath, sizeof(destPath), "%s/%s", destDir, entry->d_name);
if (rename(srcPath, destPath) != 0) {
perror("Error moving file");
}
} else if (S_ISDIR(fileStat.st_mode)) {
char subdir[256];
snprintf(subdir, sizeof(subdir), "%s/%s", sourceDir, entry->d_name);
moveFiles(subdir, destDir);
}
}
}
}
closedir(dir);
}
int main() {
// Get the user's home directory
const char *homedir;
struct passwd *pw = getpwuid(geteuid());
if (pw != NULL) {
homedir = pw->pw_dir;
} else {
perror("Error getting home directory");
return 1;
}
// Append "Movies" directory to the home directory path
char moviesDir[256]; // Assuming the directory path won't exceed 256 characters
snprintf(moviesDir, sizeof(moviesDir), "%s/Movies", homedir);
// Move files from subdirectories to the "Movies" directory
moveFiles(moviesDir, moviesDir);
printf("Files from subdirectories moved to the 'Movies' directory.\n");
return 0;
}
Are my videos gone or is there way to get them back please ?
When I want open folder from searching browser, for example YouTube, It keeps all the videos with original format and I can upload as nothing happened. Does viewing the files from browser have any influence to see these files in .mov format ?
Simply try code in different subdirectories. What you will find out that the program is working perfectly to your needs. What happened is that your videos were moved but also compromised into different formats. The list of files that were created was so long you lost track what was in the folder..
Since you mentioned being able to view the videos in their original format when accessed via a browser like YouTube, it indicates that the videos are not lost or corrupted. This is a positive sign.
It might be beneficial to attempt moving files from different subdirectories to ensure the process works as intended. This involves adjusting the source subdirectory and destination directory paths in the code and rerunning it.
void moveFiles(const char *sourceDir, const char *destDir) {
DIR *dir = opendir(sourceDir);
if (dir == NULL) {
perror("Error opening source directory");
return;
}
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
char srcPath[256];
snprintf(srcPath, sizeof(srcPath), "%s/%s", sourceDir, entry->d_name);
struct stat fileStat;
if (stat(srcPath, &fileStat) == 0) {
if (S_ISREG(fileStat.st_mode)) {
if (strstr(entry->d_name, ".str") != NULL) {
// Skip .str files and remove them instead of moving
if (remove(srcPath) != 0) {
perror("Error removing .str file");
}
} else {
char destPath[256];
snprintf(destPath, sizeof(destPath), "%s/%s", destDir, entry->d_name);
if (rename(srcPath, destPath) != 0) {
perror("Error moving file");
}
}
} else if (S_ISDIR(fileStat.st_mode)) {
moveFiles(srcPath, destDir);
}
}
}
}
closedir(dir);
}
in the main function is easier.
const char *sourceSubDir = "/Path/to/subdirectories"; // Source subdirectory
const char *destDir = "/Path/to/destination/directory"; // Destination directory
moveFiles(sourceSubDir, destDir);