How can I remove the "base" part of the path?


Denali hardtail

I need to recreate a series of folders in a source directory in another environment. The source folder looks like this:

C:\temp\stuff\folder01\
C:\temp\stuff\folder02\
C:\temp\stuff\folder03\
C:\temp\stuff\folder02\dog
C:\temp\stuff\folder02\cat

I need to remove the base part of the path C:\temp\stuff- and get the rest of the path to join with the target base folder in order to create the folder structure elsewhere.

I have the following script. The problem seems to be with the variable $DIR. $DIRThe expected "current path minus base path" is never assigned.

The variable was $tmpassigned a name close to the expected folder name. It contains the folder name minus vowels, is split on multiple lines, and contains a bunch of leading spaces. For example, a folder named folder01as follows:

f
ld
r01

Here is the PowerShell script:

$SOURCES_PATH = "C:\temp\stuff"
$DESTINATION_PATH = "/output001"

Get-ChildItem "$SOURCES_PATH" -Recurse -Directory |
Foreach-Object {

    $tmp = $_.FullName.split("$SOURCES_PATH/")

    $DIR = $_.FullName.split("$SOURCES_PATH/")[1]

    $destination = "$DESTINATION_PATH/$DIR"

    *** code omitted ***
}

I suspect $DIRit seems to be unassigned because the [1] element is a space, but I don't know why there is a space and what happened to the folder name.

What's going on and how can I fix it?

Mathias R. Jessen

String.Split("somestring")will be split on any character in "somestring", which is why you see the path being split into more than expected.

I recommend -replace '^base'removing the beginning part of the path using:

$SOURCES_PATH = "C:\temp\stuff"
$DESTINATION_PATH = "/output001"

Get-ChildItem "$SOURCES_PATH" -Recurse -Directory |Foreach-Object {

    # This turns "C:\temp\stuff\folder01" into "\folder01"
    $relativePath = $_.FullName -replace "^$([regex]::Escape($SOURCES_PATH))"

    # This turns "\folder01" into "/folder01"
    $relativeWithForwardSlash = $relativePath.Replace('\','/')

    # Concatenate destination root and relative path
    $rootedByDestination = "${DESTINATION_DIR}${relativeWithForwardSlash}"

    # Create/Copy $rootedByDestination here
}

-replaceis a regex operator, that's why I run [regex]::Escape()against the input path to escape the escape character twice :)

Related


How can I remove the "base" part of the path?

Denali hardtail I need to recreate a series of folders in a source directory in another environment. The source folder looks like this: C:\temp\stuff\folder01\ C:\temp\stuff\folder02\ C:\temp\stuff\folder03\ C:\temp\stuff\folder02\dog C:\temp\stuff\folder02\ca

How can I remove the "base" part of the path?

Denali hardtail I need to recreate a series of folders in a source directory in another environment. The source folder looks like this: C:\temp\stuff\folder01\ C:\temp\stuff\folder02\ C:\temp\stuff\folder03\ C:\temp\stuff\folder02\dog C:\temp\stuff\folder02\ca

How can I remove the "base" part of the path?

Denali hardtail I need to recreate a series of folders in a source directory in another environment. The source folder looks like this: C:\temp\stuff\folder01\ C:\temp\stuff\folder02\ C:\temp\stuff\folder03\ C:\temp\stuff\folder02\dog C:\temp\stuff\folder02\ca

How can I remove the "base" part of the path?

Denali hardtail I need to recreate a series of folders in a source directory in another environment. The source folder looks like this: C:\temp\stuff\folder01\ C:\temp\stuff\folder02\ C:\temp\stuff\folder03\ C:\temp\stuff\folder02\dog C:\temp\stuff\folder02\ca

How can I remove the first part of the path?

YoavSmall Suppose I have the path fodler1/folder2/folder3, but I don't know the name of the folder in advance. How can I remove the first part of this path to get only folder2/folder3? User 2390182 str.splitUsed with 1as maxsplitparameter : _ path = "folder1/f

How can I remove a specific part of an image?

Akash Tripuramallu) I have an image: original image I want to remove the grey grid part of the image without affecting the rest of the image i.e. the part inside the black circle. I have written a code for this import cv2 import numpy as np from PIL import Ima

How can I remove the noisy part of the heartbeat?

Juliet I have a large pulse oximeter signal. Part of it is noisy and destroys my data if I use it. Do you have any strategy to automatically remove noisy parts? (I can't really do it manually since the data is long and there are many channels). Please find the

How can I remove the first part of the for loop?

Caleb Libby For example, I have a variable already defined that I want to use as a counter in a for loop. I found that it is possible to replace the first expression with null, but I am wondering if it is possible to remove it completely using a different vers

How can I remove the last part of the url?

范 Taro I have this url:https://uk.soccerway.com/national/italy/serie-a/20172018/regular-season/r42011/?ICID=TN_02_01_02 I want to remove ?ICID=TN_02_01_02because this string is dynamic so I can't use .Replace()method, any ideas? Bryce Meister Using Uri try to

How can I remove a specific part of an image?

Akash Tripuramallu) I have an image: original image I want to remove the grey grid part of the image without affecting the rest of the image i.e. the part inside the black circle. I have written a code for this import cv2 import numpy as np from PIL import Ima

How can I remove the first part of the for loop?

Caleb Libby For example, I have a variable already defined that I want to use as a counter in a for loop. I found that it is possible to replace the first expression with null, but I am wondering if it is possible to remove it completely using a different vers

How can I remove the first part of the for loop?

Caleb Libby For example, I have a variable already defined that I want to use as a counter in a for loop. I found that it is possible to replace the first expression with null, but I am wondering if it is possible to remove it completely using a different vers

How can I remove the first part of the for loop?

Caleb Libby For example, I have a variable already defined that I want to use as a counter in a for loop. I found that it is possible to replace the first expression with null, but I am wondering if it is possible to remove it completely using a different vers

How can I remove the datetime part of a string?

username For example, I have a string "do something before 9am tomorrow morning". I am using parsedatetime module to parse string and get datetime object from it. I did this according to the parsedatetime documentation: from datetime import datetime import par

How can I remove the noisy part of the heartbeat?

Juliet I have a large pulse oximeter signal. Part of it is noisy and destroys my data if I use it. Do you have any strategy to automatically remove noisy parts? (I can't really do it manually since the data is long and there are many channels). Please find the

How can I remove the noisy part of the heartbeat?

Juliet I have a large pulse oximeter signal. Part of it is noisy and destroys my data if I use it. Do you have any strategy to automatically remove noisy parts? (I can't really do it manually since the data is long and there are many channels). Please find the

How can I remove the noisy part of the heartbeat?

Juliet I have a large pulse oximeter signal. Part of it is noisy and destroys my data if I use it. Do you have any strategy to automatically remove noisy parts? (I can't really do it manually since the data is long and there are many channels). Please find the

How can I remove the first part of a string?

Bill_Johnson77 I have an array containing strings of the following type: var Nameslist = []; Nameslist.push("home.mytest1.James Thomas 14-47"); Nameslist.push("home.mytest1.George Simon 7-2"); Nameslist.push("home.mytest1.Sandy Kylie 3-15"); Now I want to re

How can I remove a specific part of an image?

Akash Tripuramallu) I have an image: original image I want to remove the grey grid part of the image without affecting the rest of the image i.e. the part inside the black circle. I have written a code for this import cv2 import numpy as np from PIL import Ima

How can I remove the first part of the for loop?

Caleb Libby For example, I have a variable already defined that I want to use as a counter in a for loop. I found that it is possible to replace the first expression with null, but I am wondering if it is possible to remove it completely using a different vers

How can I remove the first part of the for loop?

Caleb Libby For example, I have a variable already defined that I want to use as a counter in a for loop. I found that it is possible to replace the first expression with null, but I am wondering if it is possible to remove it completely using a different vers

How can I remove the last part of the url?

范 Taro I have this url:https://uk.soccerway.com/national/italy/serie-a/20172018/regular-season/r42011/?ICID=TN_02_01_02 I want to remove ?ICID=TN_02_01_02because this string is dynamic so I can't use .Replace()method, any ideas? Bryce Meister Using Uri try to

How can I remove the first part of the for loop?

Caleb Libby For example, I have a variable already defined that I want to use as a counter in a for loop. I found that it is possible to replace the first expression with null, but I am wondering if it is possible to remove it completely using a different vers

How can I remove the noisy part of the heartbeat?

Juliet I have a large pulse oximeter signal. Part of it is noisy and destroys my data if I use it. Do you have any strategy to automatically remove noisy parts? (I can't really do it manually since the data is long and there are many channels). Please find the

How can I remove the noisy part of the heartbeat?

Juliet I have a large pulse oximeter signal. Part of it is noisy and destroys my data if I use it. Do you have any strategy to automatically remove noisy parts? (I can't really do it manually since the data is long and there are many channels). Please find the

How can I remove the noisy part of the heartbeat?

Juliet I have a large pulse oximeter signal. Part of it is noisy and destroys my data if I use it. Do you have any strategy to automatically remove noisy parts? (I can't really do it manually since the data is long and there are many channels). Please find the

How can I find a path by only providing a part of the path?

hax0r_n_code Suppose I need to find this path /<some_where_in_root>/find/this/pathand the only information I have is /find/this/path. What's the best way to find the full catalog? I basically have a program that creates a directory, and after the directory is

How to remove part of path in terminal?

User 395980 Suppose I type cd Documents/dir1/ I just want to remove dir1from that input so that I get cd Documents/ Already asked about this in the remove part of the path but couldn't find what I was looking for. Arkadiusz Drabczyk Press Esc BackspaceDelet

How can I remove the part of the string I specify

cover How should I delete everything on the string that starts with the letter I specify? Let's say: PHP/USD I want to remove everything starting with "/", so all that's left is "PHP" How can I do the opposite? Start so dollars will be the only thing left? I