Windows Vista Forums

Comparing filenames to strings

  1. #1


    Tim Guest

    Comparing filenames to strings

    What is the best way to return the name of a file without the
    extension? I have a text file that contains a list of computer
    names. I have a directory that contains a list of files in the format
    of computername.xml. So I want to do a compare to see what names are
    in the text file but don't have a corresponding .xml file in the
    directory.



    I see there is a compare-object command that looks like it will do
    what I want, but I can't figure out how to strip off the .xml from the
    file name.

    Thanks,

    Tim


      My System SpecsSystem Spec

  2. #2


    Oisin Grehan Guest

    Re: Comparing filenames to strings

    On Aug 17, 11:13 am, Tim <crosby....@gmail.com> wrote:
    > What is the best way to return the name of a file without the
    > extension? I have a text file that contains a list of computer
    > names. I have a directory that contains a list of files in the format
    > of computername.xml. So I want to do a compare to see what names are
    > in the text file but don't have a corresponding .xml file in the
    > directory.
    >
    > I see there is a compare-object command that looks like it will do
    > what I want, but I can't figure out how to strip off the .xml from the
    > file name.
    >
    > Thanks,
    >
    > Tim


    $stem = [system.io.path]::GetFileNameWithoutExtension($filename)

    Hope this helps,

    - Oisin


      My System SpecsSystem Spec

  3. #3


    Kiron Guest

    Re: Comparing filenames to strings

    compare-object $(get-content computers.txt) $(get-childItem -filter *.xml |
    foreach {$_.name -replace $_.extension}) | sort InputObject

    --
    Kiron


      My System SpecsSystem Spec

  4. #4


    Brandon Shell Guest

    Re: Comparing filenames to strings

    The glory of Powershell... so MANY ways

    $list = get-childitem -filter *.xml | foreach{$_.BaseName}
    switch -file c:\temp\test.txt{{!($list -contains $_)}{write-host $_}}

    "Tim" <crosby.tim@gmail.com> wrote in message
    news:1187363596.651612.139310@57g2000hsv.googlegroups.com...
    > What is the best way to return the name of a file without the
    > extension? I have a text file that contains a list of computer
    > names. I have a directory that contains a list of files in the format
    > of computername.xml. So I want to do a compare to see what names are
    > in the text file but don't have a corresponding .xml file in the
    > directory.
    >
    > I see there is a compare-object command that looks like it will do
    > what I want, but I can't figure out how to strip off the .xml from the
    > file name.
    >
    > Thanks,
    >
    > Tim
    >



      My System SpecsSystem Spec

  5. #5


    Tim Guest

    Re: Comparing filenames to strings

    On Aug 17, 11:55 am, "Kiron" <Ki...@HighPlainsDrifter.com> wrote:
    > compare-object $(get-content computers.txt) $(get-childItem -filter *.xml |
    > foreach {$_.name -replace $_.extension}) | sort InputObject
    >
    > --
    > Kiron


    Thanks Kiron, that helped. But I am still not getting the results I
    had hoped for. When I run your command, I get results like the
    following:

    Computer1 <=
    Computer1 =>
    Computer2 <=
    Computer2 =>

    I was expecting to only see a line for the items that are in one place
    or the other. But it lists two items for the ones that are in both
    places.

    Also, I'm wondering how I would have found information on that -
    replace command. Couldn't find anything about it in PSH help.

    Thanks,

    Tim


      My System SpecsSystem Spec

  6. #6


    Oisin Grehan Guest

    Re: Comparing filenames to strings

    On Aug 17, 2:59 pm, Tim <crosby....@gmail.com> wrote:
    > On Aug 17, 11:55 am, "Kiron" <Ki...@HighPlainsDrifter.com> wrote:
    >
    > > compare-object $(get-content computers.txt) $(get-childItem -filter *.xml |
    > > foreach {$_.name -replace $_.extension}) | sort InputObject

    >
    > > --
    > > Kiron

    >
    > Thanks Kiron, that helped. But I am still not getting the results I
    > had hoped for. When I run your command, I get results like the
    > following:
    >
    > Computer1 <=
    > Computer1 =>
    > Computer2 <=
    > Computer2 =>
    >
    > I was expecting to only see a line for the items that are in one place
    > or the other. But it lists two items for the ones that are in both
    > places.
    >
    > Also, I'm wondering how I would have found information on that -
    > replace command. Couldn't find anything about it in PSH help.
    >
    > Thanks,
    >
    > Tim


    PS > gc computers.txt | % { if (-not (test-path ".\${_}.xml") )
    { $_ } }

    this looks for computername.xml in the current directory. if not
    found, spits out computername.

    - Oisin



      My System SpecsSystem Spec

  7. #7


    Kiron Guest

    Re: Comparing filenames to strings

    Oops! Then is best to sort both outputs (the file's and get-childItem's)
    with the -unique switch, this will give you a list of unique items to
    compare against.
    I also restricted the filter to exactly match the xml extension, just in
    case.

    Try:
    compare-object $(get-content computers.txt | sort -unique)
    $(get-childItem -filter *.xml | where {$_.extension -eq '.xml'} | foreach
    {$_.name -replace $_.extension} | sort -unique) | sort InputObject

    To read about -replace:
    help about_comparison_operators
    help about_operator

    --
    Kiron


      My System SpecsSystem Spec

  8. #8


    Flowering Weeds Guest

    Re: Comparing filenames to strings


    >
    > To read about -replace:
    > help about_comparison_operators
    > help about_operator
    >


    Mmm for left and right, up
    and down help file reading

    Perhaps Microsoft's Log Parser 2.2

    PS> get-help about_comparison_operators |
    LogParser.exe "SELECT text FROM STDIN
    WHERE text IS NOT NULL"
    -i:textline /qn -o:datagrid
    PS>

    Just another way!



      My System SpecsSystem Spec

  9. #9


    Rob Campbell Guest

    Re: Comparing filenames to strings

    How about:

    gc list.txt |% {if (!(test-path $_.xml)){write-host $_}

    "Brandon Shell" wrote:

    > The glory of Powershell... so MANY ways
    >
    > $list = get-childitem -filter *.xml | foreach{$_.BaseName}
    > switch -file c:\temp\test.txt{{!($list -contains $_)}{write-host $_}}
    >
    > "Tim" <crosby.tim@gmail.com> wrote in message
    > news:1187363596.651612.139310@57g2000hsv.googlegroups.com...
    > > What is the best way to return the name of a file without the
    > > extension? I have a text file that contains a list of computer
    > > names. I have a directory that contains a list of files in the format
    > > of computername.xml. So I want to do a compare to see what names are
    > > in the text file but don't have a corresponding .xml file in the
    > > directory.
    > >
    > > I see there is a compare-object command that looks like it will do
    > > what I want, but I can't figure out how to strip off the .xml from the
    > > file name.
    > >
    > > Thanks,
    > >
    > > Tim
    > >

    >
    >


      My System SpecsSystem Spec

  10. #10


    Rob Campbell Guest

    Re: Comparing filenames to strings

    That one isn't going to work. Maybe something like:

    gc list.txt |% {$_ += ".xml";if (!(test-path $_)){write-host $_}


    "Rob Campbell" wrote:

    > How about:
    >
    > gc list.txt |% {if (!(test-path $_.xml)){write-host $_}
    >
    > "Brandon Shell" wrote:
    >
    > > The glory of Powershell... so MANY ways
    > >
    > > $list = get-childitem -filter *.xml | foreach{$_.BaseName}
    > > switch -file c:\temp\test.txt{{!($list -contains $_)}{write-host $_}}
    > >
    > > "Tim" <crosby.tim@gmail.com> wrote in message
    > > news:1187363596.651612.139310@57g2000hsv.googlegroups.com...
    > > > What is the best way to return the name of a file without the
    > > > extension? I have a text file that contains a list of computer
    > > > names. I have a directory that contains a list of files in the format
    > > > of computername.xml. So I want to do a compare to see what names are
    > > > in the text file but don't have a corresponding .xml file in the
    > > > directory.
    > > >
    > > > I see there is a compare-object command that looks like it will do
    > > > what I want, but I can't figure out how to strip off the .xml from the
    > > > file name.
    > > >
    > > > Thanks,
    > > >
    > > > Tim
    > > >

    > >
    > >


      My System SpecsSystem Spec

Page 1 of 2 12 LastLast
Comparing filenames to strings

Similar Threads
Thread Thread Starter Forum Replies Last Post
Trouble comparing strings Jacob Sampson PowerShell 6 08 Sep 2009
Comparing arrays Mike PowerShell 4 14 Apr 2009
Re: Hyper-V comparing Ryan Sokolowski [MVP] Virtual Server 0 22 Sep 2008
comparing secure strings... Ben Christian PowerShell 3 02 Apr 2008
Comparing strings - is it a bug? Tibor Soos PowerShell 7 13 Mar 2008