1
votes

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.

1
Please edit the question and explain with more details what you are trying to achieve. How is the name of unknown directory created in the first place?vonPryz
Maybe dir C:\Test\*\theFileINeed.txt | ForEach-Object { $_ | Split-Path -Parent } or dir C:\Test\*\theFileINeed.txt | Split-Path -ParentJosefZ

1 Answers

1
votes

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.