| Forum Home | ||||
| Press F1 | ||||
| Thread ID: 143103 | 2016-11-21 00:27:00 | Powershell help please | Tony (4941) | Press F1 |
| Post ID | Timestamp | Content | User | ||
| 1428981 | 2016-11-21 00:27:00 | Can any one tell me why this isn't working as it should? $source = "H:\WRRA\Newsletter\Newsletter archive" $dest ="C:\Users\Tony\Desktop\Output" $ct=0 $fileList = Get-ChildItem $source -file -Recurse write-host "Count before:" $ct foreach ($file in $fileList) { $ct++ copy-Item $file.fullname $dest -Force } write-host "Count after:" $ct The source contains a bunch of folders + 1 file at the top level and those folders contain more folders and files. As written above, it only copies the 1 top level file, and "Count After" = 1. If I change the the Get-childitem parameter to -directory it works as it should - all the folders get copied , with a count of 262 - which is the folder count to the deepest level. It has to be something obvious, but I'm blowed if I can see it. |
Tony (4941) | ||
| 1428982 | 2016-11-21 00:39:00 | You can only recurse directories, so what you need to do is walk the directories grabbing the files. | Kame (312) | ||
| 1428983 | 2016-11-21 00:58:00 | The help for get-childitem has an example: C:\PS>Get-Childitem -System -File -Recurse and gives this description: This command gets system files in the current directory and its subdirectories. Which would seem to imply it will get files as well as folders. I just tried exactly that command and I got heaps of "permission denied", but it did seem to be recovering files from nested folders. |
Tony (4941) | ||
| 1428984 | 2016-11-21 01:34:00 | D'oh! My bad. I actually looked in the folders and they were all empty. The files must have all got deleted while I was messing around. So the script had actually worked as it should. I had a backup (of course) and after I restored, the script worked perfectly finding 973 files. It just goes to prove the old debugging adage - "If you can't find the problem you're looking in the wrong place." So -recurse will recover files as well as folders. Thanks. |
Tony (4941) | ||
| 1 | |||||