I have no PowerShell skill but have happily been able to use occasional scripts in copy-paste-run mode. Embarrassingly I cannot recall the source of the script below, which I found in my Win10 files as a .txt file. Having first checked that the source was still correct, and with a USB stick as L:, I changed its exrension to .ps1, opened the PowerShell window,and after
PS C:\Windows\System32\WindowsPowerShell\v1.0> I entered this
C:\Users\terry\Dropbox\Scripts (Other,Sundry)\Powershell-CopyToSD.ps1
and then hit the Enter key. I hoped it would copy all files sequentially from the source to L:\ but it gave me the following error message:
At line:1 char:38
+ C:\Users\terry\Dropbox\Scripts (Other,Sundry)\Powershell-CopyToSD.ps1
+ ~
Missing argument in parameter list.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingArgument
At line:1 char:38
+ C:\Users\terry\Dropbox\Scripts (Other,Sundry)\Powershell-CopyToSD.ps1
+ ~
Missing argument in parameter list.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingArgument
That identifies the comma in the script path as the culprit, even though that folder name is OK in File Explorer. So I moved the script and ran it again.
PS C:\Windows\System32\WindowsPowerShell\v1.0> C:\PowerShell_Scripts\Powershell-CopyToSD.ps1
Copying 0002-two.mp3...
Copying 0001-one550.mp3...
Copying 0004-four.mp3...
Copying 0003-three300.mp3...
Copying 0006-six.mp3...
Copying 0005-five.mp3...
Copying 0008-eight.mp3...
.
.
Copying 0126-YesterdaysMinOutdoorWas2900.mp3...
Copying 0127-zero.mp3...
All files copied in order.
However Iām dismayed to find it didnāt do what it said, namely āAll files copied in order.ā
- No files have appeared in the USB drive L:\ which holds a freshly formatted micro-SD card, still empty.
- The files were NOT copied sequentially. That was the the aim, because (for obscure reasons) the SD card somehow builds its āindexā based on the order in which each file was physically copied to it
Iām stumped so any help on how I shpuld proceed would be appreciated please
Script
$sourceFolder = "C:\Users\terry\Dropbox\Electronics\Arduino\SKETCHES\MY SKETCHES\PROJECTS_Arduino\TalkingTemp Proj\_MP3 _files\Mon16Oct"
$destinationDrive = "L:\"
if (-not (Test-Path -Path $sourceFolder -PathType Container)) {
Write-Host "Source folder does not exist."
exit
}
if (-not (Test-Path -Path $destinationDrive -PathType Container)) {
Write-Host "Destination drive does not exist."
exit
}
$files = Get-ChildItem -Path $sourceFolder | Sort-Object CreationTime
foreach ($file in $files) {
$destinationPath = Join-Path -Path $destinationDrive -ChildPath $file.Name
Write-Host "Copying $($file.Name)..."
Copy-Item -Path $file.FullName -Destination $destinationPath
}
Write-Host "All files copied in order."