【问题标题】:Flattening folders programmatically以编程方式展平文件夹
【发布时间】:2015-03-26 04:35:30
【问题描述】:

我有一个文件夹,其中包含一组目录。

我想遍历此根文件夹中的所有目录,如果这些目录包含文件夹,则将它们展平(即将它们全部放入原始目录,除非该文件夹被命名为“异常”。

所以我有

>Root Folder
   >directory
     >subdirectory
      file1
     >subdirectory2
      file2
   >directory2
     >exception
      file3
   >directory3
     >subdirectory3
      file4

更改为(空行表示子目录已删除的位置):

根文件夹 目录

     file1

     file2
   >directory2
     >exception
      file3
   >directory3

     file4

每个子目录可以包含 n 个其他子目录,所以我认为这里最好使用递归解决方案,例如:

for each directory d in root folder
   find every file/folder in d not isCalled("exception")
   copy all files into directory d
   delete all folders in directory d

我不确定哪些技术可以促进在 Windows 中执行此操作,但是,有人可以帮忙吗?我怎样才能最简单地编写一个脚本来做到这一点?我已经用谷歌搜索了很长时间没有明确的答案,所以非常感谢任何帮助:)

谢谢。

【问题讨论】:

  • 你要找的是windows批处理文件

标签: windows file batch-file command-line directory


【解决方案1】:

我相信您正在寻找的是 windows 批处理。

解决方案(未测试):

Main.bat

@echo off
for /d %%a in (*) do (
  Clear "%%~a" 
  rmdir %%a
)

清除.bat

@echo off
set root=C:\Path\To\Root\
:: Edit above line. Make sure you include last "\"
cd %1
move * %root%
for /d %%d in (*) do (
  if "%%d" NEQ "exemption" (
    %root%clear "%%~d"
    rmdir %%d
  )
)

这应该可行。将这两个文件放在根目录中。如果clear 批处理文件中有错误,它应该是安全的,因为rmdir 不会清理空目录。

【讨论】:

  • 非常感谢您的回复,但是当我运行它时出现错误消息:Clear "123" rmdir 123) 'Clear' 不被识别为内部或外部命令、可运行程序或批处理文件。该系统找不到指定的文件。知道是什么原因造成的吗?
  • @SimonKiely clear.bat 和 main.bat 都在根目录下。他们都是这样命名的吗
猜你喜欢
  • 1970-01-01
  • 2017-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-07
  • 2011-01-12
相关资源
最近更新 更多