通过检查文件名中的字符串来重命名文件,这些字符串将被相应的新字符串替换
Renaming files by checking for strings within the filename that will be replaced with a corresponding new string
我想通过用特定的新字符串替换某些字符串的所有实例来重命名很多文件,但是有大量新旧对的列表。让我举个例子来帮助解释...
**Original List**
abc_mno_pqr.txt
def_mno_lmn.txt
xyz_mno_efg.txt
xyz_tuv_pqr.txt
xyz_stu_bcd.txt
fgh_efg_klm.txt
Replace instances of: with:
mno 345
xyz 123
efg 567
**Resulting List**
abc_345_pqr.txt
def_345_lmn.txt
123_345_567.txt
123_tuv_pqr.txt
123_stu_bcd.txt
fgh_567_klm.txt
Here's the same text but with colorful highlighting to help.
我对 .bat 文件或 python 甚至 excel 没有偏好。无论你能想到什么工具来实现它,我都会试一试!
更新:我用提供的解决方案做了什么
我最终在 javascript 中写了它,因为我比 python 更熟悉它。我所拥有的不是一个一体化的解决方案,但它解决了繁重的工作。感谢 ettanany,他的解决方案有助于将 steps/logic 组合在一起以完成这项工作。我正在使用的实际文件名在命名结构上与我列出的几乎不一致,但 ettanany 的回答肯定适用于我提供的内容。
var mapping = {'mno': '345','xyz': '123','efg': '567'},result=[],newName=[],
files = ['abc_mno_pqr.txt', 'def_mno_lmn.txt', 'xyz_mno_efg.txt',
'xyz_tuv_pqr.txt', 'xyz_stu_bcd.txt', 'fgh_efg_klm.txt'];
for (var item = 0; item < files.length; item++) { // loop thru files
var thisFile = files[item].split('_'); // split file into segments
newName = []; // clear out newName array
for (var i = 0; i < thisFile.length; i++) { // loop thru segments
for (var key in mapping) { // loop thru mapping object
if (key == thisFile[i]) { // if key and segment match
var segment = mapping[key];
break;
} else {
var segment = thisFile[i];
}
}
newName.push(segment); // add segment to array
}
result.push(newName.join("_")); // add new filename to results
/* With how my files actually are named,
I knew I did not need to worry about
removing/appending the file extension. */
}
使用 Python,您可以将旧值和新值存储在字典中,并在使用带有 split()
函数的列表理解时使用它们,如下所示:
d = {'mno': '345', 'xyz': '123', 'efg': '567'}
files = ['abc_mno_pqr.txt', 'def_mno_lmn.txt', 'xyz_mno_efg.txt', 'fgh_efg_klm.txt']
res = ['_'.join([item.split('_')[0], d[item.split('_')[1]], item.split('_')[2]]) for item in files]
print(res)
# Output: ['abc_345_pqr.txt', 'def_345_lmn.txt', 'xyz_345_efg.txt', 'fgh_567_klm.txt']
编辑:
我注意到您的文件名的第一部分和最后一部分可能也需要更改(不仅是第二部分),更通用的解决方案如下:
res = []
for item in files:
new_item = []
for i in item[:-4].split('_'):
if i in d:
new_item.append(d[i])
else:
new_item.append(i)
res.append('_'.join(new_item) + '.txt')
输出示例:
>>> d = {'mno': '345', 'xyz': '123', 'efg': '567'}
>>> files = ['def_mno_lmn.txt', 'xyz_mno_efg.txt', 'xyz_tuv_pqr.txt']
>>>
>>> res = []
>>> for item in files:
... new_item = []
... for i in item[:-4].split('_'):
... if i in d:
... new_item.append(d[i])
... else:
... new_item.append(i)
... res.append('_'.join(new_item) + '.txt')
...
>>>
>>> files
['def_mno_lmn.txt', 'xyz_mno_efg.txt', 'xyz_tuv_pqr.txt']
>>> res
['def_345_lmn.txt', '123_345_567.txt', '123_tuv_pqr.txt']
我想通过用特定的新字符串替换某些字符串的所有实例来重命名很多文件,但是有大量新旧对的列表。让我举个例子来帮助解释...
**Original List**
abc_mno_pqr.txt
def_mno_lmn.txt
xyz_mno_efg.txt
xyz_tuv_pqr.txt
xyz_stu_bcd.txt
fgh_efg_klm.txt
Replace instances of: with:
mno 345
xyz 123
efg 567
**Resulting List**
abc_345_pqr.txt
def_345_lmn.txt
123_345_567.txt
123_tuv_pqr.txt
123_stu_bcd.txt
fgh_567_klm.txt
Here's the same text but with colorful highlighting to help.
我对 .bat 文件或 python 甚至 excel 没有偏好。无论你能想到什么工具来实现它,我都会试一试!
更新:我用提供的解决方案做了什么
我最终在 javascript 中写了它,因为我比 python 更熟悉它。我所拥有的不是一个一体化的解决方案,但它解决了繁重的工作。感谢 ettanany,他的解决方案有助于将 steps/logic 组合在一起以完成这项工作。我正在使用的实际文件名在命名结构上与我列出的几乎不一致,但 ettanany 的回答肯定适用于我提供的内容。
var mapping = {'mno': '345','xyz': '123','efg': '567'},result=[],newName=[],
files = ['abc_mno_pqr.txt', 'def_mno_lmn.txt', 'xyz_mno_efg.txt',
'xyz_tuv_pqr.txt', 'xyz_stu_bcd.txt', 'fgh_efg_klm.txt'];
for (var item = 0; item < files.length; item++) { // loop thru files
var thisFile = files[item].split('_'); // split file into segments
newName = []; // clear out newName array
for (var i = 0; i < thisFile.length; i++) { // loop thru segments
for (var key in mapping) { // loop thru mapping object
if (key == thisFile[i]) { // if key and segment match
var segment = mapping[key];
break;
} else {
var segment = thisFile[i];
}
}
newName.push(segment); // add segment to array
}
result.push(newName.join("_")); // add new filename to results
/* With how my files actually are named,
I knew I did not need to worry about
removing/appending the file extension. */
}
使用 Python,您可以将旧值和新值存储在字典中,并在使用带有 split()
函数的列表理解时使用它们,如下所示:
d = {'mno': '345', 'xyz': '123', 'efg': '567'}
files = ['abc_mno_pqr.txt', 'def_mno_lmn.txt', 'xyz_mno_efg.txt', 'fgh_efg_klm.txt']
res = ['_'.join([item.split('_')[0], d[item.split('_')[1]], item.split('_')[2]]) for item in files]
print(res)
# Output: ['abc_345_pqr.txt', 'def_345_lmn.txt', 'xyz_345_efg.txt', 'fgh_567_klm.txt']
编辑:
我注意到您的文件名的第一部分和最后一部分可能也需要更改(不仅是第二部分),更通用的解决方案如下:
res = []
for item in files:
new_item = []
for i in item[:-4].split('_'):
if i in d:
new_item.append(d[i])
else:
new_item.append(i)
res.append('_'.join(new_item) + '.txt')
输出示例:
>>> d = {'mno': '345', 'xyz': '123', 'efg': '567'}
>>> files = ['def_mno_lmn.txt', 'xyz_mno_efg.txt', 'xyz_tuv_pqr.txt']
>>>
>>> res = []
>>> for item in files:
... new_item = []
... for i in item[:-4].split('_'):
... if i in d:
... new_item.append(d[i])
... else:
... new_item.append(i)
... res.append('_'.join(new_item) + '.txt')
...
>>>
>>> files
['def_mno_lmn.txt', 'xyz_mno_efg.txt', 'xyz_tuv_pqr.txt']
>>> res
['def_345_lmn.txt', '123_345_567.txt', '123_tuv_pqr.txt']