• I am new to Powershell and need some advice on the best way to check three different folders for files ending in .txt if there are .txt files found then the script will need to create a new sub-folder and move the files into that folder.  For example, C:\Test1 has a .txt file, so create C:\test1\error and move the .TXT file into that error folder.  Then move onto the second folder C:\Test2, this one has 3 .txt files, create a new folder C:\test2\errors and move the 3 files into that folder then move onto C:\Test3, this one has no .txt files do do nothing.


    Tuesday, November 13, 2012 4:09 PM


All replies

  • How "new" are you to Powershell?

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Tuesday, November 13, 2012 4:20 PM
  • Flowchart it first, that's my advice.

    Grant Ward, a.k.a. Bigteddy

    Tuesday, November 13, 2012 4:23 PM
  • Very new!  I did write a script already that will check a folder and get the count of .txt files and if the count is > 0 then it sends an email and then creates the new sub-folder and moves each file into it.  It works great but I need it to check multiple folders and do the same process on each, that is where I am unsure of the best process.  Obviously I can just repeat my code for each folder but I'm quite sure there is a simpler way to do it.  Not being familiar with Powershell I am stumbling.


    Tuesday, November 13, 2012 4:24 PM
  • You need to take your existing script, and use a list of folder names to drive that script once for each folder in the list using one of the Powershell loop constructs.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    • Marked as answer by CINB Tuesday, November 13, 2012 7:49 PM
    Tuesday, November 13, 2012 4:28 PM
  • I'm getting closer, but still missing something....when I do this code:

    $directory = "C:\Template1\","C:\Template2\"
    $foreach ($f in $directory} {$f = Get-ChildItem $directory -filter "*.txt" |MeasureObject }

    It will give me an $f.count but it counts all the .txt files in both directories.  So, there are 2 in template1 and 1 in template 2 so the count comes back 3.

    What code am I missing so that it shows each individual directories count?


    Tuesday, November 13, 2012 5:50 PM
  • $directory = "C:\Template1\","C:\Template2\"
    foreach ($f in $directory) {@(Get-ChildItem $f -Filter *.txt).count}

    Grant Ward, a.k.a. Bigteddy

    • Edited by Bigteddy Tuesday, November 13, 2012 6:14 PM
    • Marked as answer by CINB Tuesday, November 13, 2012 7:49 PM
    Tuesday, November 13, 2012 6:11 PM
  • One more thing, I have the script working and thought it would be simple to add a Move-Item in the mix but I keep getting errors.  Below is the script before adding the move item and this part works perfectly, it creates a new directory and sends an email.  I question if the Move-Item will need to be put in a different if statement because I am moving the files to the new folder that I created, which ends up being C:\Template1\Errors\2012114\

    $directory = "C:\Template1\","C:\Template2\"
    foreach ($f in $directory) {if(@(Get-ChildItem $f -Filter *.lis).count -gt 0) {
        New-Item "$f$("Errors\")$(get-date -f yyyyMMdd)"-type directory

    Here is the command for the Move-Item that I was listing after the sendEmail:

    Move-Item $_.FullName "$($f$("Errors\")$(get-date -f yyyyMMdd)")\$($_.BaseName)$($_.extension)"


    Wednesday, November 14, 2012 4:49 PM
  • I'm not with you.  Why are you counting the files in the first place?  Why are you trying to create a new folder for each file in the directory?

    Perhaps if you explain the bigger picture, I could be of more help.  What you're asking / trying to do doesn't make sense to me right now.

    Grant Ward, a.k.a. Bigteddy

    Wednesday, November 14, 2012 5:43 PM
  • I have to check each directory, if there are any files with a .lis extension then I have to send out an email notification, create a new folder named Errors\(current date) and then move the files into this new folder so that it doesn't affect the next days processing.  So, I was using the count function to identify if any *.lis files existed.  Now that I am typing this, I am second-guessing why I chose to do it that way as well.


    Wednesday, November 14, 2012 5:56 PM
  • Try this:

    $directories = "C:\Template1\","C:\Template2\"
    $dirName = Join-Path -Path 'c:\errorFolder' -ChildPath  ("Errors" + (Get-Date  -Format 'yyyyMMdd')) 
    if (gci $directories -Include '*.lis') {
        md $dirName
        gci $directories | Move-Item $dirName -WhatIf

    Grant Ward, a.k.a. Bigteddy

    Wednesday, November 14, 2012 6:24 PM
  • The -Include doesn't seem to work correctly unless I use -Recurse - Include but I don't want to use -recurse because I only want to check the Template1 and Template2 folders not in their sub-folders.  I did try and replace it with -filter and it worked as far as it created the new folders but unfortunately if template1 had '*.lis' files but template2 did not it still created the new folders in each template folder regardless.  I would only want it to create the new folder for the template(s) that had the *.lis files.  Then the last portion gci $directories | Move-Item $dirName didn't end up moving anything.


    Actually, figured out why it creates the directory in both template folders, I replaced the path with the $directories, so make sense that it would create it based on the first true result.  But, that's fine, I'm not overly concerned with creating the directory my current code works fine.  It's more of the move-item not working that is the issue.  I'll keep digging! 
    • Edited by CINB Wednesday, November 14, 2012 8:41 PM
    Wednesday, November 14, 2012 8:25 PM
  • The Move-Item has the -WhatIf parameter set, so it won't do anything.  It should show you on the screen what it would do.

    Grant Ward, a.k.a. Bigteddy

    Thursday, November 15, 2012 5:49 AM