0

I have a bunch of mp3's that all have a long title, followed by "part 3" or "part 4", etc. So it's like "aasasdfjklakjsdf - part 3" and "aasasdfjklakjsdf - part 4" and "aasasdfjklakjsdf - part 5" and so on.

I have over 100 of these and I would like to simply remove the "aasasdfjklakjsdf - " part from the title of each one. I have a CS degree so I know the basics of programming, but I've never actually written a script of any kind and would love to get started. I'm wondering what the fastest and easiest way to go about doing something like this is. I'm running windows 7. I'm guessing something with python maybe? Or is there something I could do from the windows command prompt?

4

3 回答 3

2

如果您打算使用 Python,我认为这是一个不错的选择,您将使用glob模块来获取所有文件名并使用os模块来重命名文件。你会想弄清楚你正在寻找什么分隔符。在 Python 中,字符串有一个split方法。对于每个文件名,在分隔符处拆分文件名,并使用拆分中的第二个字段重命名文件。

这是对您需要做什么的非常广泛的描述。祝你好运!

于 2013-10-23T19:56:00.177 回答
1

这是我的建议,先安装 python 然后再安装ipython

现在进入 ipython 并在交互式解释器中尝试如下所示:

import os
cd C:/my_dir/
trimstr = 'aasasdfjklakjsdf - '
for x in os.listdir('.'):
  if x.startswith(trimstr):
    oldname = x
    newname = x[len(trimstr):]
    print oldname, ' -> ', newname 
    if os.path.exists(newname):
      print 'skipping', x, 'because the new name would stomp an existing file'
    else:
      pass
      #os.rename(oldname, newname)
  else:
    print 'ignoring', x

如果您懒惰,可以复制我的代码块并将其粘贴到 ipython 中,但将 cd 行更改'C:/my_dir/'os.chdir(...)替换您要处理的实际目录。

在这个阶段,它只会聊天到标准输出将要进行的更改。所以运行它,如果一切看起来都不错,请继续并取消注释#commented line并真正做到这一点。您可以使用向上箭头重新运行您刚刚输入 ipython 的内容。

于 2013-10-23T19:58:35.453 回答
0

您无需为此操作编写脚本,只需使用 powershell(Windows 内置)即可。

Powershell 有一个 Rename-Item 命令行开关 ( Microsoft Docs ),您可以使用以下方式:

Dir | Rename-Item –NewName {$_.name –replace "_What_You_Want_Replaced_","_Replacement_"}

例如

>powershell
Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.

PS cd "your mp3 folder"
PS Dir | Rename-Item –NewName {$_.name –replace "aasasdfjklakjsdf -",""}

做完了。就这么容易。

于 2013-10-24T02:26:21.013 回答