powershell - Script to organize folders -
i need organize many folders, folders must have kind of format in names. need rename them , put them this:
parent folder 2nd name + "_" + 2 digits + "_" + folder name
this needs happen every folder .ps1 executed.
here better explanations, here have folders:
00_00_master folder +- 00_01_activities `- 00_02_schedules +- 02_00_dir1 `- 02_01_dir2
as can see, subfolders inside 00_02_schedules
start 2nd "name".
etc, folders (never files). better explanation, added (it happen lot) 3 more folders inside 00_02_schedules. should (before , after executing .ps1)
before:
... `- 00_02 schedules +- 02_00_dir1 +- 02_01_dir2 +- dir3 +- 12345dir4 `- 01_01_dir5
after:
... `- 00_02 schedules +- 02_00_dir1 +- 02_01_dir2 +- 02_02_dir3 +- 02_03_dir4 `- 02_04_dir5
i try explain little (i put code had, when think useless now)
$var1 = 0 $var2 = 0 # has work in same place, never fixed path # c:\users\etc\etc $arr1 = get-childitem -path .\ -directory $arr2 = new-object system.collections.arraylist [string]$dir = get-location $ruta = $dir | split-path -leaf # did these 2 things because needed folder's name, didn't know how # better # think can following code useless now, think tried # array of arrays cleave name foreach ($name in $arr1) { [void]$arr2.add($name) } foreach ($directory in $arr1) { if ($directory -match '^[0-9].*') { rename-item \$directory -newname { [string]($_.name).substring(5) } # part i've being having problems with. may # code works...as long folder's name doesn't start number, # because enters inside loop , many error logs pops out. # renames folders renamed 00_01_00_01, when it's # supposed delete before rename them. # also, think won't work if numeric extension isn't equal 5. } } $arr3 = $ruta -split '_' $foldername = $arr3[1] # works, splits , give me second "piece" between 2 "_" foreach ($directory in $arr1) { rename-item "$directory" -newname $("$foldername" + "_" + "$var1" + "$var2" + "_" + $directory.name) # think should have been: (i put way before) # $("$foldername" + "_" + "$var2" + "$var1" + "_" + $directory.name) # if did way, renamed them 10,20,30 instead of 01,02,03 etc. $var++ if ($var1 = 9) { $var1 = 0 $var2++ # suppose not fancy, in result output can see 2 numbers, not 1 } }
one more thing, possible transform (fixed) code work in folders in recursive way? (would save lot of work , executions).
with initial situation:
> tree └───00_00_master folder ├──00_01_activities └──00_02_schedules ├──01_01_dir5 ├──02_00_dir1 ├──02_01_dir2 ├──12345dir4 └──dir3
this script (adapt set-location fit environment):
## q:\test\2017\08\18\so_45750567.ps1 function test-dirschema ($dirname) { # assume parent processed , complies schema $thisdir = gi $dirname # parent's second number our first $first = $thisdir.parent.name.split('_')[1] if ($thisdir.name -match "^$($first)_\d{2}_.*") { "{0} : {1} complies schema" -f $first,$thisdir.name } else { $highest = (gci "$($thisdir.parent)\$($first)_??_*"|sort|select -last 1).name $second = [int]($highest -replace('^\d{2}_(\d{2})_.*','$1')) $second = ($second + 1).tostring('00') "{0} : {1} not comply schema" -f $first,$thisdir.name $newname = "{0}_{1}_{2}" -f $first, $second,$thisdir.name.trimstart('0123456789_') "new: {0}" -f $newname $thisdir | rename-item -newname $newname } "----" } set-location "a:\00_00_master folder" get-childitem "[0-9][0-9]_*" -rec | where-object {$_.psiscontainer} | foreach-object {test-dirschema $_.fullname}
with output:
> q:\test\2017\08\18\so_45750567.ps1 02 : 01_01_dir5 not comply schema new: 02_02_dir5 ---- 02 : 02_00_dir1 complies schema ---- 02 : 02_01_dir2 complies schema ---- 02 : 12345dir4 not comply schema new: 02_03_dir4 ---- 02 : dir3 not comply schema new: 02_04_dir3 ----
will have end situation:
> tree └───00_00_master folder ├──00_01_activities └──00_02_schedules ├──02_00_dir1 ├──02_01_dir2 ├──02_02_dir5 ├──02_03_dir4 └──02_04_dir3
the order is, due different sorting, not yours. if there no previous number start 01
.
Comments
Post a Comment