排除下载文件

时间:2015-01-25 11:12:20

标签: shell batch-file synchronization sync server

我正在使用服务器。我喜欢与家用电脑同步文件。遗憾的是,没有任何应用程序可以记住以前下载的文件 - 所以如果文件从“传入”文件夹移出,它将从服务器重新加载。

我找到了一个能够运行脚本或批量预下载的app(goodsync)。我正在考虑,我提前运行一个脚本,根据这个将所有文件名写入.txt - > http://stackoverflow.com/questions/8612324/is-this-possible-to-write-list-of-file-names-into-a-text-file-using-batch-file-w

那么,如何编写脚本或批处理“检查C:\ exclude.txt中的内容并且不要下载具有完全相同名称的文件”

2 个答案:

答案 0 :(得分:0)

好的,这里有一个小shell脚本,可用于读取已下载的文件,读取存在的文件,然后检查应下载哪些文件。但我建议你学习shell脚本来理解脚本并使脚本更好。

基本上这个代码的作用是读取downloaded.txt(或exclude.txt)文件(每行应该包含文件名和哈希连接)。 然后它从文件系统中读取所有文件,并检查该文件是否包含在downloaded.txt文件中。

如果该文件未包含在downloaded.txt文件中,您可以决定要执行的操作。

  • 您可以创建一个包含所需文件的新文本文件 下载。
  • 您可以将找到的文件添加到downloaded.txt(但使用搜索到的变量,因为它包含散列和文件名)并下载这些文件。

    found=0
    array_contains () { 
        local array="$1[@]"
        local seeking=$2
        found=0
        for element in "${!array}"; do
            if [[ $element == $seeking ]]; then
            found=1
            break
            fi
        done
    }


    downloaded_files=()

    #Load the downloaded files from downloaded.txt into the downloaded_files array
    old_IFS=$IFS      # save the field separator           
    IFS=$'\n'     # new field separator, the end of line           
    for line in $(cat downloaded.txt)          
    do          
       downloaded_files=("${downloaded_files[@]}" "$line")          
    done          


    #print the whole array just for testing
    echo ${downloaded_files[@]}


    filehashlist=($(find * -type f -exec sha1sum "{}" \; | cut -d ' ' -f 1))
    filenamelist=($(find * -type f -exec basename "{}" \;))
    filenamelistFullPath=($(find * -type f))


    to_download_files=()
    counter=0
    for filename in "${filenamelist[@]}"
    do
        searched=$filename${filehashlist[$counter]}
        array_contains downloaded_files "$searched"
        if [ $found == "0" ]; then
            echo "Please download file: $filename"
            echo "Location: ${filenamelistFullPath[$counter]}"
            echo "Hash: ${filehashlist[$counter]}"
            echo "Add this line to downloaded.txt after download: $searched"        
            echo ""
        fi
        counter=$counter+1
    done

    IFS=$old_IFS     # restore default field separator 

答案 1 :(得分:0)

从命令行或.CMD脚本:

xcopy /D /E /F /Y /EXCLUDE:exclude.txt

其中exclude.txt是您提到的文件,其中包含要排除的文件名列表

相关问题