从“旧"文件夹中移走所有文件文件夹移到父文件夹中,只要父文件夹中尚无文件 [英] move all files from "old" folders out into the parent as long as there are no files existing in the parent folder yet
本文介绍了从“旧"文件夹中移走所有文件文件夹移到父文件夹中,只要父文件夹中尚无文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
只要父文件夹中还没有文件,我的代码就应该将所有文件从旧"文件夹移到父文件夹中.
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
│ └───old
│ somefiles
├───2
│ └───old
│ somefiles
└───3
└───old
somefiles
└───Folder
├───1
│ │ somefiles
│ └───old
├───2
│ │ somefiles
│ └───old
└───3
│ somefiles
└───old
到目前为止,我的代码将1个文件移到了父文件中(如果还没有文件),此后它停止了,因为父文件中现在有一个现有文件.
My code so far moves 1 file out to the parent (if there are no files there yet) and after that it stops because there is now an existing file in the parent.
rem // Iterate over the changing directories:
for /D %%D in ("C:\testen\qft\*") do (
rem // Iterate over the files to process:
for %%F in ("%%~D\old\*.*") do (
rem // Actually move the files one level up:
dir /A:-D "%%~D" || move /Y "%%~F" "%%~dpF.."
)
)
我试图解决这样的问题:
I tried to solve the problem like this:
rem // Iterate over the changing directories:
for /D %%D in ("C:\testen\qft\*") do (
rem // Iterate over the files to process:
for %%F in ("%%~D\old\*.*") do (
rem // Actually move the files one level up:
dir /A:-D "%%~D" || set VAR="true"
if "%VAR%" == "true" (
move /Y "%%~F" "%%~dpF.."
dir /A:-D "%%~F" || set VAR="false"
)
)
)
但是我的代码调整中必须留有一些错误,因为它不再能正常工作.有人可以看到我的错误吗?
but I must have some mistake left in my code adjustment because it no longer works correctly. Can someone see my mistake(s)?
推荐答案
尝试中有两个问题:
- 您需要变量
VAR
的延迟扩展正在同一代码块中编写和读取它;如果将其更改为set "VAR=true"
和set "VAR="
(空),则可以使用if defined VAR
,它不需要延迟扩展; - 不应在内部
for
循环中检查当前迭代目录的内容,因此,如果已有文件,则将跳过该内容;
- 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 toset "VAR=true"
andset "VAR="
(empty), you could useif defined VAR
, which does not require delayed expansion; - 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.."
)
)
)
这篇关于从“旧"文件夹中移走所有文件文件夹移到父文件夹中,只要父文件夹中尚无文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文