简体   繁体   English

查找包含1个文件的文件夹的批处理文件

[英]Batch file that looks for a folder that contains 1 file

i have a lot of folders with a random number of files within. 我有很多文件夹,里面有随机数量的文件。 And the most of them just have 1 file inside it, these files needs to move to the parent folder. 而且大多数文件中只有1个文件,这些文件需要移动到父文件夹中。 but i don't know how i should do it with a batch file. 但我不知道该如何使用批处理文件。

I use Windows 7 Ultimate 我使用Windows 7 Ultimate

@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /d /r "%sourcedir%" %%a IN (*) DO (
 FOR /f %%c IN ('dir /b/a-d "%%a\*.*" 2^>nul ^|find /c /v ""') DO IF %%c==1 ECHO(MOVE "%%a\*.*" "%%a\..\"
)

GOTO :EOF

You would need to change the setting of sourcedir to suit your circumstances. 您需要更改sourcedir的设置以适合您的情况。

The for /d /r finds all of the subdirectory names starting at sourcedir and assigns thenm to %%a in turn. for /d /r查找从sourcedir开始的所有子目录名称,然后依次将nm分配给%%a

Each directory is then examined for filenames only; 然后仅检查每个目录的文件名; the 2>nul suppresses error messages for empty directories, and the output of the dir command is fed to find which counts (/c) the number of lines which don't match "" (ie. counts the lines of directory = # files returned). 2>nul禁止显示空目录的错误消息,并提供dir命令的输出以find 匹配""的行数(/ c)(即计算目录的行数=#个文件)回来)。 This is applied to %%c 这适用于%%c

If %%c is 1, then move all (one) files from the directory to its parent. 如果%%c为1,则将所有(一个)文件从目录移动到其父目录。

The required MOVE commands are merely ECHO ed for testing purposes. 所需的MOVE命令仅仅ECHO编用于测试目的。 After you've verified that the commands are correct , change ECHO(MOVE to MOVE to actually move the files. Append >nul to suppress report messages (eg. 1 file moved ) 确认命令正确无误后 ,将ECHO(MOVE更改为MOVE即可实际移动文件。Append >nul可禁止报告消息(例如,已1 file moved

This solution don't use any external .exe command, so it should run faster. 此解决方案不使用任何外部.exe命令,因此应运行得更快。 Also, it is clear enough to be understood with no problems, I think... 另外,我认为它很明显可以毫无问题地理解。

@echo off
setlocal EnableDelayedExpansion

for /D %%d in (*) do (
   set "firstFile="
   set "moreThanOneFile="
   for %%f in ("%%d\*.*") do (
      if not defined firstFile (
         set "firstFile=%%f"
      ) else (
         set "moreThanOneFile=true"
      )
   )
   if not defined moreThanOneFile move "!firstFile!" "%%d"
)

You have a number of folders that all may or may not contain files within them, and you want to move all files to some other directory? 您有许多文件夹,其中所有文件夹可能包含或可能不包含文件,并且要将所有文件移动到其他目录吗?

PowerShell is the new and improved super charged version of using a batch file, and this is how you'll do it. PowerShell是使用批处理文件的新的和改进的超级收费版本,这是您将执行的操作。

  1. Run PowerShell 运行PowerShell
  2. paste in the following 粘贴在下面

    dir -Recurse C:\\FolderContainingManySubfolder | dir-递归C:\\ FolderContainingManySubfolder | ? PSIsContainer | PSIsContainer | dir -Recurse | dir-递归| move-item -Destination T:\\somePath -WhatIf move-item-目的地T:\\ somePath -WhatIf

Replace C:\\FolderContainingManySubfolder with the folder that has all of those other folders with one or two items each, and replace T:\\somePath with the place you want the single files to go. 将C:\\ FolderContainingManySubfolder替换为包含所有其他文件夹且每个文件夹都包含一个或两个项目的文件夹,然后将T:\\ somePath替换为您要放置单个文件的位置。

Run it, and you'll see a lot of 'What If' output, which shows you what would happen if you were to run the command. 运行它,您将看到很多“如果...”输出,向您显示如果运行该命令会发生什么。 If you're happy with what you see, then remove the -WhatIf parameter from the end. 如果对所看到的内容满意,则从末尾删除-WhatIf参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM