只要父文件夹中不存在文件,我的代码就应该将所有文件从“旧”文件夹移到父文件夹中。
└───Folder
├───1
│ └───old
│ somefiles
├───2
│ └───old
│ somefiles
└───3
└───old
somefiles
└───Folder
├───1
│ │ somefiles
│ └───old
├───2
│ │ somefiles
│ └───old
└───3
│ somefiles
└───old
到目前为止,我的代码将 1 个文件移出到父级(如果那里还没有文件),然后它会停止,因为现在父级中有一个现有文件。
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.."
)
)
我试图解决这样的问题:
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"
)
)
)
但是我的代码调整中一定有一些错误,因为它不再正常工作。有人可以看到我的错误吗?
你的尝试有两个问题:
VAR
;如果将其更改为set "VAR=true"
和set "VAR="
(空),则可以使用if defined VAR
,它不需要延迟扩展;for
循环中检查当前迭代目录的内容,因此如果已经有文件,则跳过此项;这是一个可能的解决方案:
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.."
)
)
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句