简体   繁体   中英

How to check out with Batch , if a folder contains a .txt file and then copy the folder into an other location?

I would like to make a script with Batch. I want from this script to check it out, If a folder contains a file, "list.txt", and if it is in the folder i want to make a copy in an other location. I wrote some lines of code but it's not working. Any ideas?

@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

The problem in this moment is that the second loop does not the check right.

How about this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM