Solved

Powershell script to list files, create folders and move files to them

Posted on 2016-11-08
3
37 Views
Last Modified: 2016-11-08
Hi,

I need a script to
- read files from a directory
- for each file
  -> create a folder with the file name, stripping it for the file extension
  -> move the file to the newly created folder

The files have a long name with spacings. Anyone that can help me with finding a solution to this?
0
Comment
Question by:Kenneth Endestad
3 Comments
 
LVL 67

Assisted Solution

by:sirbounty
sirbounty earned 250 total points
ID: 41878586
Something like this?

$files = get-childitem c:\parentFolder
foreach ($file in $files) {
    $folderName = $file.name.Replace($file.Extension,'')
    new-item -Path $folderName -ItemType directory
    move-item $file.fullname $folderName -Force
}

Open in new window

0
 
LVL 83

Accepted Solution

by:
oBdA earned 250 total points
ID: 41878593
Assuming the target folders should be created in the same folder where the files were coming from.
It's in test mode and will only what it would normally do; remove the two "WhatIf"s in line 5 and 7 to run it for real.
$Source = "C:\Temp"
Get-ChildItem -Path $Source -File | ForEach-Object {
	$TargetPath = Join-Path -Path $Source -ChildPath $_.Basename
	If (-not (Test-Path -Path $TargetPath)) {
		New-Item -Path $TargetPath -ItemType Directory -WhatIf
	}
	Move-Item -Path $_.FullName -Destination $TargetPath -Force -WhatIf
}

Open in new window


sirbounty,
you're processing folder as well, and the files will land in whatever folder the user happens to be, or fail if the drive happens to be not a FileSystem provider.
0
 

Author Closing Comment

by:Kenneth Endestad
ID: 41878661
Works like a charm
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Utilizing an array to gracefully append to a list of EmailAddresses
The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question