I'm trying to make a script where I copy a file from some place and store it in another one. I'm using foreach recursively to get all the files.
The question is: How can I get a path segment and replace it with another string? I want to do something like this:
Copy files from
C:\sites\somefolder\folder1\file.php
to
C:\sites\anotherfolder\folder1\file.php
In this example, I need to replace 'somefolder' with 'anotherfolder'. This is how I'm getting the full path:
foreach ($i in Get-ChildItem -Recurse -Force) {
Write-host $i.Fullname
}
Inside the loop, I tried to replace it using Fullname.replace, but things didn't work as expected:
$i.Fullname.Replace($PWD, "anotherfolder")
# This returns "anotherfolder/folder1/file.php",
# instead of "c:\sites\anotherfolder\folder1\file.php
I don't know much of Powershell programming, so bear with me, ok?
Thx!