【问题标题】:Move multiple files with the same name into new folders named with the file paths将多个同名文件移动到以文件路径命名的新文件夹中
【发布时间】:2019-01-29 11:43:26
【问题描述】:

我目前有很多同名的文件存储在许多不同的项目文件夹中。我想将这些文件移动到一个新文件夹中,并且我想用文件的原始位置命名该文件夹。

我目前有这个结构:

C:\XYZ\Folder 1\File1.txt
C:\XYZ\Folder 2\File1.txt
C:\XYZ\Folder 3\File1.txt

我希望将所有 File1.txt 文件移动到新文件夹,如下所示:

F:\Destination\C_XYZ_Folder 1\File1.txt
F:\Destination\C_XYZ_Folder 2\File1.txt
F:\Destination\C_XYZ_Folder 3\File1.txt

我发现很难找到并理解我在寻找什么。我可以移动一个文件,但除此之外,系统会提示我替换刚刚移动的文件,因为它们都具有相同的名称,并且我无法将其与创建以文件位置为名称的新文件夹相结合.

最终我要做的是将位于不同文件夹中的多个同名文件移动到新位置,但仍要注意每个文件的原始位置。重命名文件没问题,但我的文件路径很长。

【问题讨论】:

  • 您能否说明我们将如何找到这些文件?我们应该搜索父文件夹的子文件夹吗?
  • xcopy 应该这样做,如果我正确理解您的问题(保持文件夹结构):xcopy /s "C:\XYZ\file1.txt" "F:\Destination\"
  • @Stephan 不,我不认为 OP 想要这个。我想他想要driveletter_path
  • @double-beep:如果我确定,我会回答的。也许这正是 koyeti 提出的一个想法,也许这是真正的需求。我们没有办法知道。至少,xcopy 可以处理像F:\Destination\C\XYZ_Folder 1\File1.txt 这样的目的地,这可能符合他们的需求——或者不符合他们的需求。现在轮到我来澄清了。
  • 你试过什么,你卡在哪里了?请分享您的努力(edit 的问题)!否则,这篇文章是一个代码请求,这显然是题外话。请阅读tour 以及这些帮助文章:How to Askminimal reproducible example

标签: batch-file


【解决方案1】:

我假设在C:\ 中有一个文件夹XYZ,其中有很多子文件夹,其中一些(或全部)有File1.txt 来创建一个文件夹并将其移动到那里,您可能需要:

@echo off
setlocal EnableDelayedExpansion

for /R "C:\XYZ\" %%A IN (File1.txt) do (
    rem /* Find path of file excluded filename (dp=drive and path): */
    set "drive_path=%%~dpA"

    rem /* In this %%~dpA, replace '\' and ':\' according to OP's requirements: */
    set "formatted=!drive_path:\=_!" & set "formatted=!formatted::=!"

    rem /* Make the folder: */
    md "F:\Destination\!formatted!"

    rem /* Move the file there: */
    move "%%~fA" "F:\Destination\!formatted!"
)

以上代码以F:\Destination\C_XYZ_etc\File1.txt 格式生成路径。如 cmets 中所述,您可能还需要:

@echo off
setlocal EnableDelayedExpansion

for /R "C:\XYZ\" %%A IN (File1.txt) do (
    rem /* Find path of file excluded filename (dp=drive and path): */
    set "drive_path=%%~dpA"

    rem /* In this %%~dpA, replace '\' and ':\' according to OP's requirements: */
    set "formatted=!drive_path:\=!" & set "formatted=!formatted::=!"

    rem /* Make the folder: */
    md "F:\Destination\!formatted!"

    rem /* Move the file there: */
    move "%%~fA" "F:\Destination\!formatted!"
)

格式为F:\Destination\CXYZETC\File1.txt

如果您要检查多个文件:(使用set /p [来自用户的输入]):

@echo off
setlocal EnableDelayedExpansion

:files
set /p files=Please enter the files you want to check separated by spaces. Quote all filenames: 
if not defined files (goto:files)

:loop

rem Loop through user input (filenames):
for %%A IN (%files%) do (
    for /R "C:\XYZ\" %%B IN ("%%A") do (
        rem /* Find path of file excluded filename (dp=drive and path): */
        set "drive_path=%%~dpB"

        rem /* In this %%~dpB, replace '\' and ':\' according to OP's requirements: */
        set "formatted=!drive_path:\=!" & set "formatted=!formatted::=!"

        rem /* Make the folder: */
        md "F:\Destination\!formatted!"

        rem /* Move the file there: */
        move "%%~fB" "F:\Destination\!formatted!"
    )
)

带参数(更简单):

@echo off
setlocal EnableDelayedExpansion

:argument_check
if [%1] == [] (echo Action requires arguments^^! Please rerun from cmd specifying arguments^^! Remember to quote each filename^^! & exit /b 1)

:loop

rem Loop through arguments (filenames):
for %%A IN (%*) do (
    for /R "C:\XYZ\" %%B IN ("%%A") do (
        rem /* Find path of file excluded filename (dp=drive and path): */
        set "drive_path=%%~dpB"

        rem /* In this %%~dpB, replace '\' and ':\' according to OP's requirements: */
        set "formatted=!drive_path:\=!" & set "formatted=!formatted::=!"

        rem /* Make the folder: */
        md "F:\Destination\!formatted!"

        rem /* Move the file there: */
        move "%%~fB" "F:\Destination\!formatted!"
    )
)

【讨论】:

  • 这些对我有用,感谢您的清晰解释
【解决方案2】:

xcopy 具有 /s 开关以递归扫描子文件夹。以下将文件夹结构(仅存在 file1.txt 的文件夹)重新创建为 F:\Destination\C\

xcopy /s "C:\XYZ\file1.txt" "F:\Destination\C\"

副本

C:\XYZ\Folder 1\File1.txt
C:\XYZ\Folder 2\File1.txt
C:\XYZ\Folder 3\File1.txt
C:\XYZ\File1.txt
C:\XYZ\Folder 1\Subfolder\File.txt

F:\Destination\C\XYZ\Folder 1\File1.txt
F:\Destination\C\XYZ\Folder 2\File1.txt
F:\Destination\C\XYZ\Folder 3\File1.txt
F:\Destination\C\XYZ\File1.txt
F:\Destination\C\XYZ\Folder 1\Subfolder\File1.txt

【讨论】:

  • 这也适用于我添加del /s "C:\XYZ\File1.txt" 以更接近我的要求,非常感谢。
  • robocopy/mov/move 开关,但语法略有不同。
猜你喜欢
  • 2020-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-13
  • 1970-01-01
  • 2020-05-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多