简体   繁体   English

使用通配符替换文件夹/文件

[英]Replacing folder/file using wildcard

I am new to programming and need a bit of a hint.我是编程新手,需要一些提示。

I would need to replace a folder/file in a certain location that has a date as a prefix , hence the name of the folder is dynamic like:我需要替换某个位置中以日期为前缀的文件夹/文件,因此文件夹的名称是动态的,例如:

C:\\20200825_Folder1 I d like to rename this folder in C:\\A_Folder1 C:\\20200825_Folder1 我想重命名这个文件夹在 C:\\A_Folder1

I tried the following:我尝试了以下方法:

get-item "C:\*Folder1 | Rename-Item -NewName {$_.Name -replace "*Folder1", "A_Folder1"}"

or或者

Rename-Item "C:\*Folder1" -NewName "A_Folder1"

As a solution I ve copied the "*Folder1", renaming it to "A_Folder1" and deleting "*Folder1".作为解决方案,我复制了“*Folder1”,将其重命名为“A_Folder1”并删除“*Folder1”。

However, I am not sure if that is the BestPractice to do, so I ve thought I asked a bit more experienced developers.但是,我不确定这是否是最佳实践,所以我想我问了一些更有经验的开发人员。

I do not expect code if you do not wish, but I would be thankful for any advice.如果您不希望,我不期望代码,但我会感谢您的任何建议。

If you don't want to make the new name of the folder depending on the old name of the folder you can simply provide the new name of the folder .... like this:如果您不想根据文件夹的旧名称创建文件夹的新名称,您可以简单地提供文件夹的新名称....像这样:

Get-Item -Path 'C:\*Folder1' | 
    Rename-Item -NewName 'A_Folder1'

BTW: Rename-Item does not allow wildcards for the -Path parameter.顺便说一句: Rename-Item不允许-Path参数使用通配符。

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

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