Move files to folder structure based on filename

I am new to PS and trying to build up knowledge and experience. I’ve heard that this community is extremely friendly and helpful so I am posting here first. I have been trying to accomplish the following a few different ways and the code below got me the closest but still not what I need. I think this should be relatively easy in PS but I am probably approaching it the wrong way.

So…I have a folder with a bunch of videos files. I need to create a folder for each file where the folder name is the same as the file (minus the file extension of course). Then I need to move that specific file into it’s associated folder. The end result should eliminate all files in the root, and each folder should contain a single file. The comments in the script provide more details.

Everything works except the last step of moving the files into their associated folders.

In this 2nd ForEach loop I try to specify the destination path using the $basename variable used in the 1st ForEach loop. This doesn’t work as it results in moving all the files into a single folder, which is the last object passed to the $basename variable from the 1st Foreach loop.

foreach ($filename in $VideoFiles)
{
Move-Item -Path \server1\Video\Staging$filename -Destination \server1\Video\Staging$basename -verbose
}

This kind of makes sense but with my limited knowledge and experience I can’t figure out another way to accomplish my goal. I started to look into nesting ForEach loops but I am guessing there is an easier way to accomplish this.

I tried the following command in the 2nd loop

Move-Item -Path \server1\Video\Staging$filename -Destination \server1\Video\Staging$filename -verbose

But of course, that variable is storing the file name (including extension) and did not move any files.

Any help will be greatly apprecieted…!

 

#This script should look at a list of files from a folder. Create a folder for each file based on it’s name, and then move each file into the folder with it’s name.

#Folder should start out looking like this:
#c:\videos\video1.avi
#c:\videos\video2.mov
#c:\videos\video3.mkv

#And then look like this:
#c:\videos\video1\video1.avi
#c:\videos\video2\video2.mov
#c:\videos\video3\video3.mkv

#Creates a text file list of the names of the video files without the file extension. Then sets a variable to represent this list/file.
Get-ChildItem -Path \server1\video\staging | ForEach-Object {$_.basename} | Out-File \server1\video\videonames.txt
$VideoNames = Get-Content \server1\video\videonames.txt

##Creates a text file list of the names of the video files with the file extension. Then sets a variable to represent this list/file.
Get-ChildItem -Path \server1\video\staging | ForEach-Object {$_.name} | Out-File \server1\video\videosfiles.txt
$VideoFiles = Get-Content \server1\video\videosfiles.txt

#ForEach loop to create the folder structure of a folder with the name of every file. Example: create a folder named “video1” based on the “video1.avi” file.
foreach ($BaseName in $VideoNames)
{
New-Item -ItemType Directory -Path \server1\Video\Staging$BaseName
}

#ForEach loop to move each file in the folder with the same name. Example: Move “video1.avi” to the “c:\videos\video1” folder.
foreach ($filename in $VideoFiles)
{
Move-Item -Path \server1\Video\Staging$filename -Destination \server1\Video\Staging$basename -verbose
}

https://stackoverflow.com/questions/54585301/create-folders-and-move-files-to-folder-structure-based-on-filename

something like this may work for you

$Files = Get-ChildItem -Path 'c:\video' 
    $Files | ForEach-Object {
    $FileFullName = $_.FullName
    $foldername = $_.BaseName 
    $destinationFolder = "c:\video\$foldername\"
    New-Item -Path c:\video\$foldername -ItemType Directory
    Move-Item $FileFullName $destinationFolder
   }