I am a newbie in batch scripting and need your help, as all the already answered questions don't really fix my problem.
我是批处理脚本的新手,需要你的帮助,因为所有已经回答的问题并没有真正解决我的问题。
I have a folder structure that contains either a parent folder with a sub-folder and a file, parent-folder with an empty sub-folder, an empty parent-folder or an parent folder with only a file. This is all in one main folder. So it can look like this:
我有一个文件夹结构,包含一个带有子文件夹和文件的父文件夹,一个带有空子文件夹的父文件夹,一个空的父文件夹或一个只有一个文件的父文件夹。这都在一个主文件夹中。所以它看起来像这样:
└───Main-Folder
├───Parent-folder 1
│ └───Sub-folder 1 (empty)
├───Parent-folder 2
│ └───Sub-folder 2
│ File 2
│
├───Parent-folder 3
│ File 3
│
└───Parent-folder 4 (empty)
Basically what I would need is that IF there is a file in a sub-folder it will be moved up one level to the respective parent-folder. All empty sub-folders should be deleted. All empty parent-folders should remain untouched. All files already in parent-folders should remain untouched.
基本上我需要的是,如果子文件夹中有一个文件,它将向上移动一级到相应的父文件夹。应删除所有空子文件夹。所有空的父文件夹应保持不变。已存在于父文件夹中的所有文件应保持不变。
2 个解决方案
#1
1
Use two stacked for /d
to
使用两个堆叠for / d to
- first iterate parent
%%P
folders and a - 2nd for the subfolders
%%S
- The Move and RD commands output and error output is redirected to null to suppress messages
首先迭代父%% P文件夹和a
子文件夹%% S的第二个
Move和RD命令输出和错误输出被重定向到null以抑制消息
:: Q:\Test\2018\07\16\SO_51362593.cmd
@Echo off
Pushd "A:\Main-folder" || Exit /B 1
For /d %%P in (*) Do (
For /d %%S in ("%%P\*") Do (
Move "%%~fS\*" "%%~fP\" 2>&1>Nul
RD /S /Q "%%~fS" 2>&1>Nul
)
)
PopD
Sample tree /f
after running the batch:
运行批处理后的样本树/ f:
> tree /f
Auflistung der Ordnerpfade für Volume RamDisk
└───Main-Folder
├───Parent-folder 1
├───Parent-folder 2
│ File 2
│
├───Parent-folder 3
│ File 3
│
└───Parent-folder 4 (empty)
#2
0
Something like that should work with your exemple :
像这样的东西应该适用于你的例子:
REM for each parent folder in your main folder
for /F %%i in ('dir /A:d /b') DO (
REM for each subfolder
for /F %%j in ('dir /A:d /b "%%i"') DO (
copy "%%i\%%j\*.*" "%%i"
rd /s "%%i\%%j"
)
)
The script must be in your main folder
该脚本必须位于您的主文件夹中
#1
1
Use two stacked for /d
to
使用两个堆叠for / d to
- first iterate parent
%%P
folders and a - 2nd for the subfolders
%%S
- The Move and RD commands output and error output is redirected to null to suppress messages
首先迭代父%% P文件夹和a
子文件夹%% S的第二个
Move和RD命令输出和错误输出被重定向到null以抑制消息
:: Q:\Test\2018\07\16\SO_51362593.cmd
@Echo off
Pushd "A:\Main-folder" || Exit /B 1
For /d %%P in (*) Do (
For /d %%S in ("%%P\*") Do (
Move "%%~fS\*" "%%~fP\" 2>&1>Nul
RD /S /Q "%%~fS" 2>&1>Nul
)
)
PopD
Sample tree /f
after running the batch:
运行批处理后的样本树/ f:
> tree /f
Auflistung der Ordnerpfade für Volume RamDisk
└───Main-Folder
├───Parent-folder 1
├───Parent-folder 2
│ File 2
│
├───Parent-folder 3
│ File 3
│
└───Parent-folder 4 (empty)
#2
0
Something like that should work with your exemple :
像这样的东西应该适用于你的例子:
REM for each parent folder in your main folder
for /F %%i in ('dir /A:d /b') DO (
REM for each subfolder
for /F %%j in ('dir /A:d /b "%%i"') DO (
copy "%%i\%%j\*.*" "%%i"
rd /s "%%i\%%j"
)
)
The script must be in your main folder
该脚本必须位于您的主文件夹中