Hello Friends
When renaming MSW upper-case filenames to lowercase,
there is 'lc' from shtools, and other ways too. Or, for
massive multiway multioption renaming there are heavy-duty
packages like 'krename'.
But i felt a lack of a small handy thing, particularly when
handling mp3 files from MSW origin in our GNU-Linux
environment, with different packages like 'mplayer' through
bash command line.
So, Debashish Das of our GLT Madhyamgram made these three
things.
1. 'mymv' : that works on files in a dir and gets away all the
space-s within filenames, either kills them with an option '-k',
or replaces them with a dot(.) with option '-d'. And after the
change, midway, asks you, if you want to undo it. This midway
files you can view with another console or a browswer showing
the same directory. There, in midway, you can undo it with an
option '1' or leave the files as they are after the change, with
any other integer.
2. 'myunmv' : that just undoes the change that 'mymv' did on the
filenames, that is replaces every dot within a filename with
a space, leaving out the last dot that usually shows filename
extension, that stupid thing gradually getting progressively
imported from MSW into GNU-Linux. Just see by touching a blank
file just with an extension '.mp3' or '.txt' and see with what
icons they are attached in KDE-Konqueror. This, probably goes
against the whole unix philosophy working file-magic. Anyway,
'myunmv' does keep the last dot with extension and re-replaces
all the dots.
3. 'mydirunmv' : that does exactly the same thing as 'myunmv',
with one extra task, if there is a sub-directory within this
directory, does not deal the last dot and remaining part as an
extension and replaces that dot too in the case of a directory
name.
Just save these three respective codes given below into three
files: 'mymv.c', 'myunmv.c' and 'mydirunmv.c'. And then, what i
did was:
gcc -o ~/bin/mymv mymv.c
gcc -o ~/bin/myunmv myunmv.c
gcc -o ~/bin/mydirunmv mydirunmv.c
This saves the binaries in my own binary directory and now
anywhere i can work with them. If after working with, you
dont like, you can remove them easily from there. And one
(maybe not that unwelcome) thing about the codes is that
they dont work on MSW platforms, most probably for the
difference in the structure of the system calls made by
headers like dirent included within code.
Debashish cannot afford a net-connection of his own. But i was
really feeling so nice about it that i couldnt do without
sharing it with you. He will get the responses, if any from you,
from my box.
dipankar das
CODE
====
1. 'mymv.c'
===========
/* Program for renaming file by removing space or filling with dots. */
#include <stdio.h>
#include <dirent.h>
#include <malloc.h>
struct dirent *entry;
main(int argc, char *argv[])
{
char *arr[200],*newarr[200],*realarr[200];
int ch;
int count=0,i,choice;
DIR *dir=opendir("."); /* Openning the current directory
Returns pointer if OK,NULL on error */
while((entry=readdir(dir))!=NULL) /* Reading Directories,
Returns pointer if OK,NULL
at end of directory or error
*/
{
arr[count]=(char *)malloc(150*sizeof(char));
strcpy(arr[count],entry->d_name); /* Copying the directory contents
into the specified array */
count=count+1;
}
for(i=0;i<count;i++)
{
realarr[i]=(char *)malloc(130*sizeof(char));
strcpy(realarr[i],arr[i]);
/* Copying the directory contents into another array */
}
if(strcmp(argv[1],"-k")==0) /* Option for removing spaces
in the file or directory name */
{
for(i=0;i<count;i++)
{
newarr[i]=(char *)malloc(130*sizeof(char));
strcpy(newarr[i],arr[i]);
bfill(newarr[i]); /* Removing the spaces
from the file or directory name
*/
rename(arr[i],newarr[i]);
/* Renaming the file or directory
with the changed name */
}
}
else
if(strcmp(argv[1],"-d")==0) /* Option for putting dots in the
spaces in the file or directory name
*/
{
for(i=0;i<count;i++)
{
newarr[i]=(char *)malloc(130*sizeof(char));
strcpy(newarr[i],arr[i]);
filldot(newarr[i]); /* Replacing spaces with dots
in file or directory name. */
rename(arr[i],newarr[i]); /* Renaming the file or directory
with the changed name. */
}
}
printf(" Do you want to undo it:");
printf(" Please enter '1' to undo:(enter any other digit to exit)");
/* Option for undo */
scanf("%d",&ch);
if(ch==1)
{
for(i=0;i<count;i++)
rename(newarr[i],realarr[i]); /* Renaming the changed name
with the real name. */
}
else
printf("\nThanks\n");
}
/* Function for removing spaces. */
bfill(char s[])
{
int count,i,j,k,n;
count=strlen(s); /* Calculating the length of the file name */
for(n=0;n<count;n++) /* For removing more than one space. */
{
for(i=0;i<count;i++)
{
if(s[i]==' ') /* Checking for space in the filename. */
{
for(k=i,j=k+1;k<count;k++,j++)
s[k]=s[j]; /* Replacing the space with
the very next character. */
}
else
continue;
}
}
}
/* Function for filling spaces with dot. */
filldot(char s[])
{
int count,i,j,k,n;
count=strlen(s);
for(n=0;n<count;n++) /* For replacing more than
one space with dots. */
{
for(i=0;i<count;i++)
{
if(s[i]==' ')
{
s[i]='.'; /* Replacing the space with dot. */
}
else
continue;
}
}
}
2. 'myunmv.c'
=============
/* Program to rename files or directories which contain
dots by replacing dots with space. */
#include <stdio.h>
#include <dirent.h>
#include <malloc.h>
struct dirent *entry;
main()
{
char *arr[200],*newarr[200];
int ch;
int count=0,i;
DIR *dir=opendir("."); /* Openning the current directory
Returns pointer if OK,NULL on error */
while((entry=readdir(dir))!=NULL) /* Reading Directories,
Returns pointer if OK,NULL
at end of directory or error */
{
arr[count]=(char *)malloc(150*sizeof(char));
strcpy(arr[count],entry->d_name); /*Copying the directorycontents
into the specified array */
count=count+1;
}
for(i=0;i<count;i++)
{
newarr[i]=(char *)malloc(150*sizeof(char));
strcpy(newarr[i],arr[i]); /* Keeping the contents of
the array into another array */
removedot(newarr[i]); /* Replacing all dots with spaces
in the file name except the
last dot before extension */
rename(arr[i],newarr[i]); /* Renaming the file with the new name. */
}
}
/* Function to replace the dots with space in the filename. */
removedot(char s[])
{
int len,i,m,j,count=0;
len=strlen(s);
for(i=0;i<len;i++) /* Calculating the length of the file name
*/
{
if(s[i]=='.')
{
m=i; /* Determining the last dot position. */
count=count+1; /* Calculating the total number
of dots in the file name */
}
}
if(count!=0) /* Checking if there is
any dot in the filename. */
for(j=0;j<m;j++)
{
if(s[j]=='.')
{
s[j]=' '; /* all the dots replaced
by space-s except the last dot. */
}
}
else
for(i=0;i<len;i++)
s[i]=s[i]; /*As there is no dot in the
filename it remains unchanged. */
}
3. 'mydirunmv.c'
================
/* Program to rename files or directories which contain
dots by replacing dots with space. */
#include <stdio.h>
#include <dirent.h>
#include <malloc.h>
#include <sys/types.h>
#include <sys/stat.h>
struct dirent *entry;
struct stat buf;
main()
{
char *dirarr[200],*filearr[200],*dirnewarr[200],*filenewarr[200];
int ch;
int dcount=0,fcount=0,i;
DIR *dir=opendir("."); /* Openning the current directory
Returns pointer if OK,NULL on error */
while((entry=readdir(dir))!=NULL) /* Reading Directories,
Returns pointer if OK,NULL
at end of directory or error */
{
if(lstat(entry->d_name,&buf) ==0) /* Checking if any symbolic link
exists between the buffer and
the directory-entry */
if(S_ISDIR(buf.st_mode)) /* Checking if the entry is a
directory or file*/
{
dirarr[dcount]=(char *)malloc(150*sizeof(char));
strcpy(dirarr[dcount],entry->d_name); /*Copying the directorycontents
into the specified array */
dcount=dcount+1;
}
else
{
filearr[fcount]=(char *)malloc(150*sizeof(char));
strcpy(filearr[fcount],entry->d_name); /* Copying the directory contents
into the specified array */
fcount=fcount+1;
}
}
for(i=0;i<fcount;i++)
{
filenewarr[i]=(char *)malloc(150*sizeof(char));
strcpy(filenewarr[i],filearr[i]); /* Keeping the contents of
the array into another array */
fileremovedot(filenewarr[i]); /* Replacing all dots with spaces
in the file name except the
last dot before extension */
rename(filearr[i],filenewarr[i]); /* Renaming the file with the new name.
*/
}
for(i=0;i<dcount;i++)
{
dirnewarr[i]=(char *)malloc(150*sizeof(char));
strcpy(dirnewarr[i],dirarr[i]); /* Keeping the contents of
the directory-entry into another
array */
dirremovedot(dirnewarr[i]); /* Replacing all dots with spaces
in the directory-name
*/
rename(dirarr[i],dirnewarr[i]); /* Renaming the directory with the new
name. */
}
}
/* Function to replace the dots with space in the filename. */
fileremovedot(char s[])
{
int len,i,m,j,count=0;
len=strlen(s);
for(i=0;i<len;i++) /* Calculating the length of the file name
*/
{
if(s[i]=='.')
{
m=i; /* Determining the last dot position. */
count=count+1; /* Calculating the total number
of dots in the file name */
}
}
if(count!=0) /* Checking if there is
any dot in the filename. */
for(j=0;j<m;j++)
{
if(s[j]=='.')
{
s[j]=' '; /* all the dots replaced
by space-s except the last dot. */
}
}
else
for(i=0;i<len;i++)
s[i]=s[i]; /*As there is no dot in the
filename it remains unchanged. */
}
dirremovedot(char s[]) /*Function to replace the dots with space-s in
the
directory-name */
{
int len,i;
len=strlen(s);
for(i=0;i<len;i++) /* Calculating the length of the file name
*/
{
if(s[i]=='.')
{
s[i]=' ';
}
}
}
--
To unsubscribe, send mail to [EMAIL PROTECTED] with the body
"unsubscribe ilug-cal" and an empty subject line.
FAQ: http://www.ilug-cal.org/node.php?id=3