你也没说对语言的要求?命令是什么命令?bash?dos?
我做过一个类似的,python 的,感觉和你这个有点像,你可以看看。
指定后缀名说白了不过是一个正则而已:/.*[.]txt$/ 就指定了 txt 文件的后缀了。
ps:这个正则是 js 的正则。
![](https://video.ask-data.xyz/img.php?b=https://iknow-pic.cdn.bcebos.com/359b033b5bb5c9ea4bc9dadbd839b6003af3b3d4?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto)
代码 gist,自行翻墙
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
import os
import random
import shutil
os.mkdir('.%s%s' % (os.sep, 'top'))
os.chdir('.%s%s' % (os.sep, 'top'))
print('你现在处于:%s' % os.path.abspath(os.curdir))
# 创建 100 个随机文件夹,并放入 a b c d e
for i in range(0, 100):
randDirName = str(random.randint(0, 99999))
os.mkdir(r'%s' % (randDirName))
for fileName in ['a', 'b', 'c', 'd', 'e']:
fd = open(r'%s%s%s' % (randDirName, os.sep, fileName), 'w+')
fd.close()
# 再额外建立 5 个新文件夹 A B C D E
for fileName in ['a', 'b', 'c', 'd', 'e']:
os.mkdir(fileName)
# 现在根据名字提取那 100 随机命名文件夹中指定姓名的文件
_100RandDirName = []
for dirName in os.listdir():
if dirName not in ['A', 'B', 'C', 'D', 'E']:
_100RandDirName.append(dirName)
print(len(_100RandDirName))
# os.sep 跨平台用
# 将对应名字的文件存入对应的名字的文件夹中
for someOneDir in _100RandDirName:
listFileNames = os.listdir(someOneDir)
for fileName in ['a', 'b', 'c', 'd', 'e']:
if fileName in listFileNames:
src = '%s%s%s' % (someOneDir, os.sep, fileName)
target = '%s' % (fileName.upper())
shutil.copy(src, target)
oldName = '%s%s%s' % (target, os.sep, fileName)
newName = '%s%s%s_%s' % (target, os.sep, someOneDir, fileName)
os.rename(oldName, newName)