Hituptony
Hituptony

Reputation: 2860

How can I change this folder name set to a variable

Ok so in my declaration portion of my script i set this path:

$temp = "\\fhnsrv01\home\aborgetti\Documentation\Projects\CCMSextract\temp"

after i do some processing i want to change the name of the folder temp to

CCMSEXTmmddyy

I have this for the date

$a = get-date
$b = get-date.ToString('MMddyy')
$b

how can I change the folder to be something like

 $temp = \\path\CCMSextract$b

I was thinking rename-item but I need to actually change the name of the folder not just the variable. Might be a simple solution. Not sure. Help please!

I've tried

Rename-Item $temp = \\path\CCMSextract$b

I suppose I could make a directory item and then copy everything over but there's gotta be as simpler way!

Upvotes: 1

Views: 464

Answers (3)

Rahul
Rahul

Reputation: 77876

You can try this and it works

$temp.replace("temp","CCMSextract$b")

Tested as below

PS C:\> $temp = "\\fhnsrv01\home\aborgetti\Documentation\Projects\CCMSextract\temp"
PS C:\> echo $temp
\\fhnsrv01\home\aborgetti\Documentation\Projects\CCMSextract\temp

PS C:\> $b = get-date

PS C:\> $temp = $temp.replace("temp","CCMSextract$b")
PS C:\> echo $temp
\\fhnsrv01\home\aborgetti\Documentation\Projects\CCMSextract\CCMSextract05/20/2014 22:35:37

Upvotes: 1

Jason Hughes
Jason Hughes

Reputation: 778

So in this context $temp is just a string. Powershell doesn't know that its a folder. Try:

$newFolder = '\\path\CCMSextract' + $b
Rename-Item -path $temp -NewName $newFolder

Upvotes: 1

TheMadTechnician
TheMadTechnician

Reputation: 36297

You were close. The syntax is Rename-Item <old path> <new path> so do something like:

Rename-Item $temp "\\CCMSExtract$b"

Upvotes: 1

Related Questions