简体   繁体   中英

How to use a var in a folder name

I'm creating a powershell script to create a folder for projects.

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.

$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.

After that you have both $path that is your old name and $newPath which is your result name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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