So i am trying to get-childItem of a specific path.
my command look like this
(Get-ChildItem -Path fes\.policy -Recurse -Directory -Exclude ".*")
and the fullpath is something like this:
path 'C:\Users\(name)\(folder)\(folder)\(folder)\(folder)\governance\fes\.policy
and it works fine when I stand in the \governance\ folder and run the command. But when i am some where else it just add the parameter \fes.policy and says that the path not exsist
Get-ChildItem: Cannot find path C:\Users\(name)\(folder)\(folder)\(folder)\(folder)\governance\src\Tests\fes\.policy' because it does not exist.
which makes sense, but it just dont know how I get it to go back in directories and then put the \fes.policy on the path.
The start of the path is not always going to be the same, so I cant just put the whole path in.
CodePudding user response:
You need to provide either the Fully-qualified path (the path starting from the drive root starting with C:
) or the relative path (the path from the current working directory). From what it sounds like, you are trying to use a relative path, which is useful when the folder structure is only partially guaranteed (e.g. FolderA
in the example below will always exist, but it's placement under C:
may differ from system to system).
..
is a reserved directory "name" meaning the parent folder, similar to how .
is used to represent the current folder. You will need to provide the relative path to the file using ..
if you are in a different branch of the directory structure than where fes/.policy
is located.
Since I don't know your full directory structure and only what you've provided let's take the following example:
- Current directory:
C:\Users\username\FolderA\FolderB\FolderC
- Target file:
C:\Users\username\FolderA\governance\src\Tests\fes\.policy
From FolderC
, you should be able to locate the file with Get-ChildItem
using the following path:
Note: If any of the path has spaces you will want to wrap them in single or double quotes (the latter if you want to expand variables within the path string), or else escape the spaces with
`
, though quoting is preferred.
Get-ChildItem ../../governance/src/Tests/fes/.policy
Since ..
represents the parent folder of the current directory, ../../
represents two parents up, and would put you in FolderA
. From there you know governance/
exists and you can craft the rest of the path as you need.