我有一堆文件存储在这样的文件目录中
root_folder
-- folder1
--folder1_2
--bunch of files here
-- folder2
--folder2_2
--bunch of files here
-- folder3
--folder3_2
--bunch of files here
如您所见,我的文件位于根文件夹的 2 级文件夹中。我如何才能轻松地移动一堆文件,使目录变成这样:
root_folder
-- folder1
--bunch of files here
-- folder2
--bunch of files here
-- folder3
--bunch of files here
有没有免费软件程序可以使用?或者我可以使用命令提示符来完成此操作?非常感谢您的帮助 :)
答案1
我还提供了一个 Powershell 示例:
$source = "c:\sourceFolder"
$dirs = dir $source | Where-Object {
$_.PSIsContainer }
foreach ($folder in $dirs){
dir $folder -recurse | Where-Object { ! $_.PSIsContainer } | Move -Destination $folder -WhatIf
dir $folder -recurse | Where-Object { $_.PSIsContainer } | rd -recurse -Whatif
}
您可以将其复制并粘贴到 Powershell ISE 中。粗体如果什么开关基本上输出一次试运行。
您可以检查输出以确保它正在将文件移动到您想要的位置。删除-如果什么切换以使脚本生效。
答案2
Powershell 解决方案。使用它root_folder
(确保下面没有任何松散的文件root_folder
):
gci -R | ?{!$_.PSIsContainer} | %{mv $_.fullname $_.directory.parent}
上述操作将找到的任何文件放入该文件的父目录所在的同一目录中。
然后,删除所有现在为空的文件夹:
gci -R | ?{(gci $_) -eq $NULL} | rm
答案3
这是用于此目的的 Windows 批处理脚本。
第一个脚本将仅在与 OP 问题中显示的目录结构类似的目录结构内移动文件。
编辑脚本变量“topfolder”的值以包含目录结构的“基本文件夹”。
编辑脚本变量“subfolderprefix”的值,使其包含要在“topfolder”中搜索的“第一级”文件夹的通用前缀。在 OP 问题中,此“前缀”为“folder”,但可以根据需要将其更改为任何字符串。例如,使用“folder”作为前缀,脚本将按如下方式搜索文件夹:
top_folder
folder1
folder2
folder3
folder99
folderABC
但是,“top_folder”中的其他文件夹将被跳过,如下所示:
top_folder
somefolder
someotherfolder
将在“第二级”子文件夹中搜索文件,这些子文件夹具有父文件夹名称的通用前缀,后跟“_”(folder1\folder1_xxx
)。同样,将跳过任何其他文件夹:
top_folder
folder1
folder1_2
(Files here will be moved to "folder1")
folder1_99
(Files here will be moved to "folder1")
folder1_ABC
(Files here will be moved to "folder1")
folder2
folder2_2
(Files here will be moved to "folder2")
somesubfolder
(Files here will be skipped)
folder3
folder3_1
(Files here will be moved to "folder3")
folder3_2
(Files here will be moved to "folder3")
folder99
folder99_XYZ
(Files here will be moved to "folder99")
folderABC
folderABC_123
(Files here will be moved to "folderABC")
somefolder
(Folders here will be skipped)
someotherfolder
(Folders here will be skipped)
这是批处理脚本:
@echo off
set "topfolder=C:\Temp\SU373589\top_folder"
set "subfolderprefix=folder"
set "startdir=%CD%"
cd /d "%topfolder%"
set ctoptest=0
set cfilecount=0
for /d %%f in ("%subfolderprefix%*") do call :worklevel1 "%%~f"
if %ctoptest% EQU 0 echo There are no matching subfolders in "%topfolder%".
if %cfilecount% EQU 0 echo There were no files moved.
if %cfilecount% EQU 1 echo %cfilecount% file was moved.
if %cfilecount% GEQ 2 echo %cfilecount% files were moved.
cd /d "%startdir%"
goto :EOF
:worklevel1
set "subfolder=%~1"
set /a ctoptest+=1
echo Processing subfolder "%subfolder%" ...
set "subsubfolderprefix=%subfolder%\%subfolder%_"
set cwork1test=0
for /d %%g in ("%subsubfolderprefix%*") do call :worklevel2 "%%~g"
if %cwork1test% EQU 0 echo There are no matching subfolders in "%subfolder%"
echo.
set "subfolder="
set "subsubfolderprefix="
set cwork1test=
goto :EOF
:worklevel2
set "subsubfolder=%~1"
set /a cwork1test+=1
echo Processing subsubfolder "%subsubfolder%" ...
set cwork2test=0
for %%h in ("%subsubfolder%\*") do call :workmove "%%~h"
if %cwork2test% EQU 0 echo There are no files to move in "%subsubfolder%"
set "subsubfolder="
set cwork2test=
goto :EOF
:workmove
set "targetfile=%~1"
set /a cwork2test+=1
set /a cfilecount+=1
echo Moving file "%targetfile%" to "%subfolder%" (%cwork2test%)...
move "%targetfile%" "%subfolder%" >nul 2>&1
set "targetfile="
goto :EOF
第二个批处理脚本的工作方式类似,它会查找所有“第一级”子文件夹,并将所有文件从所有“第二级”子文件夹移动到“第一级”子文件夹。它不会查找任何与文件夹名称匹配的“前缀”。
编辑脚本变量“topfolder”的值以包含目录结构的基本文件夹。
@echo off
set "topfolder=C:\Temp\SE373589\top_folder"
set "startdir=%CD%"
cd /d "%topfolder%"
set ctoptest=0
set cfilecount=0
for /d %%f in ("*") do call :worklevel1 "%%~f"
if %ctoptest% EQU 0 echo There are no matching subfolders in "%topfolder%".
if %cfilecount% EQU 0 echo There were no files moved.
if %cfilecount% EQU 1 echo %cfilecount% file was moved.
if %cfilecount% GEQ 2 echo %cfilecount% files were moved.
cd /d "%startdir%"
goto :EOF
:worklevel1
set "subfolder=%~1"
set /a ctoptest+=1
echo Processing subfolder "%subfolder%" ...
set cwork1test=0
for /d %%g in ("%subfolder%\*") do call :worklevel2 "%%~g"
if %cwork1test% EQU 0 echo There are no matching subfolders in "%subfolder%"
echo.
set "subfolder="
set cwork1test=
goto :EOF
:worklevel2
set "subsubfolder=%~1"
set /a cwork1test+=1
echo Processing subsubfolder "%subsubfolder%" ...
set cwork2test=0
for %%h in ("%subsubfolder%\*") do call :workmove "%%~h"
if %cwork2test% EQU 0 echo There are no files to move in "%subsubfolder%"
set "subsubfolder="
set cwork2test=
goto :EOF
:workmove
set "targetfile=%~1"
set /a cwork2test+=1
set /a cfilecount+=1
echo Moving file "%targetfile%" to "%subfolder%" (%cwork2test%)...
move "%targetfile%" "%subfolder%" >nul 2>&1
set "targetfile="
goto :EOF
批处理脚本的一个更简单版本,不包含对空文件夹等的检查……:
@echo off
set "topfolder=C:\Temp\SE373589\top_folder"
set "startdir=%CD%"
cd /d "%topfolder%"
for /d %%f in (*) do (
echo Processing folder: "%%~f"
for /d %%g in ("%%f\*") do (
echo Processing sub-folder: "%%~g"
for %%h in ("%%~g\*") do (
echo Moving file: "%%~h" to "%%~f"
move "%%~h" "%%~f" >nul 2>&1
)
)
echo.
)
cd /d "%startdir%"
最后,一个没有消息的“最小”脚本,假设您已经位于正确的“基本文件夹”中:
@echo off
for /d %%f in (*) do (
for /d %%g in ("%%f\*") do (
for %%h in ("%%~g\*") do move "%%~h" "%%~f" >nul 2>&1
)
)
而且,如果您期望的话,您可以从命令提示符运行一个“单行”命令:
@for /d %f in (*) do @(for /d %g in ("%f\*") do @(for %h in ("%~g\*") do @move "%~h" "%~f" >nul 2>&1))
答案4
在资源管理器中(假设是 Windows)打开 folder1_2,选择所有文件,右键单击并选择移动到文件夹...,在对话框中选择 Folder1 ;然后删除 folder1_2(如果您愿意的话)。
对要清除的所有子文件夹重复上述操作。
不需要任何外部程序。