Link to home
Start Free TrialLog in
Avatar of kbalaraju
kbalaraju

asked on

Changing name of a file.

I have some files in a folder with filename in format 123456.78j.
I would like to rename the corresponding file name to 12345678.jpg and I need to move it to a folder(Which is already existing with 2 digits after decimal point in filename(in this case 78)). I should do it with all the files existing in my input folder c:\input.
Can anyone help???
Thanks in Advance
-Raju
Avatar of snifong
snifong
Flag of United States of America image

What O/S?  Shell script or program?
Avatar of nietod
nietod

standard C++ has no features for this.

You must use OS-specific or compiler-specific features for this.  In windows you can use the MoveFile() function.  Are you using windows?
Avatar of kbalaraju

ASKER

No. I'm using Unix and I have about 300 files in the folder. It would be fine, If it is a shellscript in Unix.
Thanks
Raju.
You can give me the code in C/C++/Unix Shell script.
Thanks
Raju
ASKER CERTIFIED SOLUTION
Avatar of wytze
wytze

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
what if I want to move the file to parent directory/base2.
ex.
source file 98439.88j
mv the file to ../88/98439.jpg
What should I change in script file?
what if I want to move the file to parent directory/base2.
ex.
source file 98439.88j
mv the file to ../88/98439.jpg
What should I change in script file?
what if I want to move the file to parent directory/base2.
ex.
source file 98439.88j
mv the file to ../88/98439.jpg
What should I change in script file?
what if I want to move the file to parent directory/base2.
ex.
source file 98439.88j
mv the file to ../88/98439.jpg
What should I change in script file?
echo "../"$base2"/"$base".jpg" > $new_name
Adjusted points to 50
I am still not clear about constructing mv command.
How do write mv in script ?
I am still not clear about constructing mv command.
How do write mv in script ?
#! /bin/sh
for i in *
do
      base=` echo $i |  awk '{$foo=substr($0,1,length($0)-4);print $foo;}' `
      base2=` echo $i |  awk '{$foo=substr($0,length($0)-2,2);print $foo;}' `
      new_name=`echo "../"$base2"/"$base".jpg"`
      echo "moving "  $i " to " $new_name
      mv $i $new_name
done

Allthough Kernighan states that
echo $i > new_name
should work, it doesn't (not on every system at least). Therefore a small change in the construction of the new_name. This should work, sorry for the non working version :-)