[玩转系统] PowerShell 重命名文件夹 | PowerShell 重命名文件夹的示例
作者:精品下载站 日期:2024-12-14 04:49:17 浏览:16 分类:玩电脑
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 会话中的提供程序列表
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
输出:
例子#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
例子#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
输出:
结论
因此,本文详细介绍了 Rename-Item cmdlet。它可用于重命名文件夹、文件或任何项目。本文还演示了有关如何重命名单个文件、单个文件夹、多个文件、多个文件夹以及如何在文件名后附加今天日期的适当示例脚本。要详细了解此 cmdlet,建议编写示例脚本并进行练习。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag