简体   繁体   English

一次将一个文件移动到另一个目录,提取文件名的一部分,并在所有文件移动后停止

[英]Move one file at a time to another directory, extract part of filename and stop after all files moved

I have n files in source directory (C:\\SRCDIR) example: 我在源目录(C:\\ SRCDIR)示例中有n个文件:

837_FILE
845_FILE

I want to move one file at a time to target directory (C:\\TGTDIR) and take first 3 characters from a filename and jump to that function in linux script. 我想一次将一个文件移动到目标目录(C:\\ TGTDIR),并从文件名中获取前3个字符,然后在linux脚本中跳转至该函数。

I tried below script 我尝试了以下脚本

#!/bin/sh
cd `dirname $0`
srcdir=`C:\SRCDIR`
tgtdir=`C:\TGTDIR`

837()
{
echo "this is 837 file"
}

845()
{
echo "this is 845 file"
}

filecount=$(find $srcdir -maxdepth 1 -type f -name '*.*' | wc -l)
if [ $filecount -gt 0 ] ; then

## want to loop here like, take one file at a time 
## move to target location
## take first three characters of a file and go to that function (837, 845)
## loop until end of files

fi

how to use for loop ? 如何使用for循环?

#!/bin/sh
srcdir="/tmp/src"
tgtdir="/tmp/tgt"
refdir="/tmp/ref"

for filename in ${srcdir}/*; do
    first_3_chars=`echo $(basename ${filename}) | cut -c1-3`;
    wc -l ${refdir}/${first_3_chars}
    mv ${filename} ${tgtdir};
done

Test data: 测试数据:

florin@debian:/tmp$ ll src/
total 0
-rw-r--r-- 1 florin florin 0 iul 30 21:37 280_FILE
-rw-r--r-- 1 florin florin 0 iul 30 21:37 837_FILE
-rw-r--r-- 1 florin florin 0 iul 30 21:37 845_FILE
florin@debian:/tmp$ ll tgt/
total 0
florin@debian:/tmp$ ll ref/
total 4
-rw-r--r-- 1 florin florin 5 iul 30 21:52 280
-rw-r--r-- 1 florin florin 0 iul 30 21:38 837
-rw-r--r-- 1 florin florin 0 iul 30 21:38 845

Run example: 运行示例:

florin@debian:/tmp$ ./test.sh 
1 /tmp/ref/280
0 /tmp/ref/837
0 /tmp/ref/845
florin@debian:/tmp$ ll src/
total 0
florin@debian:/tmp$ ll tgt/
total 0
-rw-r--r-- 1 florin florin 0 iul 30 21:37 280_FILE
-rw-r--r-- 1 florin florin 0 iul 30 21:37 837_FILE
-rw-r--r-- 1 florin florin 0 iul 30 21:37 845_FILE
#!/bin/sh

cd `dirname $0`
prntdir=`pwd`

f837()
{
 echo "this is 837 file $filename"
}

f270()
{
 echo "this is 270 file $filename"
}

filecount=$(find $prntdir/srcdir -maxdepth 1 -type f -name '*.*' | wc -l)

if [ $filecount -gt 0 ] ; then

        for filepath in $prntdir/srcdir/*; do
        filename=$(basename $filepath)
        echo $filename
        mv $prntdir/srcdir/$filename $prntdir/tgtdir/$filnename
        first_3_chars=\f`echo $filename | cut -c1-3`;
        $first_3_chars
        done
else
   echo "no files exist for processing"
fi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM