Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to copy file(s) listing 1 by 1 to another path with $variable

Status
Not open for further replies.

baggetta

Technical User
Feb 27, 2003
116
US
The script below needs a touch up on how to strip off the path of variable ${FILE} so that it works properly in the cp and mv commands. I need to rename the file1.edi to a new path and add the batch # variable.

Example:
I have 2 files under /mfg_edi: file1.edi file2.edi
The $FILE will equal to below when doing "ls":
/mfg_edi/file1.edi
/mfg_edi/file2.edi

When I execute it, I get these results:
File to copy: /mfg_edi/file1.edi --->ok
cp /mfg_edi/file1.edi /mfg_edi/recv/1234/mfg_edi/file1.edi.dat ---->problem


#/bin/csh
umask 000
ls ${mfg_edi}*.*
if ( ! $status ) then
foreach FFILE (${mfg_edi}*.*)
set FILE=$FFILE
echo 'File to copy:' ${FILE}
cp ${FILE} ${mfg_edi_recv}${edi_batch}${FILE}.dat
mv -f ${FILE} /mfg/backup
echo 'File has been moved'
end
endif

The 1234 = ${edi_batch} is a sequential number that gets assigned when I run this script under the program.
 
I don't know the C shell, but in the Korn shell you could do something like this...
Code:
#!/bin/ksh

umask 000

mkdir ${mfg_edi_recv}${edi_batch}

for FNAME in ${mfg_edi}*
do
  print -n "Copying file ${FNAME}"
  cp ${FNAME} ${mfg_edi_recv}${edi_batch}$(basename ${FNAME}).dat
  if ( $? )
  then
    print " - ERROR copying ${FNAME}"
  else
    print ", Copy successful!"
    mv -f ${FNAME} /mfg/backup
  fi
done
What is the error message you get on your copy? Does the directory you're trying to copy to exist?

Also, you need to strip off the path from the filename, or that becomes patr of the destination name.

Hope this helps.

 
Try something like this:
Code:
#/bin/csh
umask 000
ls ${mfg_edi}*.*
if ( ! $status ) then
  foreach FFILE (${mfg_edi}*.*)
    set FILE=`basename ${FFILE} .edi`
    echo 'File to copy:' ${FFILE}
    cp ${FFILE} ${mfg_edi_recv}${edi_batch}${FILE}.dat
    mv -f ${FFILE} /mfg/backup/${FILE}.edi
    echo 'File has been moved'
  end
endif

Hope This Help
PH.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top