Implementation of batch renaming files in bat and python

高洛峰
Release: 2017-03-02 16:18:38
Original
1624 people have browsed it

I recently downloaded a batch of documents from a website, but the files are named with numerical strings (many libraries are like this). Now I have downloaded these files, and there is also a list of these files, but they cannot be listed one by one. Let’s rename the file, so I found these scripts from the Internet.

1. Use bat script (available by default in windows system)

Open Notepad, write these codes into Notepad, and save it as xx.bat file (Pay attention to the suffix name. Many novices save it as xx.bat.txt because the txt is hidden and think it won’t work)

@echo off
for /r “d:\pdf” %%a in (*.pdf) do (
  for /f “tokens=1,2 delims= ” %%b in (1.txt) do ( 
  if “%%~nxa”==”%%b” ren “%%a” “%%c.pdf”
  )
)
Copy after login

Requires the file to be placed in d In the :/pdf folder, the file suffix is ​​.pdf, and the file list is placed under 1.txt. Just save the txt as the default ANSI format under Microsoft. The internal content format is as follows:

ts001003.pdf One Hundred Volume Panorama of World Science and Technology (3) Modern Science and Technology
ts001004.pdf One Hundred Volumes Panorama of World Science and Technology (4) Revolution brought by the Steam Engine
ts001005.pdf One Hundred Volumes Panorama of World Science and Technology (5) Modern Science and Technology

Can be modified appropriately if necessary.


2. Use python script (windows system needs to install python 3.50 compilation software, about 30M, Linux is estimated to need to upgrade python to 3.50)

This script is I spent several hours writing a script (although I have learned programming for a long time, I still have trouble writing a good one fluently)
Open Notepad, write these codes into Notepad, and save it as xx.bat file (pay attention to the suffix name, many novices save it as xx.bat.txt, because the txt is hidden, so they think it won’t work)

#!/bin/env python
# -*- coding: utf-8 -*-
"""从某网站下载了一批文档,但是文件是用数字串命名的文档(很多图书馆都这样吧),
也有文档列表,所以写了一个脚本来重命名批文件

"""
__author__ = 'rublog'
import os

#1.txt文档要求每个文档一行,保存的时候必须为ANSI格式,前面是列表文档名含后缀(就是网站上文件名,一串数字
#或者字母什么的),空一格,然后是文档的真名(不带后缀)
#get_list这个从1.txt文本文件中一行一行的读取文件,去掉换行符,然后调用doc_rename
#函数
def get_list():
  #尝试不同的编码来自知乎 十五
  #http://www.php.cn/
  decode_list=["utf-8",'gb18030', 'ISO-8859-2','gb2312',"gbk","Error" ]#编码集
  #GBK不如GB18030覆盖得好,容易出错,故首先尝试GB18030。
  for k in decode_list:#编码集循环
    try:
      book_list = open('1.txt', encoding=k)
      #打开路径中的文本
      line = book_list.readline()
      while line:
        if os.name == 'nt':
          line = line.strip('\r\n')
        else:
          line = line.strip('\n')
        doc_rename(line)
        line = book_list.readline()
      break#打开路径成功跳出编码匹配
    except:
      if k == "Error":#如果碰到这个程序终止运行
        print("had no way to decode")
        raise Exception("%s had no way to decode"%directions)
      continue
#重命名,构造完整的路径和后缀
def doc_rename(book_list_line):
  try:
    name_list = book_list_line.split(' ')
    list_name = name_list[0]
    cool_list = list_name.split('.')
    ext = cool_list[-1]
    current_folder = os.getcwd()
    real_name = name_list[1]
    real_name = os.path.join(current_folder, real_name)
    real_name_ext = real_name+'.'+ext
    os.rename(os.path.join(current_folder, list_name), real_name_ext)
    print('success')
  except:
    pass
  return 0
#据说高手都会写的主函数
if __name__ == '__main__':
  get_list()
Copy after login

Requires the file to be placed in In an ordinary folder, the file suffix can be arbitrary (the suffix must be the same as the list suffix in the txt). The file list is placed under 1.txt. The txt is saved in the default ANSI format under Microsoft or UTF without BOM format. The internal content format is In the following format:


ts001003.pdf One hundred volumes of the world's science and technology panorama (3) Modern science and technology

ts001004.pdf One hundred volumes of the world's science and technology panorama (4) )The revolution brought by the steam engine

ts001005.pdf One Hundred Volumes of World Science and Technology Panorama (5) Modern Technology


1.txt document requires one line per document, and must be saved as There is no BOM format in ANSI or UTF. Other formats are fine to test.

is preceded by a list of document names including suffixes (that is, file names on the website, a string of numbers # or letters), a blank space, and then the document. Real name (without suffix)


For more articles related to the implementation of batch renaming files in bat and python, please pay attention to the PHP Chinese website!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template