简体   繁体   中英

Powershell - Move files in different folders every hour

Trying to move images from one folder into another folder but this needs to be scheduled every hour moving a chunk at a time, I've got the images chunked into folders with 1000 images in each folder.

Folder structure;

tobemoved\1
tobemoved\2
tobemoved\3
tobemoved\4

[etc.. up to 66]

These files are being moved into a single folder on the network \\\\images\\web\\upload

Is it possible to achieve this with a Powershell script, or any suggestions of an easy way to do this?

I wouldn't use Sleep as mentioned in the comments, this would mean you have a single long-running script with minimal option to recover from failure. If the script crashes, no more copy actions happen until it's re-launched.

instead, use Task Scheduler to run the script every hour. It's purpose built to run tasks on a schedule and has options to deal with failure and re-running.

Each task is run in its own process, so if the previous task fails it doesn't affect the next task - which still runs as normal.


No need to batch the images into folders, you can use Select-Object to do this for you.

Get-ChildItem C:\folder\source -File -Recurse | Select-Object -First 1000 | Move-Item -Destination \\images\web\upload -WhatIf

This will select the first 1000 files by name and path:

C:\folder\source\imageA.jpg
C:\folder\source\imageB.jpg
C:\folder\source\folderA\imageF.jpg
C:\folder\source\folderA\imageE.jpg
C:\folder\source\folderB\imageD.jpg
C:\folder\source\folderC\imageC.jpg

And move them all into the folder \\\\images\\web\\upload :

\\images\web\upload\imageA.jpg
\\images\web\upload\imageB.jpg
\\images\web\upload\imageC.jpg
\\images\web\upload\imageD.jpg
[etc]

Note: This assumes you're using a modern version of Powershell as the -File param isn't available in earlier versions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM