简体   繁体   中英

Batch Script Won't Display Variable

I have a Batch Script which when I run wont display a string variable.

@ECHO OFF
color 0c
set /a Copyed = 1
set /a File = %random%
set /a Origin = %~z1
set /a End = 0
set /a OSize = %~z1
if %~z1 LEQ 999 (
    set /a Size = %~z1
    set Z="Bytes"
)
if %OSize% GEQ 1000 (
    set /a OSize /= 1000
    set /a OEnd += 1
)
if %OSize% GEQ 1000 (
    set /a OSize /= 1000
    set /a OEnd += 1
)
if %OSize% GEQ 1000 (
    set /a OSize /= 1000
    set /a OEnd += 1
)
if %OSize% GEQ 1000 (
    set /a OSize /= 1000
    set /a OEnd += 1
)
if OEnd == 1 (set OZ="KB")
if OEnd == 2 (set OZ="MB")
if OEnd == 3 (set OZ="GB")
if OEnd == 4 (set OZ="TB")
echo ::::::::::::::::::::::: > %File%
echo :::::::::::::::::::::::
echo Original File "%1" Has A Size Of %OSize% %OZ% >> %File%
echo Original File "%1" Has A Size Of %OSize% %OZ%
pause

When I run this script using another file I get :

:::::::::::::::::::::::
Original File ""P:\Virus Testing\Fill.txt"" Has A Size Of 536
Press any key to continue . . .

In the file it creates I get this :

    ::::::::::::::::::::::: 
    Original File ""P:\Virus Testing\Fill.txt"" Has A Size Of 536 

As you can see neither of them display the varible %OZ%. How do I fix this? Thanks In Advance

Try

if %OEnd% == 1 (set OZ="KB")
if %OEnd% == 2 (set OZ="MB")
if %OEnd% == 3 (set OZ="GB")
if %OEnd% == 4 (set OZ="TB")

A few bugs in your script:

You need to initialize OEnd , not End

set /a OEnd = 0

You need a descriptor for OZ for bytes, something like

if %OEnd% == 0 (set OZ=Bytes)

And, each of the other checks needs to check the value of OEnd , not name itself:

if %OEnd% == 1 (set OZ=KB)
if %OEnd% == 2 (set OZ=MB)
if %OEnd% == 3 (set OZ=GB)
if %OEnd% == 4 (set OZ=TB)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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