move all files from “old” folders out into the parent as long as there are no files existing in the parent folder yet

前端 未结 1 571
眼角桃花
眼角桃花 2021-01-28 22:19

My code should move all files from \"old\" folders out into the parent as long as there are no files existing in the parent folder yet.

└───Folder
    ├───1
             


        
相关标签:
1条回答
  • 2021-01-28 22:42

    There are two problems in your attempt:

    1. you need delayed expansion for the variable VAR as you are writing and reading it in the same block of code; if you change it to set "VAR=true" and set "VAR=" (empty), you could use if defined VAR, which does not require delayed expansion;
    2. the content of the currently iterated directory should not be checked in the inner for loop, so this is skipped in case there are already files;

    Here is a possible solution:

    rem // Iterate over the changing directories:
    for /D %%D in ("C:\testen\qft\*") do (
        rem // Check whether current directory contains files:
        dir /A:-D "%%~D\*.*" > nul 2>&1 || (
            rem // Iterate over the files to process:
            for %%F in ("%%~D\old\*.*") do (
                rem // Actually move the files one level up:
                move /Y "%%~F" "%%~dpF.."
            )
        )
    )
    
    0 讨论(0)
提交回复
热议问题