Link to home
Start Free TrialLog in
Avatar of windows-it
windows-it

asked on

Create folders from a csv file

Hi everyone: :)
We need to create folders from an csv and want to add subfolders to them. The powerShell script works to create the folder but not to create the subfolders.

content of the csv:
Name
Folder1
Folder2
Folder3
_____________________
Just to create the main folder (this works fine)
Set-Location "D:\customers"  
$Folders = Import-Csv C:\Temp\foldernames.csv
ForEach ($Folder in $Folders) {
New-Item $Folder.Name -type directory
}
_____________________

_____________________
to create the main folder and the subfolders (this doesn't works)
Set-Location "D:\customers"  
$Folders = Import-Csv C:\Temp\foldernames.csv
ForEach ($Folder in $Folders) {
New-Item $Folder.Name\Subfolder1 -type directory
New-Item $Folder.Name\Subfolder2 -type directory
}
_____________________

How should the script look like that it works? (sorry I'm a PowerShell beginner)
Thanks for any help!
Avatar of Qlemo
Qlemo
Flag of Germany image

Do you want to create the same subfolders in each "parent"?
Are the parent folders created when you run the second script?
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
$Folders = import-csv C:\Temp\foldernames.csv
$Folders | %{
If (!(test-path D:\Project\$($_.Process)))
{
new-item -type directory -path "C:\Project\$($_.Process)"}
new-item -type directory -path "C:\Project\$($_.Process)\$($_.type)"
}
Avatar of oBdA
oBdA

Qlemo,
It doesn't work as shown by the asker, not with an object property in combination with a string (as in "New-Item $Folder.Name\Subfolder1").
And just as "md", Powershell's New-Item (at least for the filesystem provider) doesn't care whether the parent folder exists or not; it will create the whole tree if required.
Only in good old real DOS (command.com, not the NT shell cmd.exe) was it required to create one folder after the other.
And me, I hate, hate, hate file and folder operations based on the current folder.
I don't think it's cleaner code than using full paths, in contrary - if you don't check whether the Set-Location was successful, and for example the drive doesn't exist, the script will continue and just create the folders wherever you were before.
Personally, I'd do it like this, but I didn't want to deviate too much from the original script:
$BaseFolder = "C:\customers"
$Subfolders = "Subfolder1", "Subfolder2"
Import-Csv -Path "C:\Temp\foldernames.csv" | ForEach-Object {
	ForEach ($Subfolder In $Subfolders) {
		$NewFolder = "$($BaseFolder)\$($_.Name)\$($Subfolder)"
		If (-Not (Test-Path -Path $NewFolder)) {
			New-Item -Path $NewFolder -ItemType Directory
		}
	}
}

Open in new window

The outer loop with the unknown quantity of file names as pipeline, the inner loop as a PS ForEach to avoid having the confusion with two separate $_ variables in the loops.
Avatar of windows-it

ASKER

oBdA's solutions works perfect for me. Thanks a lot!!