根据pdf文件的页数批量重命名

根据pdf文件的页数批量重命名

我有很多 pdf 文件格式的论文(同一文件夹中还有其他类型的文件)。我想使用该模式重命名它们

<current file name>[<number of pages>].pdf

但经过一番尝试后,我得到了很多错误的文件名,并且末尾有重复的页数。例如,

Madsen - 分类[48][48].pdf

所以,我想:

  1. 清理所有文件名,删除末尾括号中出现的任何数字。例如,上面的文件应重命名为

马德森 - 分类.pdf

  1. [<number of pages>]重命名刚刚插入末尾的所有文件。

应该[<number of pages>]从 pdf 文件中读取,因为我没有此信息。

答案1

这是一种简单的方法,可以完全解决您原来的问题。可以作为一行完成,但这种语法使其具有可读性。

#!/bin/bash
for F in "$@"
do
echo mv "$F" "${F%.pdf}[$(pdfinfo "$F" | awk '/^Pages/{print $NF}')].pdf"
done
$ ls *pdf
aosa-bash.pdf  article.pdf  bash.pdf  bashref.pdf  rose94.pdf
$ find . -name \*.pdf -exec ./pdf.sh {} +
mv ./article.pdf ./article[11].pdf
mv ./bashref.pdf ./bashref[172].pdf
mv ./bash.pdf ./bash[75].pdf
mv ./aosa-bash.pdf ./aosa-bash[14].pdf
mv ./rose94.pdf ./rose94[13].pdf
$

如果对建议的重命名感到满意,请将所有mv命令粘贴到 shell 窗口中或修改脚本,将 替换echo mv为简单的mv.

答案2

因此,这里有两个脚本,您可以将它们放入需要重命名的 pdf 文件的文件夹中。

第一个用于添加页码,第二个用于删除页码。两个脚本都是交互式的

  • 类型中止输入退出脚本,
  • 类型输入进入逐个文件交互模式
  • 如果你想要一个脚本无需确认即可继续处理所有 pdf 文件类型y 输入 y 输入

在 file.pdf 中添加页数的脚本 (file.pdf -> file[N].pdf)

#!/bin/bash

IFS='
'
ret="not_ok";

renameinteractively () {
for file in *.pdf ; do 
  # derive number of pages in a pdf document
    npages=`pdfinfo $file | grep Pages | awk -e '{print $2}'`;
  # make up a new filename
    file2=${file%%.pdf}[$npages].pdf;
  # an auxiliary variable
    ret="not_ok";

  # interactive part. Ask to rename or not.
    printf "Rename\n $file to \n $file2 ? (y/n/abort) \n";
    until [ $ret == 'ok' ]; do
  # read your answer and y-for rename, n-skip and abort-to exit
      read ans;
      if [ $ans == "y" ]
      then 
          mv $file $file2;
          ret="ok";
          continue;
      elif [ $ans == "n" ]
      then 
          ret="ok";
          break;
      elif [ $ans == "abort" ]
      then 
          exit;
      else
          printf "Enter 'y', 'n' or 'abort', please! \n\n";
      fi
      done;
  done ;
}

renameallofthem () {
until [ $ret == 'ok' ]; do
      if [ $a == "y" ]
      then 
    for file in *.pdf ; do 
      npages=`pdfinfo $file | grep Pages | awk -e '{print $2}'`;
      mv $file ${file%%.pdf}[$npages].pdf; 
      printf "\nMoved \n"
      echo $file
      printf "to\n"
      echo ${file%%.pdf}[$npages].pdf
    done ;
    ret="ok";
    exit;
      elif [ $a == "n" ]
      then 
    printf "\n\n OK, let's do it interactively!\n\n"
    renameinteractively;
      elif [ $a == "abort" ]
      then 
    exit;
      else
    printf "Enter 'y', 'n' or 'abort', please! \n\n";
      fi;
done;
}

printf "Rename ALL of the .pdf files in current folder? (y/n/abort) \n"
read a
if [ $a == "y" ]
then
      printf "Really??\n\n Do we rename ALL of them? (y/n/abort) \n"
      read a
      renameallofthem;
elif [ $a == "n" ]
then 
  printf "\n\n OK, let's do it interactively!!\n\n"
  renameinteractively;
elif [ $a == "abort" ]
then 
    exit;
else
    printf "Enter 'y', 'n' or 'abort', please! \n\n";
fi

从文件[N][N]....[N].pdf 中删除页数的脚本(文件[N][N]....[N].pdf -> file.pdf)

#!/bin/bash

IFS='
'
deleteNumbersInteractively () {
for file in `ls *].pdf | grep '\[[0-9]*\]'` ; do 
# an auxiliary variable
  ret="not_ok";
# make up a new filename
  file2=`echo $file | sed -e 's/\[[0-9]\+\]//g' `;
# interactive part. Ask to rename or not.    
  printf "Rename\n $file to \n $file2 ? (y/n/abort) \n";
  until [ $ret == 'ok' ]; do
      read ans;
      if [ $ans == "y" ]
      then 
      mv $file $file2;
      ret="ok";
      continue;
      elif [ $ans == "n" ]
      then 
      ret="ok";
      continue;
      elif [ $ans == "abort" ]
      then 
      exit;
      else
      printf "Enter 'y', 'n' or 'abort', please! \n\n";
      fi
  done;
done;
}
deleteAllTheNumbers () {
until [ $ret == 'ok' ]; do
      if [ $a == "y" ]
      then 
    for file in *.pdf ; do 
      file2=`echo $file | sed -e 's/\[[0-9]\+\]//g' `;
      mv $file $file2; 
      printf "\nMoved \n"
      echo $file
      printf "to\n"
      echo $file2
    done ;
    ret="ok";
    exit;
      elif [ $a == "n" ]
      then 
    printf "\n\n OK, let's do it interactively!\n\n"
    deleteNumbersInteractively;
      elif [ $a == "abort" ]
      then 
    exit;
      else
    printf "Enter 'y', 'n' or 'abort', please! \n\n";
      fi;
done;
}


printf "Delete ALL of the filename[NUMBERS].pdf from files in current folder? (y/n/abort) \n"
read a
if [ $a == "y" ]
then
      printf "Really??\n\n Do we rename ALL of them? (y/n/abort) \n"
      read a
      deleteAllTheNumbers;
elif [ $a == "n" ]
then 
  printf "\n\n OK, let's do it interactively!!\n\n"
  deleteNumbersInteractively;
elif [ $a == "abort" ]
then 
    exit;
else
    printf "Enter 'y', 'n' or 'abort', please! \n\n";
fi

现在一切都好吗?

相关内容