将具有相同名称的多个文件移动到以文件路径命名的新文件夹中

时间:2019-01-29 11:43:27

标签: batch-file

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

我目前具有以下结构:

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

我发现很难找到和理解我要寻找的东西。我可以移动一个文件,但是除此之外,由于它们都具有相同的名称,因此我会提示您替换刚刚移动的文件,而且我无法将其与创建一个以文件位置为名称的新文件夹结合使用

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

2 个答案:

答案 0 :(得分: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格式创建了路径。如评论中所述,您可能还需要:

@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!"
    )
)

答案 1 :(得分:0)

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