Dup Goto 📝

RecursiveFileSearch1

PT2/lang/powershell 11-26 19:00:11
To Pop
42 lines, 236 words, 1608 chars Sunday 2023-11-26 19:00:11

How to Recursive Search and Copy

In powershell (right click the start menu and you'll get the Win+X menu)

cd $Env:UserProfile\Documents
Set-Variable -name crate -value $Env:UserProfile\Documents\TheCrate
mkdir $crate
cd SourceDir 
get-childitem -recurse -include *.wav | foreach { cp $_ $crate }

That will put everything into a single folder, which is probably what you want. Even if not, you can then use the file manager to organise things afterwards.

Copy Preserving Directory Structure

$source = $Env:UserProfile\Documents\SourceDir
cd $source
$tgt = $Env:UserProfile\Documents\TargetPath
if( -not (test-path -path $tvt) ) { mkdir $tgt }
Get-ChildItem -recurse -include *.wav | select-object -first 100 | foreach { 
    $r = resolve-path -relative $_; 
    $t = $tgt + "\" + $r; 
    $d = split-path -parent $t; 
    if( -not (test-path -path $d) ) { mkdir $d }; 
    if( -not (test-path -path $t) ) { cp $_ $t };
}

Macos Zips

See this stackoverflow for a few more examples of this kind of search.

Deleting those annoying ._whatever files you get when unarchiving a .zip on Windows or Linux.

get-childitem -path c:\path\to\files -include ._* -Recurse
get-childitem -path d:\mydir -include ._* -Recurse | foreach { echo $_ }
get-childitem -include ._* -Recurse | foreach { Remove-Item $_ }    # will start from current directory

for reference, on Windows under Cygwin or WSL, you can use

find /path/to/files -name ._\* -exec rm -v {} \;