relative path

I have searched over the internet about relative path and it is verry little info about it.

What I need is to write path to file in txt file if exist

for example I need to search for file loging_*_2020.txt in folder \latest

so current folder is D:\files and powershell should search under it in folder \latest

and relative path is

D:\files\john\year\latest\loging_11_2020.txt
D:\files\john\year\old\loging_11_2020.txt
D:\files\tim\year\latest\loging_09_2020.txt
D:\files\tim\year\old\loging_09_2020.txt
D:\files\janice\year\latest
D:\files\janice\year\old
D:\files\vera\year\latest\loging_12_2020.txt
D:\files\vera\year\old\loging_12_2020.txt
D:\files\mike\year\latest
D:\files\mike\year\old\

so what I need is a result in file path.txt

D:\files\john\year\latest\loging_11_2020.txt
D:\files\tim\year\latest\loging_09_2020.txt
D:\files\vera\year\latest\loging_12_2020.txt

Can someone help me with this please?

If I got you right you’re not looking for the relative path you’re looking for the absolute path. You can search/list files with Get-ChildItem. To select the object properties you’re after you can use Select-Object and the Property you need should be the FullName.
Please always read the complete help including the examples for the cmdlets you use to learn how to use them.

Do a recursive search for the ‘latest’ folders then look for the file.

Get-ChildItem -Recurse -Directory -Include latest -path D:\files | ForEach-Object {
    Get-ChildItem -File $_\loging_*_2020.txt | Select-Object Fullname
}

Edit: fixed typo

[quote quote=195977]If I got you right you’re not looking for the relative path you’re looking for the absolute path. You can search/list files with Get-ChildItem. To select the object properties you’re after you can use Select-Object and the Property you need should be the FullName.

Please always read the complete help including the examples for the cmdlets you use to learn how to use them.

[/quote]
I did, but after fev hours I give up, it didn’t work, it is relative path, because I can not use fullpath

[quote quote=195980]Do a recursive search for the ‘latest’ folders then look for the file.

PowerShell
3 lines
<textarea class="ace_text-input" style="opacity: 0; height: 18px; width: 7px; left: 44px; top: 0px;" spellcheck="false" wrap="off"></textarea>
1
2
3
Get-ChildItem -Recurse -Directory -Include latest -path D:\files | ForEach-Object {
Get-ChildItem Item $_\loging_*_2020.txt | Select-Object Fullname
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[/quote] I tried something like this and didn't work, and this also doesn't work.

[pre]Get-ChildItem : Cannot find path ‘D:\files\Item’ because it does not exist.
At line:2 char:5

  • Get-ChildItem Item $\loging*_2020.txt | Select-Object Fullname
  • CategoryInfo : ObjectNotFound: (D:\files\Item:String) [Get-ChildItem], ItemNotFoundException
  • FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand[/pre]

 

I just found error I changed this and now it works.

Thank you for helping.

[pre]

Get-ChildItem item

Get-ChildItem -File

[/pre]

One more thing if somebody can point me.

How to delete root folder if there is no file in it like these two for example

D:\files\janice\year\latest
D:\files\janice\year\old\

it shhould delete janice and all under it but not others if there is logging_*_2020.txt in it

D:\files\john\year\latest\loging_11_2020.txt
D:\files\john\year\old\loging_11_2020.txt
D:\files\tim\year\latest\loging_09_2020.txt
D:\files\tim\year\old\loging_09_2020.txt
D:\files\janice\year\latest
D:\files\janice\year\old
D:\files\vera\year\latest\loging_12_2020.txt
D:\files\vera\year\old\loging_12_2020.txt
D:\files\mike\year\latest
D:\files\mike\year\old\

AFAIK There’s no built-in option for that. You would need to create a function checks for empty folders and deletes them. You could try to find something fitting in the PowershellGallery or so.

You could use an If statement to determine if the folder had any items in it, and delete it if it didn’t.

Get-Childitem -Path  -Directory -Recurse | foreach-Object {
   If((get-Childitem -path $_.Fullname -File).count -eq 0){
      Remove-item $_.Fullname -Force
      }
   }

Doing it like this could be dangerous because if there is a subfolder with an item in it, but not the folder that is being searched it would delete all of the items inside. This should get you started on what you are looking for though.

thank you this part I was missing to use -eq 0

But still how script will know to delete

from this path

D:\files\janice\year\latest\

this?

janice\year\latest\

You are allowed to spend a little effort yourself and search for a solution. It takes seconds to type “powershell remove empty folder” in the google search and the first hit will teach you everything you need: How to recursively remove all empty folders in PowerShell?
With Powershell it is allowed to sneak in other peoples script and use what you need. That’s why we all share it in places like Powershell.org or StackOverflow or Microsoft Technet. :wink:

Using Get-ChildItem you have to be careful to use the -Recurse or you’re only counting the files under the parent folder. Another simple count method is to use an old COM object from vbScript which is typically faster than recusion with Get-ChildItem:

(New-Object -ComObject Scripting.FileSystemObject).GetFolder('C:\Scripts').Files.Count