删除时间戳,如果我有一个名为pippo_yyyymmdd.txt,pluto_yyyymmdd.txt等等等等,我怎么能删除时间戳和文件重命名为pippo.txt,pluto.txt一些文件?PowerShell与文件名
0
A
回答
2
试试这个,它会删除任何下划线后8位数字。这是THA basicmidea的文件最终会具有相同名称不照顾:
Get-ChildItem *.txt |
Where {$_.Name -match '_\d{8}\.txt' } |
Rename-Item -NewName {$_.Name -replace '_\d{8}'}
0
我面临同样的问题,但遗憾的是,PowerShell中并没有帮我解决这个问题。于是我采用Python来解决这个问题,它像炸弹一样工作。你只需要在python脚本复制到受影响的文件夹,然后运行该脚本从文件夹和子文件夹中的所有文件删除时间戳。
import os, sys, re
fileNameList =[]
patho = os.path.dirname(os.path.realpath(sys.argv[0]))
def renamer(fpath):
for path, subdirs, files in os.walk(fpath):
for name in files:
if re.findall(r"(?ix)(\.ini)", name, re.I) == None:
if re.search(r"(?ix)(\(\d{4}\_\d{2}_\d{2}\s\d{2}\_\d{2}\_\d{2}\sutc\))", name, re.I) != None:
old_name = os.path.join(path,name)
print(old_name)
new_name = re.sub(r"(?ix)(\s\(\d{4}\_\d{2}_\d{2}\s\d{2}\_\d{2}\_\d{2}\sutc\))", "", old_name, re.I)
print(new_name)
try:
os.replace(old_name,new_name)
except:
print(old_name)
fileNameList.append(old_name)
def log_errors_directories(fileNameList):
filename = "Log of error filenames.txt"
if len(fileNameList) != 0:
log = open(filename, "a")
log.write("###########################################################\n")
i = 1
for line in fileNameList:
nr_line = str(i) + " " + str(line)
log.write(str(nr_line))
log.write("\n")
i += 1
log.write("###########################################################\n\n")
log.close()
renamer(patho)
log_errors_directories(fileNameList)
这是我第一次在线发帖的代码。我希望它对你有用,就像它对我一样。 :)
相关问题
- 1. PowerShell文件名与日期比较
- 2. powershell Get-Location与短文件名
- 3. 在PowerShell中重命名文件与参考文件
- 4. PowerShell脚本 - 从文件名
- 5. PowerShell文件重命名
- 6. powershell - 提取文件名和扩展名
- 7. Powershell与变量的文件
- 8. 防止PowerShell与在文件
- 9. 移动名称与PowerShell文本文件中列出的目录
- 10. 使用文件夹名称在powershell中重命名文件
- 11. Powershell的重命名文件到原始文件名
- 12. PowerShell的更换与父文件夹名的文件名不改变文件结尾
- 13. 当文件内容与文件夹名称不匹配时运行powershell脚本
- 14. 删除PowerShell中的文件名字符
- 15. Powershell误解文件名中的引号
- 16. PowerShell命令批量重命名文件
- 17. Powershell:用于文件名更改的FileSystemWatcher
- 18. PowerShell:查找确切的文件夹名
- 19. Powershell out-file并命名文件
- 20. PowerShell 3的文件扩展名
- 21. Powershell从文件传递命名参数
- 22. Powershell文件扩展名匹配*
- 23. Powershell - 帮助。搜索和名称文件
- 24. 替换部分文件名Powershell
- 25. PowerShell替换文件夹名称
- 26. 重命名和移动文件Powershell
- 27. 在PowerShell中递归重命名文件
- 28. 使用PowerShell重命名文件
- 29. PowerShell可以扩展文件名吗?
- 30. PowerShell的提前整理文件名