【问题标题】:Moving files with the same name from different folders into folders with the same name as the files?将不同文件夹中的同名文件移动到与文件同名的文件夹中?
【发布时间】:2020-01-13 01:56:36
【问题描述】:

我有 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

【问题讨论】:

  • 您希望如何重命名同名文件?
  • edit 提出问题并将所有相关信息放在那里,而不是发布 cmets!文件夹名称是否可以在 _*min 后缀之前单独包含 _

标签: batch-file rename


【解决方案1】:

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

这是 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"

        }

    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-21
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 2019-07-11
    • 2018-12-28
    • 2014-04-06
    相关资源
    最近更新 更多