Solved

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

Posted on 2016-11-08
3
13 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
Comment Utility
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 82

Accepted Solution

by:
oBdA earned 250 total points
Comment Utility
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
Comment Utility
Works like a charm
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now