Friday, 15 July 2011

Powershell - Create folder using a CSV file -



Powershell - Create folder using a CSV file -

i created little script utilize csv file batch create folder. saw people doing folder creation using different way.

csv:

folder 4.1.1 process 4.1.2 score card 4.1.3 strategy 4.1.4 governance 4.1.5 master plan calendar 4.1.6 budget follow 4.1.7 budget documentation 4.1.8 benchmarkvision 4.1.9 std documentation 4.1.10 layout 4.1.11 project 4.1.12 training 4.1.13 team construction 4.1.14 work shop 4.1.15 tools 4.1.16 problem solving 4.1.17 presentation 4.1.18 working info zone 4.1.19 meeting 4.1.20 s 4.1.21 miscellenous

script:

#change $folderlist path it's hard link. $folderlist = import-csv "c:\folders.csv" $rootpath = read-host "enter path of root folder csv files created" foreach ($folder in $folderlist){ $path = $rootpath+$folder.folder new-item -type directory -path $path }

quite simple, saw people utilize things $(_$.folder) or other functions don't understand. there can show me other way using $_ , %{ }?

i hope question clear if not give more information.

john

the thing think alter (assuming input csv formatted) how you're constructing path.

foreach ($folder in $folderlist){ $path = join-path -path $rootpath -childpath $folder.folder; new-item -type directory -path $path; }

alternate:

foreach ($folder in $folderlist){ new-item -type directory -path $rootpath -name $folder.folder; }

alternate 2 (derived above):

$folderlist|foreach-object {new-item -type directory -path $rootpath -name $_.folder;}

alternate 3 (derived above):

$folderlist|foreach-object {new-item -type directory -path (join-path -path $rootpath -childpath $_.folder);}

% alias foreach-object - utilize expanded alias in scripts & explanations this, ensure crystal clear.

edit: 1 more way that's more concise , depending on size of csv file, improve on memory usage.

$rootpath = read-host "enter path of root folder csv files created" import-csv "c:\folders.csv"|foreach-object {new-item -type directory -path (join-path -path $rootpath -childpath $_.folder);}

powershell-v2.0

No comments:

Post a Comment