1

请我需要有关我的批处理脚本的帮助。它运行得很好,但是后来当我想运行它时,它会不断重复我之前显示的结果。例如,我上周执行了一个名为运行 echo “The program at first last week”的程序,而当我今天尝试通过 echo “This is today”运行它时。它运行并显示了之前的结果“

PS 代码现在可以正常工作了.... @echo off

SETLOCAL ENABLEDELAYEDEXPANSION
set file="C:\compress.gdb"


if not exist %file% goto :EOF
FOR %%A IN (%file%) DO set size=%%~zA

>"%temp%\math.vbs" echo Set objArgs = WScript.Arguments: num = objArgs(0) : result = 0
>>"%temp%\math.vbs" echo if num ^> 1073741824 then result=1
>>"%temp%\math.vbs" echo if num ^> 6442450944 then result=2
>>"%temp%\math.vbs" echo Wscript.echo result

for /f %%a in ('cscript /nologo "%temp%\math.vbs" %size% ') do set z=%%a

del "%temp%\math.vbs"

:: z will be 2 if %size% greater than 6442450944
:: z will be 1 if %size% greater than 1073741824
:: z will be 0 if %size% less    than 1073741825

echo %z%

if %z% EQU 2 (goto EXECUTE)
if %z% EQU 0 (goto LOGOUT)
if %z% EQU 1 (goto LOGOUT)

goto :EOF

:EXECUTE

call "C:\Batch_Scripts_Sensitive\message.rtf" /max
"C:\Program Files\Embarcadero\InterBase\bin\isql" -i "C:\Batch_Scripts_Sensitive\Empty_Tables_2.sql" localhost:"C:\Program Files (x86)\GE Aviation\AB139 HGS\DB\compress.GDB" -user xxxxxxx -pass xxxxxxx

:: Create Date for Back up and Restore

set hour=%time:~0,2%
if "%hour:~0,1%" == " " set hour=0%hour:~1,1%
echo hour=%hour%
set min=%time:~3,2%
if "%min:~0,1%" == " " set min=0%min:~1,1%
echo min=%min%
set secs=%time:~6,2%
if "%secs:~0,1%" == " " set secs=0%secs:~1,1%
echo secs=%secs%

set year=%date:~-4%
echo year=%year%
set month=%date:~3,2%
if "%month:~0,1%" == " " set month=0%month:~1,1%
echo month=%month%
set day=%date:~0,2%
if "%day:~0,1%" == " " set day=0%day:~1,1%
echo day=%day%

rem use this if you want to add seconds rem set datetimef=mydate_%year%_%month%_%day%_%hour%:%min%:%secs%
set datetimef=%year%_%month%_%day%_%hour%%min%
echo datetimef=%datetimef%
mkdir "C:\Monthly-gdb-restore\%datetimef%"


copy "C:\compress.gdb" "C:\Monthly-gdb-restore\%datetimef%" /Y


:: Delete empty folder in this location %Datetime%

for /f "delims=" %%d in ('dir "C:\Monthly-gdb-restore" /s /b /ad ^| sort /r') do rd "%%d"


forfiles /P "C:\Monthly-gdb-restore"  /S /M *.gdb /D -7 /C "cmd /c del @PATH /q"

"C:\Program Files\Embarcadero\InterBase\bin\gbak"   -b "C:\compress.gdb" "C:\compress.ibk"  -user xxxxxxx -password xxxxxxx

set hour=%time:~0,2%
if "%hour:~0,1%" == " " set hour=0%hour:~1,1%
echo hour=%hour%
set min=%time:~3,2%
if "%min:~0,1%" == " " set min=0%min:~1,1%
echo min=%min%
set secs=%time:~6,2%
if "%secs:~0,1%" == " " set secs=0%secs:~1,1%
echo secs=%secs%

set year=%date:~-4%
echo year=%year%
set month=%date:~3,2%
if "%month:~0,1%" == " " set month=0%month:~1,1%
echo month=%month%
set day=%date:~0,2%
if "%day:~0,1%" == " " set day=0%day:~1,1%
echo day=%day%

rem use this if you want to add seconds rem set datetimef=mydate_%year%_%month%_%day%_%hour%:%min%:%secs%
set datetimef=%year%_%month%_%day%_%hour%%min%
echo datetimef=%datetimef%
mkdir "C:\Monthly-ibk-backup\%datetimef%"

copy "C:\compress.ibk"   "C:\Monthly-ibk-backup\%datetimef%"  /Y

:: Delete empty folder in this location %Datetime%

for /f "delims=" %%d in ('dir "C:\Monthly-ibk-backup" /s /b /ad ^| sort /r') do rd "%%d"

forfiles /P "C:\Monthly-ibk-backup"  /S /M *.ibk /D -7 /C "cmd /c del @PATH /q"

"C:\Program Files\Embarcadero\InterBase\bin\gbak"  -R "C:\compress.ibk" "C:\compress.gdb" -user xxxxxxx -password xxxxxxx

 goto FINISH


:LOGOUT

Echo " The size of the database file is not up to 6GB

goto :EOF

:FINISH

ECHO " The database for xxxxxxx  was up to 6GB OR MORE, therefore proper backup and restoration has been performed.





goto :EOF
4

2 回答 2

7

里面的数字set minbytesize=6442450944太大了。
Batch 只能处理 32 位数字。

但是当您引用数字时,将使用字符串比较函数,因此大小"9"将大于"6442450944".

要获得正确的比较,您可以在数字前加上零,在您的情况下,这很简单,因为您知道第二个数字有多长。

...
set "size=000000000000%size%"
set "size=%size:~-10%"
if "%size%" GEQ "%minbytesize%" (goto EXECUTE) 

它仍然是一个字符串比较,但它现在对数字也是正确的。

顺便提一句。这不会是你的主要问题。设置以查看问题发生的位置
可能是一个好主意。ECHO ON

您使用的是什么时间格式?
如果它在一小时内使用前缀零,那么您会遇到问题set /a hh=%hh%+100

于 2013-06-19T14:14:59.580 回答
4

如果您两次都在同一个控制台窗口中运行它,那么问题很可能是一个未初始化的变量,您会看到最后一个结果。

要使用 VBS 检查数字,请尝试将此作为代码的第一部分。留在最后goto :eof以阻止它通过比较进入以下代码。

检查最后的 if 比较以确保他们正在做你想要的。

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set file="C:\Program Files (x86)\GE Aviation\AB139 HGS\DB\AW139 DB.gdb"


if not exist %file% goto :EOF
FOR %%A IN (%file%) DO set size=%%~zA

>"%temp%\math.vbs" echo Set objArgs = WScript.Arguments: num = objArgs(0) : result = 0
>>"%temp%\math.vbs" echo if num ^> 1073741824 then result=1
>>"%temp%\math.vbs" echo if num ^> 6442450944 then result=2
>>"%temp%\math.vbs" echo Wscript.echo result

for /f %%a in ('cscript /nologo "%temp%\math.vbs" %size% ') do set z=%%a

del "%temp%\math.vbs"

:: z will be 2 if %size% greater than 6442450944
:: z will be 1 if %size% greater than 1073741824
:: z will be 0 if %size% less    than 1073741825

if %z% EQU 2 (goto EXECUTE) 
if %z% EQU 1 (goto EXECUTE) 
if %z% EQU 0 (goto LOGOUT)

goto :EOF
于 2013-06-19T14:01:04.233 回答