如果文件的扩展名与子目录扩展名匹配,则将文件移动到父目录

时间:2018-03-13 11:40:16

标签: batch-file cmd

我正在寻找一个批处理脚本,如果它们的扩展名与子目录扩展名匹配,它会将文件从子目录移动到其父目录。

示例:

  • .txt

    移动任何directory parent\files.txt\文件

    "parent\files.txt\test.txt"将成为"parent\test.txt"

  • .zip

    移动任何directory parent\files.zip\文件

    "parent\files.zip\test.zip"将成为"parent\test.zip"

如果文件的扩展名与其子目录名的扩展名相同,我只想move。子目录和任何其他内容必须保持不变。

这就是我现在所拥有的,但它只会删除我的文件:

@echo off
md temp
set view=
if "%view%"=="1" @echo on
color 72
mode con cols=30 lines=8
setlocal enableDelayedExpansion
set /p location_with_dirs=location: 
echo:
type nul> ".\temp\folderlist.txt"
FOR /D %%G IN ("%location_with_dirs%\*") DO (
    echo process file:
    echo %%G
    choice
    if "%errorlevel%"=="1" echo %%G >> ".\temp\folderlist.txt"
    cls
)
FOR /f "delims=" %%G in (".\temp\folderlist.txt") DO (
    for %%F in (%%G\*.*) do move /Y %%F "%location_with_dirs%\"
    rd %%G
)
del /f /q ".\temp\folderlist\*.txt"

1 个答案:

答案 0 :(得分:0)

我相信会有很多方法可以达到这个目标,其中一个:

DECLARE @BadDesign table (ID int, Col1 varchar(200), Col2 varchar(200));

INSERT @BadDesign VALUES
    (110,'450,2,50,110,600', '3,45,30,901,1001'),
    (250,'2,250,300,1', '1,33,540,900'),
    (45 ,'1,45,320', '200,444,600')    

SELECT
    B.*, Col1Value=b1.Item, Cal2Value = B2.Item
FROM
    @BadDesign B
    CROSS APPLY
    (SELECT 
       rn = ROW_NUMBER() OVER (ORDER BY (SELECT 1)), F.Item
     FROM  
        dbo.SplitStringsOrdered(B.Col1, ',') F
    ) b1
    CROSS APPLY
    (SELECT 
        rn = ROW_NUMBER() OVER (ORDER BY (SELECT 1)), F1.Item
     FROM
        dbo.SplitStringsOrdered(B.Col2, ',') F1
     ) b2
WHERE
    b1.rn = b2.rn

只需将@CD /D "Parent" 2>Nul || Exit /B @For /D %%A In (*) Do @If /I Not "%%~xA"=="" Move /-Y "%%A\*%%~xA">Nul Parent更改为您父目录的完整路径或相对路径。

正如礼貌一样,这是一个希望改进你提供的代码中更重要部分的版本,(它忽略了进度条和颜色)

1
相关问题