Solved

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

Posted on 2016-11-08
3
65 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 84

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the adminiā€¦

740 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