将具有相同名称的文件从不同的文件夹移动到与文件具有相同名称的文件夹中?

时间:2019-09-11 08:54:59

标签: batch-file rename

我有13个文件夹,每个文件夹有36个磁贴。

文件夹被命名为:

NAMEFOLDER_60min
NAMEFOLDER_75min
...
NAMEFOLDER_240min

图块是根据其坐标命名的

tile_x001_y001.tif to tile_x001_y006.tif
tile_x002_y001.tif to tile_x002_x006.tif
...
tile_x006_y001.tif to tile_x006_y006.tif

此刻,图片按时间点分组在文件夹中,但我想按坐标将它们分组。例如,将所有文件夹中的每个tile_x001_y001.tif都移动到名为tile_x001_y001的新文件夹中,然后再移动所有图片。为了防止覆盖,我相信磁贴必须重命名为例如。在文件夹“ tile_x001_y001”中:tile_x001_y001_60min.tif,tile_x001_y001_75min.tif,...,tile_x001_y001_240min.tif

我尝试制作新文件夹,并尝试了一些我在网上找到的用于将图片移动/复制到相应文件夹的脚本,但是失败了,因为我尝试的脚本使用了一个包含所有图片的文件夹,但是当我移动/复制时有多个文件夹新文件夹中有多个相同名称的图片,它将覆盖它。

@echo off
for /l %%y in (1,1,6) do for /l %%x in (1,1,6) do (md tile_x00%%x_y00%%y)

@echo off
setlocal enabledelayedexpansion
for %%A in (*.tif) do (
   echo file found  %%A
   for /f "delims=" %%B in ("%%A") do set fname=%%~nB
   for /f "delims=" %%C in ("%%A") do set fextn=%%~xC
   for /f "tokens=* delims=_" %%D in ("!fname!") do set folname=%%D
   echo folder name !folname!
   if not exist "!folname!" (
      echo Folder !folname! does not exist, creating
      md "!folname!"
   ) else (
      echo Folder !folname! exists
   )
   echo Moving file %%A to folder !folname!
   move "%%A" "!folname!"
   )
echo Finished
pause

1 个答案:

答案 0 :(得分:0)

编辑:添加了每个复制文件的重命名。

这是Powershell中的建议

根据需要替换路径。我也将其复制而不是移动文件,如果一切顺利,您可以稍后再清理源代码...

$folders = Get-ChildItem -Path "D:\source" -Recurse -Directory

foreach ($folder in $folders) {

    $files = Get-ChildItem -Path "D:\source\$($folder.name)" -File

    $atfn = $folder.Name
    $atfn = $atfn.Substring(11)

    foreach ($file in $files) {

        $folderName = $file.Name
        $folderName = $folderName.Substring(0,$folderName.Length-4)

        if (!(Test-Path -Path D:\destination\$($folderName))) {

            Write-Host "Directory does not exist - Creating Directory"
            New-Item -ItemType Directory -Path "D:\destination" -Name $folderName
            Write-Host "Copying file..."
            Copy-Item -Path "D:\source\$($folder.name)\$($file.Name)" -Destination "D:\destination\$($folderName)" -Force
            Rename-Item -Path "D:\destination\$($folderName)\$($file.Name)" -NewName "$($folderName)_$($atfn).tif"

        }

        else {

            Write-Host "Copying file..."
            Copy-Item -Path "D:\source\$($folder.name)\$($file.name)" -Destination "D:\destination\$($folderName)" -Force
            Rename-Item -Path "D:\destination\$($folderName)\$($file.Name)" -NewName "$($folderName)_$($atfn).tif"

        }

    }

}