This article mainly introduces the implementation code for batch renaming files in bat and python. For more information, see the batch of files downloaded from a website, however, files are named after numerical strings (many libraries do this). now I have downloaded these files and listed them, you just can't rename the files one by one, so I found these scripts from the Internet.
I. use bat scripts (available in windows by default)
Open the script and write the code into the script. the script is a xx.batfile (with a suffix). many small files are saved as xx.bat.txt, because the txt file is hidden and cannot be used)
@echo offfor /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” ))
Files must be placed in the folder d:/folder, and the file suffix is. folder, and the file suffix is placed under 1.txt. Save the txt file as the default ANSI format under Microsoft. the internal content format is as follows:
Ts001003.pdf World Science and Technology Panorama hundred books (3) Modern Science and Technology
Ts001004.pdf world technology Panorama volume book (4) steam engine revolution
Ts001005.pdf World Science and Technology Panorama hundred books (5) modern technology
You can modify it as needed.
II. use python scripts (python 3.50 compilation software is required for windows systems, which is about 30 MB. python to 3.50 must be upgraded for linux systems)
This script is a script that I spent several hours writing (although I have learned programming for a long time, it is not easy to write smoothly)
Open the script and write the code into the script. the script is a xx.batfile (with a suffix). many small files are saved as xx.bat.txt, because the txt file is hidden and cannot be used)
#! /Bin/env python #-*-coding: UTF-8-*-"downloads a batch of documents from a website, however, a document is named by a number string (this is the case in many libraries), and a document list is also available, therefore, I wrote a script to rename the batch file "" _ author _ = 'rubog' to import a row of reading files in the batch text file and remove the line break, then call the doc_rename # function def get_list (): # try different encodings from zhihuhuhu.15## decode_list = ["UTF-8", 'gb18030', 'ISO-8859-2 ', 'gb2312', "gbk", "Error"] # Limit set # GBK is not as well covered as GB18030, It is prone to errors, so first try GB18030. For k in decode_list: # collect set loop try: book_list = open('1.txt ', encoding = k) # open the text 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 # The path is successfully opened and the matching encoding T: if k = "Error ": # If this program terminates running print ("had no way to decode") raise Exception ("% s had no way to decode" % directions) continue # Rename, construct the complete path and suffix 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') before t: pass return 0 # if _ name _ = '_ main _': get_list ()
To save a file to a common folder, you can set the file suffix to any (suffix and txt suffix). the file should be placed under 1.txt, and the txt file should be saved as the default ANSI format under Microsoft or UTF without BOM format. the internal content format is as follows:
Ts001003.pdf World Science and Technology Panorama hundred books (3) Modern Science and Technology
Ts001004.pdf world technology Panorama volume book (4) steam engine revolution
Ts001005.pdf World Science and Technology Panorama hundred books (5) modern technology
1.txt documents require one line of each document. they must be saved in ANSI or UTF without BOM format. other formats are okay,
The first is the list document name with a suffix (that is, the file name on the website, a string of numbers # or letters), an empty box, and then the document's real name (without a suffix)
For more information about how to batch rename bat and python files, see PHP!