如何删除文件夹名称和文件名称的“最终空格”?

我有600GB的Mac用户数据,保存在格式化为HFS +的磁盘中。 很多文件夹和文件名都包含“最终空格”。 我不知道用户如何插入它们,但结果是通过Samba,一个名为“Customer ABC”的文件夹(最后一个空格)成为例如“EHFJ〜1”。 另外,用户使用特殊字符如•和其他奇怪的文件名。

如何大量重命名文件/文件夹删除最后的空格? 是否有可能使用Linux / Mac OS脚本?

谢谢

我已经写了一个小python脚本来删除所有字符的文件名,使* nix难以处理它们。

也许这可以帮助你。

#! /usr/bin/python # -*- coding: UTF-8 -*- """ usage: fixFileNames.py FILE... Renames FILEs to sensible names, avoiding collision. """ import sys import os from string import maketrans def fixFileName(file): ''' move file to filename: - without spaces, pipe characters, quotes ''' intab = ' |' outtab = '__' trantab = maketrans(intab, outtab) newFileName = file.translate(trantab, '\'\"').replace('_-_', '-') if file != newFileName: #only renames file if it's name contains any unwanted characters if os.path.exists(newFileName): print "ERROR: Not renaming %s, %s exists already" % (file, newFileName) else: print "renaming %s to %s" % (file, newFileName) os.rename(file, newFileName) # else: # print "file %s and newFilename %s are equal" % (file, newFileName) if __name__ == "__main__": if not len(sys.argv) > 1: print __doc__ sys.exit(1) for file in sys.argv[1:]: fixFileName(file) 

任何人都可以自由使用或改善这一点。 如果您有任何改进,我想听听他们的消息。

是的! 我并不确切地知道如何,但是由于没有人回答这个问题,我认为总比没有好。 使用一些正则expression式,匹配最终的空白,应该是可能的。 我不知道如何获得文件名的截断部分