Konzertmeister
Konzertmeister

Reputation: 41

Powershell navigating to unknown directory

I have stumbled the unfortunate situation, having to be in a directory in which another directory is located:

C:\Test\[Folder with unknown name]\theFileINeed.txt

The structure mentioned above originates from a Zip-file from an external source. So i can not change the structure.

My goal is to navigate to the Directory with the unknown name, so it is my working directroy and I can execute further commands there. (Like Get-Childitem e.g.)

Is there a simple way to e.g. use the cd command to move into that directory?

I have fiddled around a bit with Resolve-Path but couldn't find a helpful solution.

Thanks in advance.

Upvotes: 1

Views: 1267

Answers (1)

AdamL
AdamL

Reputation: 13191

Consider this structure:

C:\TMP\TEST
├───unknowndir1
│   │   nonuniquefile.txt
│   │   uniquefile.txt
│   │
│   ├───nonuniquesubdir
│   └───uniquesubdir
└───unknowndir2
    │   nonuniquefile.txt
    │
    └───nonuniquesubdir

You could do cd .\test\*\uniquesubdir but you can't cd .\test\*\nonuniquesubdir as you'll gen an error (...) path (...) resolved to multiple containers. The same error is even with cd .\test\*\uniquesubdir\.. as if it didn't even check for existence of uniquesubdir.

So if you want to enter unknown directory based of a file it contains, you'd have to do something like this: cd (Get-Item .\test\*\uniquefile.txt).DirectoryName. It will fail if you use nonuniquefile.txt as it again resolves to two different directories. You could enter the first of these directories with cd (Get-Item .\test\*\nonuniquefile.txt).DirectoryName[0] if you don't care which of them you use.

Upvotes: 2

Related Questions