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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

check if there is an file and then move it to another dir

Status
Not open for further replies.

Marcel1958

Technical User
Oct 11, 2001
38
0
0
NL
In a directory i have several files. For example the following names:

jan.001
jan001.001
jan002.001
feb.001
feb002.

1
i will check if there are files in the directory starting with name jan (this can be different each time i start the script so i have to use an variable)
2
if exist move the first file found to another directory and
rename the file to xxx.001
3
if not exist stop the script

So the first time the script runs it must move the file jan.001 to another dir.
The second time it must be the file jan001.001

Any help is welcom.

Greetings

Marcel

 
Try this:

#!/usr/bin/ksh -p

DIR=PATH_WHERE_FILES_ARE_KEPT
NAME=$1
NDIR=NEW_PATH_WHERE_FILES_WILL_BE_KEPT

if [ $# -ne 1 ]
then
print "You must provide this script with one argument."
fi

for FILES in `/usr/bin/ls ${DIR}/${NAME}*`
do
/usr/bin/mv -f ${DIR}/${FILES} ${NDIR}/${FILES}.001
done



This will keep the file name the same, but move it to a new directory with the .001 extension.

To run, type this:

./SCRIPT_NAME YOUR_VARIABLE_HERE (jan for instance.)
 
This will move all the jobs.

I will only 1 job move to another directory. Then process this file with another script. After this i will start the script again and process the next file.
 
Then try this:

#!/usr/bin/ksh -p

DIR=PATH_WHERE_FILES_ARE_KEPT
NAME=$1
NDIR=NEW_PATH_WHERE_FILES_WILL_BE_KEPT

if [ $# -ne 1 ]
then
print "You must provide this script with one argument."
fi


/usr/bin/ls ${DIR}/${NAME} 1> /dev/null 2> /dev/null
if [ $? -eq 0 ]
then
/usr/bin/mv -f ${DIR}/${NAME} ${NDIR}/${NAME}.001
print "File: ${NAME} has been moved to a new location."
else
print "File: ${NAME} does not exist."
fi

#EOF


I'm not sure what you are trying to do, but it sounds like you are making this a little more complicated then it really needs to be...this other script that will parse through this file for whatever info you need can do the exact same thing like I have here.....you would just need to change the syntax in the other script to run in a for loop to move the file like I have in my original entry and then start processing the file, when finished, the for loop moves to the next file and so on and so on. This way you don't need to have two seperate scripts.
 


/usr/bin/ls ${DIR}/${NAME} 1> /dev/null 2> /dev/null
if [ $? -eq 0 ]

could become:

if [ -f "${DIR}/${NAME}" ] ; then
vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
I knew I was forgetting something when I was writing that statement...still need to get the morning cup of coffee.
 
use the old test:

[ -f $dir/$name ] && do the job

change -f to -s if the file should not be empty
read: man test -----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
Thanks so far. I still have a problem.

There are 3 files in the directory.

jan.001, jan001.001, jan002.001.

With a scheduler program i must parse the parameter for the file. Because i can not see what files in the directory are present i parse the parameter with &quot;jan&quot;. The first time there is a file with this name but the next time the scheduler will parse de parameter &quot;jan&quot; but the name of the file is not &quot;jan&quot; but jan001.001. This file must be moved to another directory and renamed to &quot;jan.txt.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top