File globbing with powershell -


script working how intended, still struggling renaming duplicate files. cannot figure out how name files

filename(1).ext

filename(2).ext

the closest have gotten was

filename(1).ext

filename(1)(2).ext

`#region actual script  $srcroot = "c:\srclocation"  $dstroot = "c:\dstlocation"   $filelist = get-childitem -path $srcroot -file -force -recurse      foreach ($file in $filelist) {       $filename = $file.name.toupper()     $fileext = $file.extension.toupper()      $dstfilename = $null       switch -regex ($filename)     {    '[a-z]{4}-[0-9]{3}' { $dstfilename = $filename }    '[a-z]{4} [0-9]{3}' { $dstfilename = $filename -replace '([a-z]{4})\s([0-     9]{3})','$1-$2' }    '[a-z]{4}[0-9]{3}' { $dstfilename = $filename -replace '([a-z]{4})([0-9]     {3})','$1-$2'}    default { write-warning -message "$filename not expected filename"     }    }     if ($dstfilename) {    $dstdir = $dstfilename.split('.')[0].substring(0,8)    $dstpath = join-path -path $dstroot -childpath $dstdir     if (-not (test-path -path $dstpath)) {        new-item -path $dstpath -itemtype directory    }    $i = 1    if (test-path $dstpath\$dstfilename){            $dstfilename = $dstfilename.split('.')[0] + "($i)" + $fileext     while (test-path $dstpath\$dstfilename){         $i +=1         $dstfilename = $dstfilename -replace      }    }      write-verbose "moving $($file.fullname)"    move-item -path $($file.fullname) -destination $dstpath\$dstfilename -    erroraction continue    }    }    #endregion` 

you can use replace method of string objects in powershell. verify input, can use regex. move-item throw error, if file exists in destination anyways. complete script this.

#region setup new-item -path c:\srcpath,c:\dstpath -itemtype directory set-location c:\srcpath new-item 'abcd123.txt','abcd 123.txt','abcd-123.txt','aaaa111.txt','bbbb 222.jpg','bbbb-222.txt' -itemtype file #endregion  #region actual script $srcroot = "c:\srcpath" $dstroot = "c:\dstpath"  $filelist = get-childitem -path $srcroot -file -force -recurse  foreach ($file in $filelist) {      $filename = $file.name.toupper()     $dstfilename = $null      switch -regex ($filename)     {         '[a-z]{4}-[0-9]{3}' { $dstfilename = $filename }         '[a-z]{4} [0-9]{3}' { $dstfilename = $filename -replace '([a-z]{4})\s([0-9]{3})','$1-$2' }         '[a-z]{4}[0-9]{3}' { $dstfilename = $filename -replace '([a-z]{4})([0-9]{3})','$1-$2'}         default { write-warning -message "$filename not expected filename" }     }      if ($dstfilename) {         $dstdir = $dstfilename.split('.')[0]         $dstpath = join-path -path $dstroot -childpath $dstdir          if (-not (test-path -path $dstpath)) {             new-item -path $dstpath -itemtype directory         }          write-verbose "moving $($file.fullname)"         move-item -path $($file.fullname) -destination $dstpath\$dstfilename -erroraction continue     } } #endregion  #region result write-host '----- result -----' -backgroundcolor darkyellow get-childitem c:\dstpath -recurse | select-object -expandproperty fullname #endregion 

Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -