简体   繁体   English

如何在文件夹名称中使用 var

[英]How to use a var in a folder name

I'm creating a powershell script to create a folder for projects.我正在创建一个powershell脚本来为项目创建一个文件夹。

The idea is that I first enter the name of the project, and then use that name in the project folder.这个想法是我首先输入项目的名称,然后在项目文件夹中使用该名称。 Could anyone please give me a hand on how to rename the "00000 - Project Name" in powershell with the variable entered.任何人都可以帮助我如何在输入变量的情况下重命名 powershell 中的“00000 - 项目名称”。

$PFname = Read-Host -Prompt 'Enter the name of the new project'
Rename-Item -Path "K:\Projects\00000 - Project Name" -NewName "K:\Projects\00000 - Project Name"

Hello you have to do something like this:您好,您必须执行以下操作:

$PFname = Read-Host -Prompt 'Enter the name of the new project'
$path = "K:\Projects\00000 - Project Name"
$newPath = $path -replace "00000 - Project Name$", $PFname
Rename-Item -Path $path -NewName $newPath

The key to complete this is the operator -replace to whom you have to pass two values, first the value you are looking for and second the value you want to replace.完成此操作的关键是运算符-replace您必须向其传递两个值,首先是您要查找的值,其次是您要替换的值。

After that you have both $path that is your old name and $newPath which is your result name.之后,您将拥有旧名称 $ $path和结果名称$newPath

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

相关问题 如何在Powershell中使用System IO获得不带限定符的文件夹名称 - How to use System IO in powershell to get the folder name without qualifiers 从文件夹中提取名称并使用它 powershell - Extract name from folder and use it powershell 如何获取文件夹名称中编号最大的文件夹名称? - How to get the folder name with highest number in folder name? 如果字符ASCII编码大于128,如何用字符“_”替换A文件夹中的所有文件NAME使用powershell - How to replace all files NAME in A folder with character "_" if the character ASCII encoding is greater than 128 use powershell 在Powershell中,如何通过将名称作为$ var提供来设置环境变量? - In Powershell, how do I set an environment variable by supplying the name as a $var? 如何根据名称和基名在文件夹之间移动文件 - How to move files from folder to folder based on name and Basename Powershell:如何在一行中输出文件夹名称,lastwritetime和文件夹大小? - Powershell: How to output folder name, lastwritetime and folder size in one line? 如何在文件夹名称中具有“ []”的PowerShell 2.0中的文件夹路径 - How to path to a folder in powershell 2.0 that has '[]' in the folder name 如何从文件夹名称中删除第一个单词? - How to remove first word from folder name? 如何获取 powershell 中最后修改的文件夹名称? - How to get last modified folder name in powershell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM