locked
Moving and Organizing files based on number of lines RRS feed

  • Question

  • Hello,

    I have a directory containing nearly 1500 files, some of which are composed of 12 lines, some of which are composed of 4 lines, all of which are .phy (PHYLIP) files, which can be viewed within notepad/wordpad like text files.

    I need to run a script which will identify the number of lines in each file, and move them to the corresponding folder. File size will not work for sorting, I have already tried.

    i.e.- all files with 4 lines in one folder, all files with 12 lines in another folder, no files left (so i can be sure everything was inspected) in the main directory.

    Can anyone help?

    thanks

    originally posted in the microsoft answers forum and redirected by a mod,

    cross-posted here, redirected a second time :http://social.technet.microsoft.com/Forums/windows/en-US/a6cfad18-f94b-41c3-ae9c-30ebf260aacd/moving-and-organizing-files-based-on-number-of-lines?forum=w7itprogeneral

    Tuesday, July 29, 2014 3:44 PM

Answers

  • Given that you can read these files with notepad, I would think you could use Get-Content.

    Something like this may work:

    $Path = "\\Path\To\Directory"
    $12linepath = "\\Path\To\12lineDirectory"
    $4linepath = "\\Path\To\4lineDirectory"
    $nomatchpath = "\\Path\To\NoMatchDirectory"
    $files = Get-ChildItem -Path $Path -Filter *.phy | Select -ExpandProperty FullName
    ForEach ($file in $files)
    {
        $linecount = (Get-Content $file).count
        If($linecount -eq 12)
        {
            Move-Item -Path $file -Destination $12linepath
        }
        If($linecount -eq 4)
        {
            Move-Item -Path $file -Destination $4linepath
        }
        else
        {
            Move-Item -Path $file -Destination $nomatchpath
        }
    }

    • Marked as answer by Chavo2 Tuesday, July 29, 2014 4:46 PM
    Tuesday, July 29, 2014 4:10 PM

All replies

  • Have you tried anything so far? I'd try opening the files using PowerShell using Get-Content and seeing if it is possible to treat them as simple text documents.

    Tuesday, July 29, 2014 3:46 PM
  • All that I have tried so far is to import them into excel using VBA code, so that i could sort the data.  Excel was able to treat the files as text documents, even with the odd extension, so i expect that a method that treats them as such should yield perfect results.

    once i got them into excel, I realized that these 4-line files were hiding among the swarm, and I need to remove them before import so that I can work with that data separately.

    using powershell  get-content i was able to directly view the files contents as well.


    Thermal Process Application Engineer

    Tuesday, July 29, 2014 3:54 PM
  • Following onto Alex Brassington's question:

    What specifically have you tried? You have not posted your script and what hasn't worked. Specifically, you said "File size will not work for sorting, I have already tried" but you did not say what this means. (When you say that something didn't work, you have to say how it didn't work.)

    Please post your code and tell exactly why what you did wasn't effective. Specific error messages are helpful.

    It would also be helpful to give enough detail about your environment so that respondents can reproduce your scenario and cause the error(s) you are seeing.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, July 29, 2014 4:05 PM
  • In that case you'll want to count the number of lines in the object and then move them as required.

    Have a crack at it, a quick google found a lot of training resources with similar goals.

    Tuesday, July 29, 2014 4:06 PM
  • Given that you can read these files with notepad, I would think you could use Get-Content.

    Something like this may work:

    $Path = "\\Path\To\Directory"
    $12linepath = "\\Path\To\12lineDirectory"
    $4linepath = "\\Path\To\4lineDirectory"
    $nomatchpath = "\\Path\To\NoMatchDirectory"
    $files = Get-ChildItem -Path $Path -Filter *.phy | Select -ExpandProperty FullName
    ForEach ($file in $files)
    {
        $linecount = (Get-Content $file).count
        If($linecount -eq 12)
        {
            Move-Item -Path $file -Destination $12linepath
        }
        If($linecount -eq 4)
        {
            Move-Item -Path $file -Destination $4linepath
        }
        else
        {
            Move-Item -Path $file -Destination $nomatchpath
        }
    }

    • Marked as answer by Chavo2 Tuesday, July 29, 2014 4:46 PM
    Tuesday, July 29, 2014 4:10 PM
  • thank you Mr. Ince, this worked flawlessly.


    Thermal Process Application Engineer

    Tuesday, July 29, 2014 4:46 PM