如何将文件从2级文件夹移动到1级文件夹


3

我有一堆文件存储在文件目录中

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

我可以使用任何免费软件程序吗?或者也许我可以使用命令提示符来完成此操作?非常感谢您的帮助 :)


什么操作系统?????
surfasb

Windows 7 32位
Nicholas Lie

Answers:


3

我还提供了一个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中。大胆 如果 开关基本上输出干运行。

您可以检查输出以确保将文件移动到您确切需要的位置。除掉 -如果 切换到使脚本生效。


我复制粘贴你的代码,它给出以下错误:在一元运算符'!'后丢失表达式。在行:5 char:39
Nicholas Lie

@NicholasLie:编辑:我没有将代码格式化为 所以一些角色没有被转移。在$之后应该有一个下划线。
surfasb

2

Powershell解决方案。使用它 root_folder (确保没有任何松散的文件直接在 root_folder ):

gci -R | ?{!$_.PSIsContainer} | %{mv $_.fullname $_.directory.parent}

上面将它找到的任何文件放入文件的父目录所在的同一目录中。

然后,删除任何现在空的文件夹:

gci -R | ?{(gci $_) -eq $NULL} | rm


0

这是一个Windows批处理脚本。

第一个脚本将仅在目录结构中移动文件,类似于OP问题中显示的文件。

编辑脚本变量“topfolder”的值以包含目录结构的“基本文件夹”。

编辑脚本变量“subfolderprefix”的值,以包含要在“topfolder”中搜索的“第一级”文件夹的公共前缀。在OP问题中,这个“前缀”是“文件夹”,但可以根据需要将其更改为任何字符串。例如,使用“folder”作为前缀,脚本将搜索如下文件夹:

top_folder
   folder1
   folder2
   folder3
   folder99
   folderABC

但是,“top_folder”中的其他文件夹将被跳过,如下所示:

top_folder
   somefolder
   someotherfolder

将搜索文件的“第二级”子文件夹具有父foldername的公共前缀,后跟“_”( 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




第二个批处理脚本将以类似的方式工作,查看所有“第一级”子文件夹,并将所有文件从所有“第二级”子文件夹移动到“第一级”子文件夹。它不会与任何foldernames的任何“前缀”匹配。

编辑脚本变量“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))

-1

在资源管理器中(假设Windows在这里)打开文件夹1_2,选择所有文件,右键单击&选择Move to Folder ...,选择Folder1 在对话中;然后删除folder1_2,如果这是你的意图。

对要清除的所有子文件夹重复上述操作。

不需要任何外部程序。


1
如果只有10个左右的文件夹就没问题。如果我有100个以上的文件夹,这对我来说很麻烦(在我的情况下是200多个文件夹)>,<
Nicholas Lie
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.