当前结构:
Cat and wolf
----- Volume 1.zip
----- Volume 2.zip
Cat and fox
---- Volume 01.rar
Rat and eagle
---- Rat and eagle 01.7x
如您所见,该结构并不遵循简单的模式。我希望它成为文件夹名称+数字。 e.g:
Cat and wolf 01.zip
Cat and wolf 02.zip
Cat and fox 01.rar
Rat and eagle 01.7z
有什么方法可以达到这个结果?
答案 0 :(得分:0)
你们所有文件的模式都是:*/LoremFolderName/Impsum dolore X.?
其中x是在dot [。] FileType之前和空格之后出现的数字
和?是你的任何文件大小;
默认情况下没有任何程序可以做到这一点,但是如果你的意思是编写脚本或程序,你可以在linux中尝试python但是在windows中我推荐一个简单的C#编程;
伪代码:
for each folder FLDR in /Path/To/RootFolder/
{
for each file in FLDR
{
lastScpace=find last occurred space()
newName= copy file name from LastSpace to end
rename file name to (FLDR.Name+newName)
}
}
答案 1 :(得分:0)
这是一个非常具有挑战性的问题。最棘手的方面是递归的要求。但是,正如您所指出的那样,命名模式并不简单。为了构造目标文件名,我们必须从原始文件名stem解析出尾随数字,并将其连接到递归构造的目录名前缀上。
以下是我的解决方案,以bash实现。它迭代给定基目录中的所有对象。对于子文件,它将它们移动到所需文件名下的目标目录中,而对于子目录,它会递归,建立在分隔符字符串上连接的目录名称的前缀。初始基目录,目标目录和分隔符字符串都被参数化为函数参数。为方便起见,我还会在处理完基本目录后将其删除,但前提是它在此时才会被清空。
function renameSubFilesToDest {
local base;
local dest;
local sep;
local prefix;
local file;
local fileBase;
local -i num;
local ext;
local new;
local -i rc;
## parse arguments
if [[ $# -lt 1 ]]; then echo 'too few arguments.' >&2; return 1; fi;
if [[ $# -gt 4 ]]; then echo 'too many arguments.' >&2; return 1; fi;
base="$1"; ## unconditionally take base dir as 1st arg
## take destination dir as optional 2nd arg, default to base if not given
if [[ $# -ge 2 ]]; then dest="$2"; else dest="$base"; fi;
## take name separator as optional 3rd arg, default to space if not given
if [[ $# -ge 3 ]]; then sep="$3"; else sep=' '; fi;
## take new name prefix as optional 4th arg, default to empty string if not given
if [[ $# -ge 4 ]]; then prefix="$4"; else prefix=''; fi;
## iterate over all objects in the base dir
for file in "$base"/*; do
fileBase="$(basename -- "$file";)";
if [[ -d "$file" ]]; then
## recurse on dir, appending file basename and trailing sep to prefix
renameSubFilesToDest "$file" "$dest" "$sep" "$prefix$fileBase$sep";
rc=$?; if [[ $rc -ne 0 ]]; then return 1; fi;
else
## don't process files at the first depth level
if [[ -z "$prefix" ]]; then continue; fi;
## parse num and ext from file name using bash extended regular expressions
if [[ ! "$fileBase" =~ ([1-9][0-9]*)\.([^.]+)$ ]]; then
echo "warning: file \"$fileBase\" does not match expected pattern." >&2;
continue;
fi;
num=${BASH_REMATCH[1]};
ext=${BASH_REMATCH[2]};
## derive the final file name in dest
new="$dest/$prefix$(printf %02d $num).$ext";
printf '%s -> %s\n' "$file" "$new"; ## print status messages at run-time
mv -i -- "$file" "$new";
rc=$?; if [[ $rc -ne 0 ]]; then echo "error: mv [$rc]." >&2; return 1; fi;
fi;
done;
## for convenience, remove the dir if it is now empty
find "$base" -maxdepth 0 -empty -delete;
return 0;
} ## end renameSubFilesToDest()
这是即将到来的演示的一个小辅助函数,它只是在当前目录中设置输入文件结构:
function setupDemo {
mkdir Cat\ and\ wolf Cat\ and\ fox Rat\ and\ eagle;
touch Cat\ and\ wolf/Volume\ {1,2}.zip;
touch Cat\ and\ fox/Volume\ 01.rar
touch Rat\ and\ eagle/Rat\ and\ eagle\ 01.7z
} ## end setupDemo()
这是输入文件结构上的函数演示:
setupDemo; ## set up the file structure
find *; ## show it
## Cat and fox
## Cat and fox/Volume 01.rar
## Cat and wolf
## Cat and wolf/Volume 1.zip
## Cat and wolf/Volume 2.zip
## Rat and eagle
## Rat and eagle/Rat and eagle 01.7z
renameSubFilesToDest .; ## run the solution
## ./Cat and fox/Volume 01.rar -> ./Cat and fox 01.rar
## ./Cat and wolf/Volume 1.zip -> ./Cat and wolf 01.zip
## ./Cat and wolf/Volume 2.zip -> ./Cat and wolf 02.zip
## ./Rat and eagle/Rat and eagle 01.7z -> ./Rat and eagle 01.7z
find *; ## show the result
## Cat and fox 01.rar
## Cat and wolf 01.zip
## Cat and wolf 02.zip
## Rat and eagle 01.7z
正如GhostCat在他的评论中所指出的,这个测试用例在技术上并不涵盖真正的递归文件结构,这需要至少两个级别的子目录。我编写的解决方案是完全递归的,因为你在标题中隐含了递归是目标,而且,无论如何,完全递归的解决方案会更通用,可能更有用。这是第二级演示,我们如何更改此演示的分隔符:
mkdir -p level\ one/level\ two;
touch level\ one/level\ two/some\ file\ 7.txt;
find *;
## level one
## level one/level two
## level one/level two/some file 7.txt
renameSubFilesToDest . . _;
./level one/level two/some file 7.txt -> ./level one_level two_07.txt
find *;
## level one_level two_07.txt
答案 2 :(得分:0)
您可以对文件进行循环并构建目标文件。
find * -mindepth 1 | while read f; do
target=$(echo "${f}" | sed 's#/# #g; s/Volume //')
mv "$f" "${target}"
done
替换斜杠的sed
命令的第一部分可以由内部shell命令替换。
find * -mindepth 1 | while read f; do
target=$(echo "${f//\// }" | sed 's/Volume //')
mv "$f" "${target}"
done
现在你想把数字变成一个额外的数字0.你没有解释这个要求,我假设当你在最后一个点前面有一个数字时,我们必须加一个0。
find * -mindepth 1 | while read f; do
target=$(echo "${f//\// }" |
sed 's/Volume //; s/\([^0-9]\)\([0-9]\)\(\.[^.]*\)/\10\2\3/')
mv "$f" "${target}"
done
您是否意识到您可以通过这种方式丢失文件?
比较x/Cat 2.zip
,x/Cat 02.zip
和x Cat/2.zip
如何找到一个独特的文件名将是另一个问题,也许你首先尝试像
prospectTarget=${target}
counter=2
while [ -f "${target}" ]; do
target=$(echo "${prospectTarget}_${counter}"
((counter++))
done