relative path

Welcome Forums General PowerShell Q&A relative path

Viewing 10 reply threads
  • Author
    Posts
    • #195950
      Participant
      Topics: 1
      Replies: 4
      Points: 23
      Rank: Member

      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?

    • #195977
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      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.

    • #195980
      Participant
      Topics: 0
      Replies: 25
      Points: 163
      Helping Hand
      Rank: Participant

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

      Edit: fixed typo

    • #196010
      Participant
      Topics: 1
      Replies: 4
      Points: 23
      Rank: Member

      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.

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

    • #196013
      Participant
      Topics: 1
      Replies: 4
      Points: 23
      Rank: Member

      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

      I tried something like this and didn’t work, and this also doesn’t work.

       

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

      Thank you for helping.

    • #196205
      Participant
      Topics: 1
      Replies: 4
      Points: 23
      Rank: Member

      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\

    • #196223
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      How to delete root folder if there is no file in it …

      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.

    • #196346
      Participant
      Topics: 5
      Replies: 8
      Points: 128
      Rank: Participant

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

      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.

    • #196418
      Participant
      Topics: 1
      Replies: 4
      Points: 23
      Rank: Member

      D:\files\janice\year\latest\

      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\

    • #196442
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      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. 😉

    • #196655
      Participant
      Topics: 15
      Replies: 1761
      Points: 3,167
      Helping Hand
      Rank: Community Hero

      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:

Viewing 10 reply threads
  • The topic ‘relative path’ is closed to new replies.