简体   繁体   English

使用Powershell向用户询问名称时批量重命名文件

[英]Batch renaming files while asking user for name using powershell

I'm trying to batch rename files using Powershell. 我正在尝试使用Powershell批量重命名文件。 What I basically need is to: 我基本上需要做的是:

$askUser = Read-Host 'What is the title'

$fpath = "path-to-files"
ForEach .mkv in $fpath{
    Rename-Item -NewName ($askUser + ".mkv")
}

If this makes any sense. 如果这有意义。

Agree with GigaRohan, 同意GigaRohan,

Look at the Following code to understand what you are missing, i've added comments 查看以下代码以了解您缺少的内容,我已添加了注释

$askUser = Read-Host 'What is the title' ## For Example: Hello
$Counter = 1 ## This is a digit to add because you can't rename all the files to the same filename

$fpath = Get-ChildItem "C:\Test" -Include '*.mkv' -Recurse ## First get all your mkv files (filter only mkv files)
    ForEach ($mkv in $fpath) ## Start the loop
    {
        Rename-Item $mkv.FullName ("$askUser" + "_$Counter" + ".mkv") 

        ## rename the source file name which is $mkv.FullName (c:\test\file.mkv) to: 
        ## (everything in the parentheses will be the new name start with $askuser 
        ## then add _1 the adds .mkv for example "hello_1.mkv")

        $Counter ++ 
        ## add 1 to the counter digit so the next file will be "hello_2.mkv"
    }

For future reference, you should put in effort to find the solution to your problem and only if you can't or don't understand something, etc., should you post a question on SO. 供将来参考,您应该尽力找到解决问题的方法,并且只有在您不了解或不了解某些内容时,才应该在SO上发布问题。

For iterating files of a certain type in a directory (or in all sub-directories) and renaming them using PowerShell, see http://blogs.technet.com/b/heyscriptingguy/archive/2013/11/22/use-powershell-to-rename-files-in-bulk.aspx 有关在目录(或所有子目录)中迭代某种类型的文件并使用PowerShell重命名它们的信息,请参阅http://blogs.technet.com/b/heyscriptingguy/archive/2013/11/22/use-powershell -to-重命名,文件正在bulk.aspx

In your code, you change the name of all the .mkv files to the same. 在您的代码中,将所有.mkv文件的名称更改为相同的名称。 You can use _<number> at the end where <number> is an integer that is incremented for each additional file you rename in the loop. 您可以在末尾使用_<number> ,其中<number>是整数,对于循环中重命名的每个其他文件,该整数都会递增。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM