批处理脚本以在文件夹中查找空文件 [英] Batch script to find an empty file in a folder

查看:207
本文介绍了批处理脚本以在文件夹中查找空文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要标识一个文件夹中的零KB文件,并将输出写入文本文件.以下是我使用批处理脚本找到的代码,我想按照以下要求自定义它们.

I have a requirement to identify a zero KB file in a folder and write the output to a text file. Below is the code I found by using batch script and I would like to customize the same as per my below requirement.

@echo off
set out=found.txt
(for /r c:\myfolderfilestosearch %%F in (*.txt) do (if %%~zF LSS 1 echo %%F)) > %out%
pause

我实际上想创建将结果写入到文件,并且前提是上述文件夹仅包含任何0kb文件,但是即使没有0KB文件,我的上述脚本也会为每个实例创建一个txt文件.

I would actually want to create & write the result to file if and only if the above folder has any of the 0kb files only, but my above script creates a txt file for every instance even if there are no 0KB files.

我认为我们可以使用 if 来实现它,但是正如我所说的,我是新手,如果有人使用脚本对其进行了指导,我将不胜感激.

I think we can implement it by using if else but as I said I am new and appreciate if someone guides on it with a script.

P.S.我也可以用powershell编写脚本.谢谢.

P.S. I am fine to have a script written in powershell as well.Thanks.

推荐答案

PowerShell

PowerShell

Get-ChildItem -Path C:\myfolderfilestsearch -Recurse -Force | Where-Object { $_.PSIsContainer -eq $false -and $_.Length -eq 0 } | Select -ExpandProperty FullName | Add-Content -Path found.txt

要为每次运行重新创建输出文件,请执行以下操作:

To recreate the output file for each run:

(Get-ChildItem -Path C:\myfolderfilestsearch -Recurse -Force | Where-Object { $_.PSIsContainer -eq $false -and $_.Length -eq 0 } | Select -ExpandProperty FullName) | Set-Content -Path found.txt

这篇关于批处理脚本以在文件夹中查找空文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆