Reputation: 193
I need a command that can be run from the powershell to create a folder for each file (based on the file-name) in a directory and then move the file into the newly created folders.
Example :
Starting Folder:
Dog.jpg
Cat.jpg
The following command works great at creating a folder for each filename in the current working directory.
Result Folder:
\Dog\ \Cat\
Dog.jpg
Cat.jpg
What I want to achieve is:
\Dog\Dog.jpg
\Cat\Cat.jpg
Can someone help me with this?
Upvotes: 0
Views: 1376
Reputation: 793
I would try something like this:
Function Create-FolderAndMoveFile
{
param(
[string]$folder
)
$files = Get-ChildItem -Path $folder -Filter *.jpg
foreach($file in $files)
{
$dir = [io.path]::GetFileNameWithoutExtension("$file")
$dest = $("$folder\$dir")
New-Item $dest -ItemType Directory
Move-Item -Path $file.Fullname -Destination $dest
}
}
Then you can call this function like Create-FolderAndMoveFile -folder C:\YourSuperAwesomePath\
and it will create a folder for every .jpg file there is..
Hope this helps
Upvotes: 1