Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Need help with PowerShell - create a list of files in folder, removing a part of the file name
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

Need help with PowerShell - create a list of files in folder, removing a part of the file name

myhkenmyhken Member
edited August 2017 in General

Hello, this time I need help with a powershell script that can give me a list of file in two folders.
But I want to remove a part of the file name, before I get the list.
Each filename is formatted in this way:

Folder 1

VERITAS-SRV-201_2017-08-03_0129.os.txt

VERITAS-SRV-201_2017-08-03_0129.prod.txt

VERITAS-SRV-201_2017-08-03_0129.ip.txt

Folder 2

VERITAS-SRV-201-192.168.1.120.txt

In the first folder, I just want a list based on one of the three files per server. It will always be three files per server. So the list can be based on VERITAS-SRV-201_2017-08-03_0129.os.txt
The server name is always before the first _. I also want to loose .os.txt so I just get a list with VERITAS-SRV-201 an so on. The servername can variate from just NAME, to NAME-MORE to like what you see over, NAME-MORE-MORE

In folder two, I just want to get a list with the servername. I want to loose the IP and .txt
Again, the name before the IP can be just NAME, or NAME-MORE or NAME-MORE-MORE

Can anybody help me we this?

Comments

  • NekkiNekki Veteran

    I've got one of the greatest poweshell minds on the planet on my team.

    Unfortunately, he's on holiday.

    Thanked by 2myhken WSS
  • Nekki said: Unfortunately, he's on holiday.

    What?? Recall his holiday at once, so he could help me with this.

  • NekkiNekki Veteran

    @myhken said:

    Nekki said: Unfortunately, he's on holiday.

    What?? Recall his holiday at once, so he could help me with this.

    I could actually call him but I reckon he'd be pretty annoyed once he found out it was to help a random guy on a forum.

    Thanked by 1myhken
  • show what you have done, maybe some user might help tweak the script

    Thanked by 1myhken
  • I actually found it out after I played with some guides, took little from here and there and suddently I got what I wanted. I'm on my phone now, but can post the final script here for future referance later on.

  • WSSWSS Member
    #!/bin/sh
    for shitfile in folder1/*.txt; do
      echo $shitfile | cut -f\. -d1
    done
    

    For the second one, I'd break at the first period, then back up at the prior hypen, but I bet this will piss off ClodFart enough. No, it's not PowerShell. I prefer a functional OS.

  • @Nekki said:
    I've got one of the greatest poweshell minds on the planet on my team.

    Unfortunately, he's on holiday.

    I'm back :)

    Thanked by 1myhken
  • NekkiNekki Veteran

    @jetchirag said:

    @Nekki said:
    I've got one of the greatest poweshell minds on the planet on my team.

    Unfortunately, he's on holiday.

    I'm back :)

    You're not even close, son.

    Thanked by 1myhken
  • Here is the script that did the trick for me in folder 1:

    $orgPath = "C:\folder1\" $delim = "_" Get-ChildItem $orgPath -File "*.ip*" -Name | foreach { $nameArray = $_.Split($delim) $newName = $nameArray[0] Write-Output $newName | Out-File "C:\temp\server-sjekk-liste.txt" -Append }

Sign In or Register to comment.