仅用于文件大小的 Windows 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/483864/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:59:16  来源:igfitidea点击:

Windows command for file size only

windowscommand-line

提问by Liam

Is there a Windows command that will output the size in bytes of a specified file like this?

是否有 Windows 命令可以像这样输出指定文件的大小(以字节为单位)?

> filesize test.jpg
65212

I know that the dircommand outputs this information, but it outputs other information also.

我知道该dir命令输出此信息,但它也输出其他信息。

I could easily write such a program, but I would prefer to use a native Windows command if possible, or only what is available in a fresh install of Windows XP.

我可以轻松编写这样的程序,但如果可能,我更愿意使用本机 Windows 命令,或者仅使用全新安装的 Windows XP 中可用的命令。

回答by Mike Houston

If you are inside a batch script, you can use argument variable tricks to get the filesize:

如果您在批处理脚本中,则可以使用参数变量技巧来获取文件大小:

filesize.bat:

文件大小.bat:

@echo off
echo %~z1

This gives results like the ones you suggest in your question.

这给出的结果类似于您在问题中建议的结果。

Type

类型

help call

at the command prompt for all of the crazy variable manipulation options. Also see this articlefor more information.

在所有疯狂的变量操作选项的命令提示符下。另请参阅此文章以获取更多信息。

Edit:This only works in Windows 2000 and later

编辑:这只适用于 Windows 2000 及更高版本

回答by Patrick Cuff

If you don't want to do this in a batch script, you can do this from the command line like this:

如果您不想在批处理脚本中执行此操作,您可以从命令行执行此操作,如下所示:

for %I in (test.jpg) do @echo %~zI

Ugly, but it works. You can also pass in a file mask to get a listing for more than one file:

丑陋,但它的工作原理。您还可以传入文件掩码以获取多个文件的列表:

for %I in (*.doc) do @echo %~znI

Will display the size, file name of each .DOC file.

将显示每个 .DOC 文件的大小、文件名。

回答by David Doumèche

Use a function to get rid off some limitation in the ~zoperator. It is especially useful with a forloop:

使用函数来摆脱~z操作符中的一些限制。它对for循环特别有用:

@echo off
set size=0
call :filesize "C:\backup120714-0035\error.log"
echo file size is %size%
goto :eof

:: Set filesize of first argument in %size% variable, and return
:filesize
  set size=%~z1
  exit /b 0

回答by David Doumèche

Try forfiles:

尝试forfiles

forfiles /p C:\Temp /m file1.txt /c "cmd /c echo @fsize"

The forfilescommand runs command cfor each file min directory p.

forfiles命令cmdirectory 中的每个文件运行命令p

The variable @fsizeis replaced with the size of each file.

该变量@fsize被替换为每个文件的大小。

If the file C:\Temp\file1.txtis 27 bytes, forfilesruns this command:

如果文件C:\Temp\file1.txt为 27 字节,则forfiles运行以下命令:

cmd /c echo 27

Which prints 27to the screen.

打印27到屏幕上。

As a side-effect, it clears your screen as if you had run the clscommand.

作为副作用,它会像运行cls命令一样清除屏幕。

回答by Scott Weinstein

Since you're using Windows XP, Windows PowerShell is an option.

由于您使用的是 Windows XP,因此可以选择 Windows PowerShell。

(Get-Item filespec ).Length 

or as a function

或者作为一个函数

function Get-FileLength { (Get-Item $args).Length }
Get-FileLength filespec

回答by Kanagavelu Sugumar

C:\>FORFILES  /C "cmd /c echo @fname @fsize"


C:\>FORFILES  /?

FORFILES [/P pathname] [/M searchmask] [/S]
         [/C command] [/D [+ | -] {MM/dd/yyyy | dd}]

Description:
    Selects a file (or set of files) and executes a
    command on that file. This is helpful for batch jobs.

Parameter List:
    /P    pathname      Indicates the path to start searching.
                        The default folder is the current working
                        directory (.).

回答by Nikita G.

This is not exactly what you were asking about and it can only be used from the command line (and may be useless in a batch file), but one quick way to check file size is just to use dir:

这不是您要问的确切内容,它只能从命令行使用(并且可能在批处理文件中无用),但是检查文件大小的一种快速方法就是使用dir

> dir Microsoft.WindowsAzure.Storage.xml

Results in:

结果是:

Directory of C:\PathToTheFile

08/10/2015  10:57 AM         2,905,897 Microsoft.WindowsAzure.Storage.xml
               1 File(s)      2,905,897 bytes
               0 Dir(s)  759,192,064,000 bytes free

回答by user5814704

Create a file named filesize.cmd(and put into folder C:\Windows\System32):

创建一个名为filesize.cmd 的文件(并放入文件夹C:\Windows\System32):

@echo %~z1

回答by XFlak

In a batch file, the below works for local files, but fails for files on network hard drives

在批处理文件中,以下适用于本地文件,但不适用于网络硬盘驱动器上的文件

for %%I in ("test.jpg") do @set filesize=%~z1

However, it's inferior code, because it doesn't work for files saved on a network drive (for example, \\Nas\test.jpgand \\192.168.2.40\test.jpg). The below code works for files in any location, and I wrote it myself.

但是,它是劣质代码,因为它不适用于保存在网络驱动器上的文件(例如,\\Nas\test.jpg\\192.168.2.40\test.jpg)。下面的代码适用于任何位置的文件,我自己写的。

I'm sure there are more efficient ways of doing this using VBScript, or PowerShell or whatever, but I didn't want to do any of that; good ol' batch for me!

我确信使用VBScript或 PowerShell 或其他任何方法可以更有效地执行此操作,但我不想这样做;对我来说很好!

set file=C:\Users\Admin\Documents\test.jpg
set /a filesize=
set fileExclPath=%file:*\=%

:onemoretime
set fileExclPath2=%fileExclPath:*\=%
set fileExclPath=%fileExclPath2:*\=%
if /i "%fileExclPath%" NEQ "%fileExclPath2%" goto:onemoretime

dir /s /a-d "%workingdir%">"%temp%\temp.txt"
findstr /C:"%fileExclPath%" "%temp%\temp.txt" >"%temp%\temp2.txt"

set /p filesize= <"%temp%\temp2.txt"

echo set filesize=%%filesize: %fileExclPath%%ext%=%% >"%temp%\temp.bat"
call "%temp%\temp.bat"

:RemoveTrailingSpace
if /i "%filesize:~-1%" EQU " " set filesize=%filesize:~0,-1%
if /i "%filesize:~-1%" EQU " " goto:RemoveTrailingSpace

:onemoretime2
set filesize2=%filesize:* =%
set filesize=%filesize2:* =%
if /i "%filesize%" NEQ "%filesize2%" goto:onemoretime2

set filesize=%filesize:,=%
echo %filesize% bytes

SET /a filesizeMB=%filesize%/1024/1024
echo %filesizeMB% MB

SET /a filesizeGB=%filesize%/1024/1024/1024
echo %filesizeGB% GB

回答by klyde

In PowerShell you can do:

在 PowerShell 中,您可以执行以下操作:

$imageObj = New-Object System.IO.FileInfo("C:\test.jpg")    
$imageObj.Length