如何仅使用 windows 命令外壳找到文件大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5858736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I find the file size only using the windows command shell?
提问by chama
I'm trying to write a script that checks if the size of a file is greater than 0 and if so, prints "greater". I looked at thisquestion and got some good ideas. I tried to implement what the second answerer answered, but it's not working properly.
我正在尝试编写一个脚本来检查文件的大小是否大于 0,如果是,则打印“更大”。我看了这个问题,得到了一些好主意。我试图实现第二个回答者的回答,但它无法正常工作。
This is what I have so far:
这是我到目前为止:
for %%i in (*.txt) do (
set size=0
set /A size=%%~zi
echo %%i %size%
if %size% GTR 0 echo greater
)
When I try this, it keeps giving me the same size for all the files, even though I know that one of them is different. When I remove the set size=0
, if the file size is 0, it gives an error 0 was unexpected at this time.
当我尝试此操作时,它始终为所有文件提供相同的大小,即使我知道其中一个文件不同。当我删除 时set size=0
,如果文件大小为 0,则会出现错误0 was unexpected at this time.
Any ideas about what I'm doing wrong?
关于我做错了什么的任何想法?
回答by Leniel Maccaferri
Try this command:
试试这个命令:
for %%I in (*.txt) do @echo %%~znI
When I run this, I get the following result on Windows 7:
当我运行它时,我在 Windows 7 上得到以下结果:
C:\Users\Leniel\Desktop>for %I in (*.txt) do @echo %~znI
14 test1
34 test2
where 14 and 34 is the file size in bytes...
其中 14 和 34 是以字节为单位的文件大小...
回答by Joey
If you want to use an actual environment variable inside that for
block, you need delayed expansion:
如果要在该for
块内使用实际环境变量,则需要延迟扩展:
setlocal enabledelayedexpansion
for %%i in (*.txt) do (
set size=0
set /A size=%%~zi
echo %%i !size!
if !size! GTR 0 echo greater
)
Note also that you need to replace %size%
by !size!
to trigger delayed expansion instead of the normal one.
还需要注意的是需要更换%size%
的!size!
,以触发延迟扩展,而不是正常的。
The problem is that normal environment variables are expanded when a statement is parsed; the for
block is a single statement in that regard. So once the loop runs the value of size
from beforethe loop is used.
问题是在解析语句时会扩展正常的环境变量;for
在这方面,块是一个单一的语句。因此,一旦循环在使用循环之前运行size
from的值。
Delayed expansion (with !
) will expand the variables right before execution instead of when a statement is parsed so you will see the updated value from within the loop.
延迟扩展(使用!
)将在执行之前而不是在解析语句时扩展变量,因此您将在循环内看到更新的值。
回答by user2796806
robocopy /L (dir you want the size of) (C:\arbitrary folder) ./Log:(C:\arbitrary folder\robolog.txt)
robocopy /L (dir 你想要的大小) (C:\arbitrary folder) 。/Log:(C:\任意文件夹\robolog.txt)
That wil create a document that has the size findstr Bytes C:\robocopylog.txt >>getsize.txt
这将创建一个大小为 findstr Bytes C:\robocopylog.txt >>getsize.txt 的文档
You can use For /? to help navigate to the last entry of getsize.txt which should contain the size of the entire directory. You will have to handle whether it is k for Kilo m for mega bytes or g for Gigs Which can be solved since robocopy uses 0 as place holders so you would only need to get the 3rd and 4th token of the last line. 514.46 m
您可以使用 For /? 帮助导航到 getsize.txt 的最后一个条目,该条目应包含整个目录的大小。您将不得不处理是 k 表示兆字节的 Kilo m 还是 g 表示 Gigs 这可以解决,因为 robocopy 使用 0 作为占位符,因此您只需要获取最后一行的第 3 个和第 4 个标记。514.46 m
回答by Janus Troelsen
Here's how to do it in PowerShell. I hope it convinces you to upgrade to a more modern shell:
以下是如何在 PowerShell 中执行此操作。我希望它能说服您升级到更现代的 shell:
PS C:\test> Get-ChildItem *.txt | ForEach-Object { Write-Host $_.name, $_.length }
test.txt 16
test2.txt 5
回答by chama
I'm not sure why this caused the problem, but this code worked.
我不确定为什么这会导致问题,但这段代码有效。
for %%i in (*.txt) do (
echo %%i %%~zi
echo %%~zi
if %%~zi GTR 0 echo greater
)
I think it had to do with when the %size% variable was getting replaced and reset. I'd still like to replace the %%~zi
with a variable name, but I think I'll leave it if it works.
我认为这与 %size% 变量何时被替换和重置有关。我仍然想%%~zi
用变量名替换,但我想如果它有效我会留下它。