Forum Discussion

Ashina09's avatar
Ashina09
Copper Contributor
Oct 26, 2022

PS script to create and move files inside it

Hello

There are a bunch of folders with files inside a folder.

How to create a new folder "Letter" inside each folder containing a name "ABC"? How to move files/folders to the newly created folder if a folder`s name inside the ABC folder contains a word "alphabet"? Thank you in advance!

  • HelloAshina09 hope you are doing good! 

    I have looked at your question and think I understand what you want to do. 

     

    I have a test folder called "Test" on my desktop, and this folder has a few different folders, some of them containing "ABC" in the name and some dont 

    In the "ABC" Folders i have three files, one containing "Alphabet" in the name 

    And the other files have no file with the name "alphabet" in them 

     

    Now I want Powershell to create a new folder in the "ABC" folders called "Letters" 

    This will be done with the following powershell 

    cd "C:\Users\Oliwer\Desktop\TEST"
    
    $Folders = Get-ChildItem |Where-Object {$_.Mode -contains "d-----" -and $_.Name -Like "*ABC*"}
    
    Foreach ($Folder in $Folders){
        New-Item -Path $Folder -Name "Letter" -Type Directory 
    } 

    The script above redirects me to Testfolder and then looks for all folders in there that contains "ABC" in the name. This is saved to a variable called $Folders
    Then the script loops through all the folders found and Creates a new folder called "Letter" 
    This was successfull, as you can see, "Letter" is created in "ABC 1" and "ABC 2" but not in "DEF 1" 

     

    First step completed! 🙂 

    Now we want to look in the "ABC" folders for files that contain the word "Alphabet" and move those files into the newly created "Letter" folder 

    This will be done with the following Powershell

    foreach ($ABC in $Folders){
        $AlphabetFiles = Get-ChildItem -Path $ABC | Where-Object {$_.Mode -NE "d-----" -And $_.Name -like "*Alphabet*"}
    
    foreach ($File in $AlphabetFiles){
        Move-Item -Path $File.FullName -Destination $ABC\Letter
    }
        }

    In the above part of the script we again loop through the "ABC" Folders in the variable $Folders, this time we are looking for FILES that contain "Alphabet" in their name.  These files will be saved to a variable called $AlphabetFiles

    We will then do a loop through the Alphabet files, and move them to the Letter Folder in their "ABC" folder. 

    This was successfull as we can see that the Alphabet 1 file has been moved into the "Letter" folder in both "ABC 1" and "ABC 2" 

     

    Have I understood you correctly that this is what you wanted to do? 

    Otherwise please get back to me and I can help you further 🙂 

     

    The full script to achieve all these steps is below, just change the folder path in  the "CD" command on the top to your file where all of these files are located. 

    cd "C:\Users\Oliwer\Desktop\TEST"
    
    $Folders = Get-ChildItem |Where-Object {$_.Mode -contains "d-----" -and $_.Name -Like "*ABC*"}
    
    Foreach ($Folder in $Folders){
        New-Item -Path $Folder -Name "Letter" -Type Directory 
    } 
    
    foreach ($ABC in $Folders){
        $AlphabetFiles = Get-ChildItem -Path $ABC | Where-Object {$_.Mode -NE "d-----" -And $_.Name -like "*Alphabet*"}
    
    foreach ($File in $AlphabetFiles){
        Move-Item -Path $File.FullName -Destination $ABC\Letter
    }
        }
      • oliwer_sundgren's avatar
        oliwer_sundgren
        Steel Contributor

        No problem at all! ChingisR Glad I Could help 🙂 

        Feel free to mark my reply as best reponse if you feel like it answered your question! 

         

        Wish you a good monday! 

Resources