如果文件夹包含.txt文件,然后将文件夹复制到其他位置,如何使用Batch签出?

时间:2015-07-13 14:03:27

标签: windows batch-file

我想用Batch制作一个脚本。我想从这个脚本检查出来,如果一个文件夹包含一个文件,“list.txt”,如果它在文件夹中,我想在其他位置复制。我写了一些代码,但它没有用。有任何想法吗?

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

:loop
    for /f %%i in ('dir "C:\Users\ntosis\Desktop\Draft" /ad /o:d /s /b') do ( 
    SET a=%%i
        for /R %%a %%t in (*.txt) do if "%%~nxt"=="list.txt" SET p=%%~dpnxt
        echo !p!

        IF DEFINED %p% ( robocopy C:\Users\ntosis\Desktop\Draft\%a% C:\Users\ntosis\Desktop\Copied\%a% /MOVE /E )
)

echo Folder is empty or does not exist
timeout /t 15
goto loop

此刻的问题是第二个循环没有检查权。

2 个答案:

答案 0 :(得分:0)

这个怎么样:

for /f %%i in ('dir "C:\Users\ntosis\Desktop\Draft" /ad /o:d /s /b') do ( 
    IF EXIST "%%i\list.txt" (
        robocopy %%i C:\Users\ntosis\Desktop\Copied\ /MOVE /E
    )
)

答案 1 :(得分:0)

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "source=C:\Users\ntosis\Desktop\Draft"
    set "target=C:\Users\ntosis\Desktop\Copied"

    for /l %%t in (0) do (

        set "found="
        for /d /r "%source%" %%a in (list.txt) do if exist "%%a" (
            for %%b in ("%%~dpa.") do (
                echo Found "%%a"
                robocopy "%%~dpa." "%target%\%%~nxb" /move /e
            )
            set "found=1"
        )

        if not defined found (
            echo folder is empty or does not exist
        )
        timeout /t 15

    )