PS script to create and move files inside it

Copper Contributor

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!

3 Replies

Hello@Ashina09 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 

oliwer_sundgren_0-1667742958532.png

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

oliwer_sundgren_1-1667743001052.png

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

oliwer_sundgren_2-1667743028621.png

 

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" 

oliwer_sundgren_3-1667743188468.pngoliwer_sundgren_4-1667743209845.pngoliwer_sundgren_5-1667743236350.png

 

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" 

oliwer_sundgren_6-1667743491856.pngoliwer_sundgren_7-1667743506840.png

 

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_sundgrenThank you for your detailed answer! You are great!

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!