Hi
Suppose I have a variable called filename and it it contains the name of a file. I then would like to append an extension to that filename. The filename currently has no extensions.
How do I do this?
Thanks
Hi
Suppose I have a variable called filename and it it contains the name of a file. I then would like to append an extension to that filename. The filename currently has no extensions.
How do I do this?
Thanks
Say you wanna add a .txt extension to the variable
mv $filename ${filename}.txt
Hi
Thanks for your reply. It looks like a relatively easy task, however, it doesn't work on my side.
Here is my code:
First I remove the old file extension
filename=${cfile%%.*}
Then I append the new extension
mv $filename $filename.dsx
And echo the filename to see that our new file has the correct name
echo ${filename##*/}
This is my output:
$ ./Import_PlannedBSC.sh -b
mv: cannot stat `/var/local/dsx/csv/ds_pl_bsc_tester': No such file or directory
ds_pl_bsc_tester
mv: cannot stat `/var/local/dsx/csv/rnChk_pos_bsc_orig_001': No such file or directory
rnChk_pos_bsc_orig_001
Hi.
Shouldn't that be
mv $cfile $filename.dsx
Hi Scottn
This methos gives me the following output:
$ ./Import_PlannedBSC.sh -b
ds_pl_bsc_tester
rnChk_pos_bsc_orig_001
You see, this gives me filenames without the .dsx file extension that I desperately need.
I was looking at the renaming convention as well, still reading up on how to possibly make that work.
I guess you echo at the end the variable "filename" so it prints the without extension which is correct.
If you can post the complete code it will be easier to debug.
Okay it's quite long so here goes:
I have files stored in different directories, hence I have put directory names into variables:
dsxfile="$dsxfile"
csvfile="$csvfile"
filename=""
cfile=""
csvdir="/var/local/dsx/csv"
dsxdir="/var/local/dsx/import"
Here is my scriptname that is called like so from the command line:
./Import_PlannedBSC.sh -b
I have getops that when I call the script with the -b option, it calls a function batchConverter which is the most important part here
while getopts " c: d: b h " option
do
case $option in
c ) csvfile="$OPTARG";;
d ) dsxfile="$OPTARG";;
b ) batchConverter;exit;;
h | ? | * ) displayHelp;exit;;
esac;
done
Here is my function which pulls all files that have filename "_bsc_" pattern and then removes it's file extension called .csv and then appends a new file extension called .dsx
function batchConverter
{
for cfile in $csvdir/*_bsc_*;
do
filename=${cfile%%.*}
mv $cfile $filename.dsx
echo ${filename##*/}
done
}
I want the filename without the path. The I copy these new files to another directory called $dsxdir
filename=${cfile%%.*}.dsx
mv $cfile $dsxdir/${filename##*/}
At what point do I add the new file extension?
You can do
filename=${cfile%%.*}.dsx
mv $cfile $dsxdir/${filename##*/}
Or
filename=${cfile%%.*}
mv $cfile $dsxdir/${filename##*/}.dsx
It really makes no difference...
Thank you, the second option works brilliantly. Problem is that both the files are moved to the $dsxdir directory, but it's no problem, I will try and sort that out.
Thank you so so so much for all your help