Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2016-11-08
3
Medium Priority
?
377 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 1000 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 85

Accepted Solution

by:
oBdA earned 1000 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
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…
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 anti-spam), the admin…

972 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