deoren / notes

Various notes for topics I'm learning
2 stars 0 forks source link

PowerShell | Batch process list of content #73

Open deoren opened 5 years ago

deoren commented 5 years ago

This is an example of using an input text file (one item per line) and a small PowerShell script to read in the list and do something to each item.

In my case I wanted to copy all MP3 files from a UNC network path to a flash drive. Our Bluetooth "compatible" stereo system is being cranky, but presumably still accepts a flash drive with MP3 files for playback.


Text file which resides on the remote share:

artist1
artist2
artist3

PowerShell script run locally:

$source_base_unc_path = "\\1.1.1.1\volume`(sda2`)"
$subfolder = "music"
$dest_path = "D:"

$music_to_sync = Get-Content -Path "$source_base_unc_path\$subfolder\sync_list.txt"

$music_to_sync | ForEach-Object {
    robocopy /S "$source_base_unc_path\$subfolder\$_" "$dest_path\$_" *.mp3
}

I'm sure there are much better approaches, but this worked in a pinch. Recording here for future reference.

deoren commented 5 years ago

Credit for using the *.mp3 pattern with robocopy to filter out content that I'm not interested in copying:

https://arstechnica.com/civis/viewtopic.php?t=608581

Evidently it's fairly clear from the help output from robocopy, but I looked right over it.