
Windows 10 を実行しています。
あるフォルダーから別のフォルダーに多くのファイルをコピーしたいのですが、おそらくスクリプトか、それを実行できる他のソリューションが必要です。
フォルダーが 2 つあります。フォルダーにはA
次のような名前のファイルが含まれています。
00010_name0_1680x1050.jpg
00020_name11_1680x1050.jpg
00021_name222_1680x1050.jpg
00022_name300_1680x1050.jpg
宛先フォルダーにはB
次のような名前のファイルがあります:
00010_name0_1920x1200.jpg
00020_name11_1920x1200.jpg
00021_name222_1920x1200.jpg
00030_name500_1920x1200.jpg
したがって、ファイル名は、解像度に関する最後の部分を除いて同じです。
A
からまでのすべてのファイルをコピーしたいのですB
が、解像度を分ける最後の部分まで、名前の最初の部分が異なるファイルのみをコピーし_
ます。
したがって、名前を比較してコピーするかどうかを決定できる正規表現スクリプトが必要になるかもしれません。どんな簡単な解決策でも歓迎します。
答え1
次のような PowerShell スクリプトを使用できます。
# Save directory paths to vars
$dirA = C:\Some\Folder\A
$dirB = C:\Some\Folder\B
# Get file names of each found .jpg (with 1920x1200 in name)
$fileNamesB = (Get-ChildItem -Path $dirB -Filter *_1920x1200.jpg).Name
# Get prefixes of names that shouldn't be copied, by removing the resolution postfix
# % is shorthand for ForEach-Object
$prefixesB = $fileNamesB | % { $_ -replace "_1920x1200\.jpg$", '' }
$filesA = Get-ChildItem -Path $dirA -Filter *_1680x1050.jpg
# Filter files by checking their name property against our prefixes
# ? is shorthand for Where-Object
$filteredFilesA = $filesA | ? { ($_.Name -replace "_1680x1050\.jpg$", '') -NotIn $prefixesB }
# Copy the filtered files to folder B
$filteredFilesA | Copy-Item -Destination $dirB
機能の説明はコメントにあります。