TrickierStinky Posted April 16, 2008 Share Posted April 16, 2008 (edited) Hey guys i'm creating a dos style rename script, so if a user types say q14.* as the 1st param and b14.* as the 2nd and will rename all q14 files to b14 but keep the extensions, so i've developed nearly the full script "i think", if i use echo(echo "if $1 had been renamed it would now be $newfile") to simulate the rename it works fine shows q14.1 will be renamed to b14.1q14.2 will be renamed to b14.2 .....etc but it becomes a whole different story if i alter that line to "mv $1 $newFile" it renames the 1st one fine then the rest go along the lines of dosRename q14.* b14.* it dose first one fine but 2nd one ends up q14.1.2 and third q14.1.2.3 any ideas what is up with my script? Edited April 28, 2008 by TrickierStinky Link to comment Share on other sites More sharing options...
TrickierStinky Posted April 18, 2008 Author Share Posted April 18, 2008 anyone? Link to comment Share on other sites More sharing options...
rson451 Posted April 18, 2008 Share Posted April 18, 2008 i can't test right now, but for kicks, have you tried cp then rm verses mv? it will likely produce the same results if there is a problem with your script. Link to comment Share on other sites More sharing options...
aludanyi Posted April 18, 2008 Share Posted April 18, 2008 UNIX has NO file extensions. What you should probably do is to rename a file, but in a way that the characters from the end of the file name after the last dot "." remain the same. Link to comment Share on other sites More sharing options...
TrickierStinky Posted April 18, 2008 Author Share Posted April 18, 2008 (edited) UNIX has NO file extensions. What you should probably do is to rename a file, but in a way that the characters from the end of the file name after the last dot "." remain the same. sorry but I don't fully understand what you mean, I know that Unix files don't have file extensions, I'm doing this for files that have extensions like html, php, and jpeg that sort of stuff and if you see I'm not just allowing the rename of extensions, i'm also trying to change the filename so stuff like dosRename *.html *.htm that would rename every html files to htm aswell as dosRename index.* notindex.* that would rename anything that has index to notindex if this is any help this is the output if a put cp or mv in the script if nobby.1 had been renamed it would now be b14.1if nobby.1.2 had been renamed it would now be b14.1.1.2 and this is the output if i dont include them if nobby.1 had been renamed it would now be b14.1if nobby.1.2 had been renamed it would now be b14.1.2 Edited April 18, 2008 by TrickierStinky Link to comment Share on other sites More sharing options...
aludanyi Posted April 18, 2008 Share Posted April 18, 2008 sorry but I don't fully understand what you mean, I know that Unix files don't have file extensions, I'm doing this for files that have extensions like html, php, and jpeg that sort of stuff and if you see I'm not just allowing the rename of extensions, i'm also trying to change the filename so stuff like that would rename every html files to htm aswell as that would rename anything that has index to notindex if this is any help this is the output if a put cp or mv in the script and this is the output if i dont include them the filename on UNIX file systems are stored in one string, so an extension approach would not help you. What you have to do is to split the string into to part; one part will be from the first character to the last dot ".", the second will be the last dot ".", and everything after the last dot to the end of the string. Then you change the first part but not the second. Link to comment Share on other sites More sharing options...
Borbus Posted April 18, 2008 Share Posted April 18, 2008 Have you tried using bash instead of sh? #! /bin/bash And maybe you could use Python instead... it's a lot easier to work with. Link to comment Share on other sites More sharing options...
TrickierStinky Posted April 18, 2008 Author Share Posted April 18, 2008 (edited) well its part of some Uni work and cant use sh! To aludanyi. well I've done regex to check for ."rest of filename", i know my example show 1.1.2 but even if i just have nobby.2 that would show b14.1.2 Edited April 18, 2008 by TrickierStinky Link to comment Share on other sites More sharing options...
ichi Posted April 18, 2008 Share Posted April 18, 2008 I'm not sure why it fails, but you are repeating some stuff for no actual reason inside the while loop when you could do it just once before, and it's exactly there where it's failing. This would work: endFile=$file fileName=`echo $endFile | sed -e 's/\.\*//'` # <-------- These variables will be the same for every loop iteration. extension=`echo $endFile |sed -e 's/\*\./\./'` # <-------- while [ $# -ne 1 ] do case $type in 'extension') newfile=`echo $1 |sed -e "s/\.[A-Za-z0-9]*/$extension/"` echo "if $1 had been renamed it would now be $newfile" mv $1 $newfile ;; 'main') newFile=`echo $1 | sed -e "s/[A-Za-z0-9]*\.\(.*[A-Za-z0-9]*\)/$fileName\.\1/"` echo "if $1 had been renamed it would now be $newFile" mv $1 $newFile ;; esac shift done ;; Then again you could just do something like rename q14. b14. q14.*. Link to comment Share on other sites More sharing options...
Borbus Posted April 18, 2008 Share Posted April 18, 2008 well its part of some Uni work and cant use sh! Huh? You are using sh. I suggested to use bash instead. If you meant Python, well, I can't see why you can't use Python, it's almost as common as bash on GNU/Linux distros. Link to comment Share on other sites More sharing options...
TrickierStinky Posted April 18, 2008 Author Share Posted April 18, 2008 lol ment bash :blush: sorry I'm not sure why it fails, but you are repeating some stuff for no actual reason inside the while loop when you could do it just once before, and it's exactly there where it's failing. This would work: endFile=$file fileName=`echo $endFile | sed -e 's/\.\*//'` # <-------- These variables will be the same for every loop iteration. extension=`echo $endFile |sed -e 's/\*\./\./'` # <-------- while [ $# -ne 1 ] do case $type in 'extension') newfile=`echo $1 |sed -e "s/\.[A-Za-z0-9]*/$extension/"` echo "if $1 had been renamed it would now be $newfile" mv $1 $newfile ;; 'main') newFile=`echo $1 | sed -e "s/[A-Za-z0-9]*\.\(.*[A-Za-z0-9]*\)/$fileName\.\1/"` echo "if $1 had been renamed it would now be $newFile" mv $1 $newFile ;; esac shift done ;; Then again you could just do something like rename q14. b14. q14.*. thanks will try that and get back to you edit~ brill your a star can't bel;ive was something so daft as that lol Link to comment Share on other sites More sharing options...
Recommended Posts