• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

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
0
kbalaraju
Asked:
kbalaraju
1 Solution
 
snifongCommented:
What O/S?  Shell script or program?
0
 
nietodCommented:
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?
0
 
kbalarajuAuthor Commented:
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.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
kbalarajuAuthor Commented:
You can give me the code in C/C++/Unix Shell script.
Thanks
Raju
0
 
wytzeCommented:
#! /bin/sh
for i in *
do
      echo $i
      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;}' `
      echo $base2"/"$base$base2".jpg" > $new_name
      echo $new_name
      # create the move command
      # something like: mv $i $new_name
      # i cannot test this without any dir struc
done
0
 
kbalarajuAuthor Commented:
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?
0
 
kbalarajuAuthor Commented:
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?
0
 
kbalarajuAuthor Commented:
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?
0
 
kbalarajuAuthor Commented:
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?
0
 
wytzeCommented:
echo "../"$base2"/"$base".jpg" > $new_name
0
 
kbalarajuAuthor Commented:
Adjusted points to 50
0
 
kbalarajuAuthor Commented:
I am still not clear about constructing mv command.
How do write mv in script ?
0
 
kbalarajuAuthor Commented:
I am still not clear about constructing mv command.
How do write mv in script ?
0
 
wytzeCommented:
#! /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 :-)
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now