当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell 重命名文件夹 | PowerShell 重命名文件夹的示例

作者:精品下载站 日期:2024-12-14 04:49:17 浏览:16 分类:玩电脑

PowerShell 重命名文件夹 | PowerShell 重命名文件夹的示例


[玩转系统] PowerShell 重命名文件夹 | PowerShell 重命名文件夹的示例

PowerShell 重命名文件夹简介

在 PowerShell 中,可以借助 Rename-Item Cmdlet 来重命名项目(例如文件夹或文件名)。它仅更改项目,而不更改项目的内容。这与移动项目不同,不能用于移动项目。正在重命名的文件夹/项目必须存在,否则 cmdlet 将引发错误。这也可用于同时重命名多个项目/文件夹。本文将详细介绍与 Rename-Item cmdlet 相关的各种参数、它们的用途以及重命名在 PowerShell 中的工作原理。

语法和参数

以下是 Rename-Item Cmdlet 的语法:

NAME
Rename-Item
SYNTAX
Rename-Item [-Path] <string> [-NewName] <string> [-Force] [-PassThru] [-Credential <pscredential>] [-WhatIf] [-Confirm] [-UseTransaction]  [<CommonParameters>]
Rename-Item [-NewName] <string> -LiteralPath<string> [-Force] [-PassThru] [-Credential <pscredential>] [-WhatIf] [-Confirm] [-UseTransaction]  [<CommonParameters>]
ALIASES
rni
ren

示例:

Rename-Item -Path "c:\test\test1.txt" -NewName "test2.txt"
The above cmdlet will rename the file test1.txt to test2.txt without altering the content
Rename-Item -Path "c:\test\" -NewName "test2"
The above cmdlet will rename the folder test to test2

参数:

  • 确认:这用于在执行 cmdlet 之前获得用户的确认。这是一个可选参数。默认值为 false,类型为 switch 参数。该参数的别名是 cf。默认值为 false。它不接受管道字符作为输入,也不接受通配符。
  • 凭据:这用于模拟目的或提升正在运行 cmdlet 的帐户。要使用此功能,必须使用调用命令,并且 PowerShell 中安装的任何提供程序均不支持该命令。它的类型是 PS 凭证。它的默认值是当前用户。它接受管道输入,但不接受通配符。
  • 强制:隐藏文件夹或项目或只读文件夹或只读项目无法重命名,除非在 rename-item cmdlet 中明确提及强制参数。常量变量和别名也不能重命名。即使使用强制参数,安全限制仍然保留。其类型为开关参数。它的默认值为 false。它不接受管道字符作为输入,也不接受通配符。
  • 文字路径:这表示或代表要重命名的一个或多个项目路径。任何作为文字值键入的值。所有字符均不被解释为通配符。如果路径包含转义字符,则必须将其括在单引号内,以通知 PowerShell 不要将其解释为转义序列。这是一个强制参数。
  • -NewName:这表示必须将项目重命名为的项目的新名称。只需输入名称,而不是路径。该 cmdlet 将了解是否必须重命名路径或文件。不接受外卡。当要重命名多个项目时,必须使用替换运算符。它的类型是字符串。默认值为无。它接受管道输入,但不接受通配符。
  • PassThru:这不会生成任何输入。它用于返回要成为管道的项目。这是一个可选参数。其类型为开关参数。它的默认值为 false。它不接受管道字符作为输入,也不接受通配符。
  • 路径:这是必填参数。它表示要重命名的项目的路径。它的类型是字符串。它的默认值是无。接受管道输入,但不考虑通配符。
  • WhatIf:这不会产生任何输出。它仅显示运行 cmdlet 时会发生什么。其类型为开关参数。它的别名是wi。它的默认值为 false。它不接受管道字符作为输入,也不接受通配符。

rename-item cmdlet 仅支持公开的提供程序。要查找会话中可用提供程序的列表,请运行以下 cmdlet。

输入:

Get-PsProvider

输出:

[玩转系统] PowerShell 重命名文件夹 | PowerShell 重命名文件夹的示例

当前 PowerShell 会话中的提供程序列表

Name Capabilities Drives Registry ShouldProcess, Transactions {HKLM, HKCU} Alias ShouldProcess {Alias} Environment ShouldProcess {Env} FileSystem Filter, ShouldProcess, Credentials {C} Function ShouldProcess {Function} Variable ShouldProcess {Variable}

PowerShell 重命名文件夹的示例

下面给出示例:

例子#1

输入:

Write-Host "Welcome to the demo of renaming folder"
write-host "Renaming Test1 to test9 in the below example"
Write-Host "List of Folders before renaming"
Get-ChildItem -Path C:\Vignesh\Test
Rename-Item -Path C:\Vignesh\Test\Test1 -NewName Test9
Write-Host "Renaming completed successfully"
Write-Host "List of folders after renaming"
Get-ChildItem -Path C:\Vignesh\Test
Write-Host "Renaming a file"
Write-Host "File name before renaming"
Get-ChildItem -Path C:\Vignesh\Test\Actual1239
Write-Host "After Renaming"
Rename-Item -Path C:\Vignesh\Test\Test9\test.txt -NewNameactual.txt
Get-ChildItem -Path C:\Vignesh\Test\Actual1239

输出:

[玩转系统] PowerShell 重命名文件夹 | PowerShell 重命名文件夹的示例

[玩转系统] PowerShell 重命名文件夹 | PowerShell 重命名文件夹的示例

例子#2

输入:

write-host "Welcome to the demo of bulk renaming of folders and files"
Write-Host "Renaming multiple folders at once"
Write-Host "before renaming the folders names are as below"
Get-ChildItem -Path C:\Vignesh\Test
Get-ChildItem C:\Vignesh\Test -Recurse | Rename-Item -NewName{ $_.name -replace 'Actual', 'Actual123'}
Write-Host "After renaming the folders"
Write-Host "Now the folders name are as below"
Get-ChildItem -Path C:\Vignesh\Test
Write-Host "Renaming txt files to doc"
Write-Host "Before Renaming the files are as below"
Get-ChildItem C:\Vignesh\Test\Actual1239
Write-Host "Renaming all txt files to log"
Get-ChildItem C:\Vignesh\Test\Actual1239 | Rename-Item -NewName{ $_.Name -replace '.txt','.log' }
Write-Host "After renaming"
Get-ChildItem C:\Vignesh\Test\Actual1239

[玩转系统] PowerShell 重命名文件夹 | PowerShell 重命名文件夹的示例

[玩转系统] PowerShell 重命名文件夹 | PowerShell 重命名文件夹的示例

例子#3

输入:

Write-Host "Moving folders within a directory to another directory"
Write-Host "Before moving the list of files in the source directory is"
Get-ChildItem  C:\Vignesh\Script
Write-Host "List of files in destination directory"
Get-ChildItem C:\Vignesh\Test
Write-Host "Moving the folders from script to test"
Get-ChildItem -Path C:\Vignesh\Script | Move-Item -Destination C:\Vignesh\Test
Write-Host "Folders moved successfully"
Write-Host "Folders now in source"
Get-ChildItem  C:\Vignesh\Script
Write-Host "Folders now in Destination"
Get-ChildItem  C:\Vignesh\Test
Write-Host "Renaming file name with todays date and time"
Write-Host "File names before renamin"
Get-ChildItem C:\Vignesh\Test\Actual1239
$curDateTime = Get-Date -Format yyyyMMdd-HHmmss
Get-ChildItemC:\Vignesh\Test\Actual1239  -Recurse |
Rename-Item -NewName {$_.Basename + '_' + $curDateTime + $_.Extension }
Write-Host "File names after renaming"
Get-ChildItem C:\Vignesh\Test\Actual1239

输出:

[玩转系统] PowerShell 重命名文件夹 | PowerShell 重命名文件夹的示例

[玩转系统] PowerShell 重命名文件夹 | PowerShell 重命名文件夹的示例

结论

因此,本文详细介绍了 Rename-Item cmdlet。它可用于重命名文件夹、文件或任何项目。本文还演示了有关如何重命名单个文件、单个文件夹、多个文件、多个文件夹以及如何在文件名后附加今天日期的适当示例脚本。要详细了解此 cmdlet,建议编写示例脚本并进行练习。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯