Programing

Windows 배치 스크립트에서 파일 크기를 어떻게 확인할 수 있습니까?

lottogame 2020. 12. 7. 07:44
반응형

Windows 배치 스크립트에서 파일 크기를 어떻게 확인할 수 있습니까?


파일의 내용을 확인하는 배치 파일을 갖고 싶습니다 filesize.

크기가 더 크면 %somany% kbytes,GOTO를 사용하여 다른 곳으로 리디렉션해야합니다.

예:

[check for filesize]
IF %file% [filesize thing Bigger than] GOTO No
echo Great! Your filesize is smaller than %somany% kbytes.
pause
exit
:no
echo Um... You have a big filesize.
pause
exit

파일 이름이 배치 파일에 대한 매개 변수로 사용되는 경우 필요한 것은 %~z1(1은 첫 번째 매개 변수를 의미 함)

파일 이름이 매개 변수가 아닌 경우 다음과 같이 할 수 있습니다.

@echo off
setlocal
set file="test.cmd"
set maxbytesize=1000

FOR /F "usebackq" %%A IN ('%file%') DO set size=%%~zA

if %size% LSS %maxbytesize% (
    echo.File is ^< %maxbytesize% bytes
) ELSE (
    echo.File is ^>= %maxbytesize% bytes
)

나는 % ~ z1 비밀 소스에 대한 설명 때문에 @Anders 대답을 좋아합니다. 그러나 지적했듯이 파일 이름이 배치 파일의 첫 번째 매개 변수로 전달 될 때만 작동합니다.

@Anders는을 사용하여이 FOR문제를 해결했습니다. 이는 문제에 대한 1 줄짜리 훌륭한 수정이지만 읽기가 다소 어렵습니다.

대신를 사용하여 % ~ z1로 더 간단한 답으로 돌아갈 수 있습니다 CALL. 환경 변수에 파일 이름이 저장되어있는 경우 배치 파일의 루틴에 대한 매개 변수로 사용하면 % 1이됩니다.

@echo off
setlocal
set file=test.cmd
set maxbytesize=1000

call :setsize %file%

if %size% lss %maxbytesize% (
    echo File is less than %maxbytesize% bytes
) else (
    echo File is greater than or equal %maxbytesize% bytes
)
goto :eof

:setsize
set size=%~z1
goto :eof

32 비트 제한에 대한 J. Bouvrie의 우려에 대해 궁금합니다. 그가 LSS파일 크기 로직 자체 를 사용 하지 않는 문제에 대해 이야기하고있는 것 같습니다. J. Bouvrie의 우려를 다루기 위해 패딩 된 문자열 비교를 사용하도록 솔루션을 다시 작성했습니다.

@echo on
setlocal
set file=test.cmd
set maxbytesize=1000

call :setsize %file%

set checksize=00000000000000000000%size%
set checkmaxbytesize=00000000000000000000%maxbytesize%
if "%checksize:~-20%" lss "%checkmaxbytesize:~-20%" (
    echo File is less than %maxbytesize% bytes
) else (
    echo File is greater than or equal %maxbytesize% bytes
)
goto :eof

:setsize
set size=%~z1
goto :eof

%~z1배치 파일의 첫 번째 인수 크기로 확장됩니다. 보다

C:\> call /?

C:\> if /?

간단한 예 :

@ECHO OFF
SET SIZELIMIT=1000
SET FILESIZE=%~z1

IF %FILESIZE% GTR %SIZELIMIT% Goto No

ECHO Great! Your filesize is smaller than %SIZELIMIT% kbytes.
PAUSE
GOTO :EOF

:No
ECHO Um ... You have a big filesize.
PAUSE
GOTO :EOF

DOS 기능을 선호합니다. 나에게 더 깨끗한 느낌.

SET SIZELIMIT=1000
CALL :FileSize %1 FileSize
IF %FileSize% GTR %SIZELIMIT% Echo Large file

GOTO :EOF

:FileSize
SET %~2=%~z1

GOTO :EOF

당신이 경우 %file%입력 매개 변수는, 당신은 사용할 수 있습니다 %~zN경우, N매개 변수의 수입니다.

예 : test.bat함유

@echo %~z1

Will display the size of the first parameter, so if you use "test myFile.txt" it will display the size of the corresponding file.


As usual, VBScript is available for you to use.....

Set objFS = CreateObject("Scripting.FileSystemObject")
Set wshArgs = WScript.Arguments
strFile = wshArgs(0)
WScript.Echo objFS.GetFile(strFile).Size & " bytes"

Save as filesize.vbs and enter on the command-line:

C:\test>cscript /nologo filesize.vbs file.txt
79 bytes

Use a for loop (in batch) to get the return result.


Another example

  FOR %I in (file1.txt) do @ECHO %~zI

Create a one line batch file GetFileSize.bat containing

GetFileSize=%~z1

then call it

call GetFileSize  myfile.txt
if (%GetFileSize) == ()     goto No_File
if (%GetFileSize) == (0)    goto No_Data
if (%GetFileSize) GTR 1000  goto Too_Much_Data
rem Etc.

You can even create your test file on the fly to eliminate the pesky required file, note the double percent in the echo statement:

echo set GetFileSize=%%~z1 > %temp%\GetFileSize.bat
call %temp%\GetFileSize  myfile.txt
if (%GetFileSize) GTR 1000  goto Too_Much_Data
rem etc

This latter solution is antispaghetti. So nice. However, more disk writes. Check use count.


Just saw this old question looking to see if Windows had something built in. The ~z thing is something I didn't know about, but not applicable for me. I ended up with a Perl one-liner:

@echo off

set yourfile=output.txt
set maxsize=10000

perl -e "-s $ENV{yourfile} > $ENV{maxsize} ? exit 1 : exit 0"
rem if %errorlevel%. equ 1. goto abort
if errorlevel 1 goto abort

echo OK!
exit /b 0

:abort
echo Bad!
exit /b 1

This was my solution for evaluating file sizes without using VB/perl/etc. and sticking with native windows shell commands:

FOR /F "tokens=4 delims= " %%i in ('dir /-C %temp% ^| find /i "filename.txt"') do (  
    IF %%i GTR 1000000 (  
        echo filename.txt filesize is greater than 1000000  
    ) ELSE (  
        echo filename.txt filesize is less than 1000000  
    )
)  

Not the cleanest solution, but it gets the job done.


Important to note is the INT32 limit of Batch: 'Invalid number. Numbers are limited to 32-bits of precision.'

Try the following statements:

IF 2147483647 GTR 2147483646 echo A is greater than B (will be TRUE)
IF 2147483648 GTR 2147483647 echo A is greater than B (will be FALSE!)

Any number greater than the max INT32 value will BREAK THE SCRIPT! Seeing as filesize is measured in bytes, the scripts will support a maximum filesize of about 255.9999997615814 MB !


After a few "try and test" iterations I've found a way (still not present here) to get size of file in cycle variable (not a command line parameter):

for %%i in (*.txt) do (
    echo %%~z%i
)

Just an idea:

You may get the filesize by running command "dir":

>dir thing

Then again it returns so many things.

Maybe you can get it from there if you look for it.

But I am not sure.

참고URL : https://stackoverflow.com/questions/1199645/how-can-i-check-the-size-of-a-file-in-a-windows-batch-script

반응형