How to write a C Program to Recursively converts all file-names to lower-case in C Programming Language ?
Solution:
/*Recursively converts all file-names to lower-case.*/ #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <dirent.h> #include <unistd.h> #include <string.h> #include <ctype.h> int verbose = 0; int convertDirectory ( const char *name ) /* returns number of converted file-names, -1 if failed */ { DIR *dir; char fn[2048], oldFn[2048], *ptr; int len, rw, counter, needs, subCounter; struct dirent *entry; struct stat st; if ( !name || !name[0] ) return -1; dir = opendir(name); if ( !dir ) return -1; strcpy(fn,name); len = strlen(fn); fn[len++] = '/'; rw = 0; /* the directory is writtable? */ counter = 0; /* number of changed file-names */ while ( (entry = readdir(dir)) ) { /* process one directory entry */ if ( entry->d_name[0] == '.' && (!entry->d_name[1] || entry->d_name[1] == '.' && !entry->d_name[2]) ) continue; /* special directory */ /* assemble the item name: */ strcpy(fn+len,entry->d_name); /* lower-case conversion: */ needs = 0; for ( ptr = fn + len; *ptr; ptr++ ) if ( isupper(*ptr) ) { needs = 1; break; } /* the directory-entry needs to be converted: */ if ( needs ) { if ( !rw ) { /* make the mother directory writable */ fn[len-1] = (char)0; if ( !stat(fn,&st) && !(st.st_mode & S_IWUSR) ) chmod(fn,st.st_mode|S_IWUSR); fn[len-1] = '/'; rw = 1; } strcpy(oldFn,fn); for ( ptr = fn + len; *ptr; ptr++ ) if ( isupper(*ptr) ) *ptr = tolower(*ptr); rename(oldFn,fn); if ( verbose ) printf("%s -> %s\n",oldFn,fn); counter++; } /* stat the entry .. subdirectories must be traversed */ if ( !stat(fn,&st) && S_ISDIR(st.st_mode) ) { /* directory */ subCounter = convertDirectory(fn); if ( subCounter >= 0 ) counter += subCounter; else if ( verbose ) printf("Error converting '%s' directory\n",fn); } } closedir(dir); return counter; } int main ( int argc, char *argv[] ) { /* TODO: command-line argument: directory to convert */ /* TODO: -v (verbose) */ int converted = convertDirectory("."); if ( converted <= 0 ) printf("No file-names were converted\n"); else printf("%d file-names were converted\n",converted); return 0; }
Learn More :
File
- COPY DATA FROM ONE FILE TO ANOTHER FILE USING C PROGRAM
- C Program File Input & Output Example
- C Program To Destruc Self Execution File ?
- C Program To Store Students Record In A Text File
- C Program To Write Data In A File And Search Data From File
- Make a copy of file given in argv[1] to file given in argv[2]
- C Program to copy a file to another file
- C Function to read instructions from the file and obey them
- Client/Server C program to make client send the name of a file
- A small I/O bound program to copy N bytes from an input file to an output file.
- C Program to Read and Write FIFO
- File Number Control C Program Example
- C Program to opens and reads dictionary file specified in spellrc
- C Program to Find the Size of File using File Handling Function
- C Program Recursive function that searches for a given file in a given folder
- C Program Read a char & print next char ( file to file )
- C Program to Input Student Details into a File For Two Subjects
- Identifies the architecture Windows PE (exe or dll) file was compiled C Program
- File Handling (console to file) in C Program
Convert
- How To Write a C program that reads your first and last names and then converts them to upper-case and lower-case letters. The results will be printed to standard output console.
- C Program to Convert Text String to Binary
- C Program To Convert Decimal To Binary Using Recursion
- C Program To Convert Temperature In Celsius To Fahrenheit, Using Function
- C Program To Convert Temperature Into Celsius
- C Program to Convert Celsius to Fahrenheit temperature
- C Program to convert a string to upper case
- C Program Convert MIDI note to pitch
- C Program to Convert Celcius to Fahrenheit and vice versa
- C Program to Convert a Decimal number to Octal Number
- C Program to Convert Number to Words in C
- C Program to Converts a Number to a String.
- C Program to Convert Celsius Temperature into Fahrenheit Temperature
- C Program to accept string from user & convert all lower case letters into upper case letters & vice versa
- C Program to convert given decimal number into binary number
- C Program To Read The Adjecancy Matrix of Directed Graph And Convert It Into Adjecancy List
Lower Case
- How To Write a C program that reads your first and last names and then converts them to upper-case and lower-case letters. The results will be printed to standard output console.
- Lower-Upper Case/Positive-Negative Integer
- C Program Anagrams
- C Program to accept string from user & convert all lower case letters into upper case letters & vice versa
Name
- How To Write a C program that reads your first and last names and then converts them to upper-case and lower-case letters. The results will be printed to standard output console.
- Client/Server C program to make client send the name of a file
- C Program to accept 5 names from user & store these names into an array. Sort these array elements in alphabetical order