简体   繁体   English

OSX:将文件复制到具有相同名称的文件夹

[英]OSX: Copying files to folders with same name

I'm looking for a script of some sort, to copy about 800 files with separate case numbers ex: "1234567 - XXXX XXX" of which is placed in one folder, into a directory of another folder(a server) with the same case name, under a subfolder ex: 我正在寻找某种脚本,以将大约800个文件(例如:“ 1234567-XXXX XXX”)放置在一个文件夹中,并将单独的案例编号复制到另一个具有相同案例的文件夹(服务器)的目录中名称,在子文件夹下,例如:

"1234567 - XXXX XXX/01_Management/01_Correspondence" (subfolder identical for all folders) “ 1234567-XXXX XXX / 01_Management / 01_Correspondence”(所有文件夹的子文件夹相同)

Sum up: Copying one file, to another folder with the same name as the file, placing it in a subfolder. 总结:将一个文件复制到另一个与该文件同名的文件夹中,然后将其放在子文件夹中。

macOS Sierra macOS Sierra

Thank you -Theis 谢谢-Theis

You could do that with a bash script. 您可以使用bash脚本来做到这一点。

Make a back-up first! 首先进行备份!

Save the following in your HOME directory as reorganise : 将以下内容保存到HOME目录中,作为reorganise

#!/bin/bash

find $(pwd) -name "* -*" -print0 | while read -d $'\0' f; do
   echo Processing file $f...
   base=$(basename "$f")
   echo "   base: $base"
   case=${base/ -*/}
   echo "   case: $case"
   # EDIT THE FOLLOWING LINE TO BE CORRECT FOR YOU !!!
   newplace="/path/to/new/place/$case"
   echo mkdir -p "$newplace"
   echo cp "$f" "$newplace"
done

Then start Terminal and type the following just once to make the script executable: 然后启动Terminal并仅输入以下命令一次即可使脚本可执行:

chmod +x reorganise

Then go to the directory where your files are: 然后转到文件所在的目录:

cd where/your/files/are

And then you can run the script with: 然后,您可以使用以下命令运行脚本:

$HOME/organise

and it will tell you what it would do, without doing anything. 它会告诉您它将做什么,而不做任何事情。 If you like it, you can remove the word echo on the second and third to last lines and run it again. 如果愿意,可以删除倒数第二行和倒数第三行的单词echo ,然后再次运行。

You will need to edit the indicated line at least. 您至少需要编辑指示的行。

Sample Output 样本输出

Processing file /Users/mark/tmp/1234563 - fredb...
   base: 1234563 - fredb
   case: 1234563
mkdir -p /path/to/new/place/1234563
cp /Users/mark/tmp/1234563 - fredb /path/to/new/place/1234563
Processing file /Users/mark/tmp/1234567 - fred...
   base: 1234567 - fred
   case: 1234567
mkdir -p /path/to/new/place/1234567
cp /Users/mark/tmp/1234567 - fred /path/to/new/place/1234567

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

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