我有 2000 張圖片,我想將它們移動到不同的檔案夾中。每個檔案夾應包含 400 張影像,并且每個檔案夾位于不同的路徑中。
\Desktop\images 包含:
0001.jpeg
0002.jpeg
...
2000.jpeg
檔案應移至以下檔案夾:
\Desktop\Project\Folder1\images:
0001.jpeg
0002.jpeg
...
0400.jpeg
\Desktop\Project\Folder2\images
0401.jpeg
0402.jpeg
...
0800.jpeg
\Desktop\Project\Folder3\images:
1801.jpeg
1802.jpeg
...
1200.jpeg
\Desktop\Project\Folder4\images:
1201.jpeg
1202.jpeg
...
1600.jpeg
\Desktop\Project\Folder5\images:
1601.jpeg
1602.jpeg
...
2000.jpeg
檔案夾的圖形顯示
uj5u.com熱心網友回復:
當您使用配備有的操作系??統時 電源外殼,這里有一個替代方案,它利用了它:
分發檔案.ps1
$DesktopPath = [System.Environment]::GetFolderPath([System.Environment SpecialFolder]::Desktop)
$SourceDir = $DesktopPath "\images"
$FileMask = "*.jpeg"
$DestDir = $DesktopPath "\Project"
$FilesPerDir = 400
$DestStartNum = 1
$i = 0
Get-ChildItem -Path $SourceDir -Filter $FileMask -Name | Sort-Object | ForEach-Object {
New-Item -Path ($DestDir "\Folder" $DestStartNum) -Type Directory -Force > $Null
Move-Item ($SourceDir "\" $_) ($DestDir "\Folder" $DestStartNum)
$i
If ($i -Eq $FilesPerDir) {
$DestStartNum
$i = 0
}
}
為了繼續討論您的話題 指令 標記,您可以從命令提示符視窗運行該腳本,如下所示:
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -ExecutionPolicy RemoteSigned -File "L:\ocation\Of\DistributeFiles.ps1"
uj5u.com熱心網友回復:
另一種方法是使用支持的 Windows 系統上已有的 PowerShell。此cmd.bat 檔案腳本運行 PowerShell 腳本,該腳本將使用 .jpeg 檔案名的前四 (4) 位數字來確定應將其移動到哪個目錄。不會移動不以四 (4) 位數字開頭的檔案。
將 .bat 和 .ps1 檔案放在同一目錄中。當您對將創建正確的目錄并將檔案移動到正確的目錄感到滿意時,請-WhatIf從mkdir和Move-Item命令中洗掉。
=== 分發檔案.bat
@powershell -NoLogo -NoProfile -File "%~dp0%~n0.ps1"
=== 分發檔案.ps1
$ProjectPath = Join-Path -Path $Env:USERPROFILE -ChildPath 'Desktop\Project';
$NFilesPerDirectory = 400;
Get-ChildItem -File -Path (Join-Path -Path $Env:USERPROFILE -ChildPath 'Desktop\images') -Filter '*.jpeg' |
ForEach-Object {
# Check to see if the filename starts with four (4) digits.;
if ($_.BaseName -match '^(\d{4}).*') {
$FolderNumber = [math]::Floor([int]$Matches[1] / $NFilesPerDirectory);
$FolderName = 'Folder' $FolderNumber.ToString();
$FolderPath = Join-Path -Path $ProjectPath -ChildPath $FolderName;
# If the destination directory does not exist, create it.;
if (-not (Test-Path -Path $FolderPath)) { mkdir $FolderPath -WhatIf | Out-Null }
# Move the file to the destination directory.;
Move-Item -Path $_.FullName -Destination $FolderPath -WhatIf
}
}
如果由于某種原因,您無法使用 .ps1 腳本檔案,則可以將其編碼在 .bat 檔案腳本中。
powershell -NoLogo -NoProfile -Command ^
$ProjectPath = Join-Path -Path $Env:USERPROFILE -ChildPath 'Desktop\Project'; ^
$NFilesPerDirectory = 400; ^
Get-ChildItem -File -Path (Join-Path -Path $Env:USERPROFILE -ChildPath 'Desktop\images') -Filter '*.jpeg' ^| ^
ForEach-Object { ^
^<# Check to see if the filename starts with four (4) digits.#^> ^
if ($_.BaseName -match '^^(\d{4}).*') { ^
$FolderNumber = [math]::Floor([int]$Matches[1] / $NFilesPerDirectory); ^
$FolderName = 'Folder' $FolderNumber.ToString(); ^
$FolderPath = Join-Path -Path $ProjectPath -ChildPath $FolderName; ^
^<# If the destination directory does not exist, create it.#^> ^
if (-not (Test-Path -Path $FolderPath)) { mkdir $FolderPath -WhatIf ^| Out-Null }; ^
^<# Move the file to the destination directory.#^> ^
Move-Item -Path $_.FullName -Destination $FolderPath -WhatIf; ^
}; ^
};
uj5u.com熱心網友回復:
這是此檔案移動任務的注釋批處理檔案,其中包含一些額外內容以使其有趣(對我而言):
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "SourceFolder=%UserProfile%\Desktop\images"
if not exist "%SourceFolder%\*.jpeg" goto :EOF
set "DestinationPath=%UserProfile%\Desktop\Project"
set "DestinationName=Folder"
set "FolderNameLength=6"
set "MaxFileCount=400"
setlocal EnableDelayedExpansion
set "FolderNumber=0"
set "FileCount=%MaxFileCount%"
set "DestinationFolder=!DestinationPath!\!DestinationName!"
rem Search for all non-hidden subfolders with a name starting with the
rem destination folder name and having a number append and determine
rem the greatest folder number in all existing folder names. Folder
rem names with one or more leading zeros are not supported by this code.
for /F "delims=" %%I in ('dir "!DestinationFolder!*" /AD-H-L /B 2^>nul ^| %SystemRoot%\System32\findstr.exe /I /R "^folder[0123456789][0123456789]*$"') do (
set "FolderName=%%I"
if !FolderName:~%FolderNameLength%! GTR !FolderNumber! set "FolderNumber=!FolderName:~%FolderNameLength%!"
)
rem Determine the number of *.jpeg files in the images folder in the folder
rem with greatest folder number if there is an existing images folder at all.
if exist "!DestinationFolder!!FolderNumber!\images\" (
set "FileCount=0"
for /F "eol=| delims=" %%I in ('dir "!DestinationFolder!!FolderNumber!\images\*.jpeg" /A-D /B 2^>nul') do set /A FileCount =1
)
rem Get a list of JPEG files in source folder loaded into memory of the
rem Windows command processor ordered by name and move all these files
rem with making sure that no images folder has more than the number of
rem JPEG files as defined above. The code below is not capable moving
rem files with one or more exclamation marks because of always enabled
rem delayed expansion.
rem Note: The command DIR orders the file names strictly alphabetically
rem and not alphanumerical as done by Windows File Explorer. So
rem the JPEG file names should have all the same number of digits.
for /F "eol=| delims=" %%I in ('dir "!SourceFolder!\*.jpeg" /A-D /B /ON 2^>nul') do (
set /A FileCount =1
if !FileCount! GTR %MaxFileCount% (
set "FileCount=1"
set /A FolderNumber =1
md "!DestinationFolder!!FolderNumber!\images"
)
echo Moving file "%%I" to "!DestinationFolder!!FolderNumber!\images\"
move "!SourceFolder!\%%I" "!DestinationFolder!!FolderNumber!\images\"
if errorlevel 1 set /A FileCount-=1
)
endlocal
endlocal
此批處理檔案適用于沒有目標檔案夾中現有的所有以及現有在這種情況下的JPEG檔案與最大數量的目標檔案夾的數量被發現,以確定有多少JPEG檔案到另外移動到某個目的地檔案夾images的檔案夾FolderX數量最多。
請注意SourceFolder,DestinationPath、DestinationName、FolderNameLength和MaxFileCount必須適應當地要求。
要了解使用的命令及其作業原理,請打開命令提示符視窗,在那里執行以下命令,并仔細閱讀為每個命令顯示的所有幫助頁面。
dir /?echo /?endlocal /?findstr /?goto /?if /?md /?move /?rem /?set /?setlocal /?
Read the Microsoft documentation about Using command redirection operators for an explanation of 2>nul and |. The redirection operators > and | must be escaped with caret character ^ on the FOR command lines to be interpreted as literal character swhen Windows command interpreter processes these command lines before executing command FOR which executes the embedded dir command lines without or with findstr in a separate command process started in background with %ComSpec% /c and the command line within ' appended as additional arguments.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/312634.html
