List file in a folder but also search for sign in the filename

Welcome Forums General PowerShell Q&A List file in a folder but also search for sign in the filename

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
2 months, 1 week ago.

  • Author
    Posts
  • #170614

    Participant
    Topics: 1
    Replies: 1
    Points: -6
    Rank: Member

    Hi
    My first post here and I need a little help
    I have work a little with powershell before and also other programming language but
    have problem to start to do something basic.

    I have now a bat file that I run to do some working for me but I like to upgrade it to powershell because I need to do a little
    advance search for sign in the file name.

    Is it possible to easy search for a specific file extension (not the subfolders) and then run a loop for each file (it going to run some commands for each file) but first I want to see if the file have sign in the filename.

    Example if the file is name "hello_world_one.ts" then it run this commands, and if the file have name "hello_world.ts" it going to run some anothers commands. it look for the word "one" to decide what commands to run and it can also be more then one sign it going to look after.

  • #170635

    Senior Moderator
    Topics: 8
    Replies: 1085
    Points: 3,650
    Helping Hand
    Rank: Community Hero

    Yes of course, all of the above are possible. You would require below cmdlets to achieve it.

    Get-ChildItem, read the online help doc by executing Get-Help Get-ChildItem -Online, take a look at -Filter parameter as well.

    ForEach-Object, read the online help doc by executing Get-Help ForEach-Object -Online.

    There are examples provided for both the help docs which will help you to put up some code. You can update this thread when getting stuck/errors.

  • #170644

    Participant
    Topics: 1
    Replies: 1
    Points: -6
    Rank: Member

    Ahh thanks alot.

    Now I know what to look for I have almost got it to work.

    I trying to use mkvmerge to merge a ts and srt file to a mkv file.

    The original code was this

     C:\Program Files (x86)\MKVToolNix\mkvmerge" -o E:\TV\TS\klar\%%~nA.mkv E:\TV\TS\klar\%%~nA.ts "–language" "0:swe" "–forced-track" "0:yes" E:\TV\TS\klar\%%~nA.swe.srt

    and I know I can use & to run a command and I use Write-Host to see what output the command show me

     $myfile = "C:\Program Files (x86)\MKVToolNix\mkvmerge -o  E:\TV\TS\klar\$_.BaseName.mkv E:\TV\TS\klar\$_.BaseName.ts –language 0:swe –forced-track 0:yes E:\TV\TS\klar\$_.BaseName.swe.srt"  
    Write-Host $myfile 

    but basename does not work. I does work if just do

     Write-Host $_.BaseName

    But when I run my code it show fullname instead just the name of the file

    /UPDATE

    Hmm I think I almost got it to work

    $myfile = "C:\Program Files (x86)\MKVToolNix\mkvmerge -o  E:\TV\TS\klar\" + $_.BaseName + ".mkv E:\TV\TS\klar\" + $_.BaseName + ".ts –language 0:swe –forced-track 0:yes E:\TV\TS\klar\" + $_.BaseName + ".swe.srt"

    Now it show right but I have to somehow quote "C:\Program Files (86)\MKVToolNix\mkvmerge" in the variable?

You must be logged in to reply to this topic.