Batch file batfile计数不正确
我有一个小的.bat文件,其中有一个脚本,它计算数据库中的记录并将其保存到一个文件中。之后,我比较了文件。如果保存的记录等于导出的*.csv文件 在这种情况下,我的文件具有正确的编号。csv文件也有相同的编号,但不在变量中 第二个问题是,来自我创建的文件的数字的末尾有一个空格,但我在文件中什么也看不到Batch file batfile计数不正确,batch-file,Batch File,我有一个小的.bat文件,其中有一个脚本,它计算数据库中的记录并将其保存到一个文件中。之后,我比较了文件。如果保存的记录等于导出的*.csv文件 在这种情况下,我的文件具有正确的编号。csv文件也有相同的编号,但不在变量中 第二个问题是,来自我创建的文件的数字的末尾有一个空格,但我在文件中什么也看不到 chcp 1252 sqlcmd -S SQLSERVER\SQlINSTANZ -U User Id -P UserPass -d dbTable -s"" -Q "SET NOCOUNT ON
chcp 1252
sqlcmd -S SQLSERVER\SQlINSTANZ -U User Id -P UserPass -d dbTable -s"" -Q "SET NOCOUNT ON; SELECT COUNT(*) FROM sync_Winkey" -h -1 -o "\\SERVERNAME\g$\Path\to\counter1.txt"
@ECHO OFF
setlocal EnableDelayedExpansion
for /f "tokens=1 delims= " %%b in (\\SERVERNAME\g$\Path\to\counter1.txt) do set temp_records=%%b
::echo %temp_records%
for /l %%a in (1,1,100) do if "!temp_records:~0,-1!" == "s/" set temp_records=!temp_records:~0,-1!
setlocal EnableDelayedExpansion
for /f "tokens=4 delims== " %%c in (\\SERVERNAME\g$\Path\top\file.csv) do set /A "winkeyFile+=1"
echo %winkeyFile%
@ECHO ON
IF "%temp_records%" == "%winkeyFile% " GOTO CopyFile
goto:fail
:CopyFile
ECHO %date%
ECHO Daten wurden kopiert:
ECHO %time% Successfully copied. %winkeyFile% records copied
ECHO -----------------------------
PAUSE
goto:eof
:fail
PAUSE
goto:eof
您的代码有一些问题 1) 该空间不在文件中。空格在代码中,在变量赋值的末尾
....) do set temp_records=%%b
^ here
最好把线路改成
for /f %%b in ("\\SERVERNAME\g$\Path\to\counter1.txt") do set "temp_records=%%b"
或者,如果文件中唯一的数据是数字
<"\\SERVERNAME\g$\Path\to\counter1.txt" set /p "temp_records="
这一行后面有一个空格。因此,temp\u记录
将被设置为“somenumberSpace”
将此更改为
for /f "tokens=1 delims= " %%b in (\\SERVERNAME\g$\Path\to\counter1.txt) do set "temp_records=%%b"
::echo %temp_records%
for /l %%a in (1,1,100) do if "!temp_records:~0,-1!" == "s/" set temp_records=!temp_records:~0,-1!
我不知道你想用这条线做什么。它说:“如果除最后一个字符外,temp\u记录的内容是s/
,则删除最后一个字符,并执行100次。”
请再次注意,行上有一个尾随空格,因此。。。并添加一个空格
这意味着(如果删除了最后一个…并添加了空格),如果内容是s/?
,则将其替换为s/
-执行100次
这个值不是应该是数字吗?您没有提供示例…您可以修改您的问题并显示counter1.txt
的示例吗?另外,在您的第一个for
循环中,您使用令牌5做什么<代码>%%b
将仅捕获令牌1。感谢您对5的提示。没有必要。所以我把它拿走了。在counter1.txt中,它是从SELECT COUNT(*)语句中写入的数字,在文件33871中也是这样写入的。如果counter1.txt
包含一行数字,那么更优雅的读取方式可能是set/P“temp_records=“<\\SERVERNAME\g$\Path\to\counter1.txt
。但我看到了另一个问题。在if
语句中,请删除=
周围的空格,或者改用eq
。
for /f "tokens=1 delims= " %%b in (\\SERVERNAME\g$\Path\to\counter1.txt) do set temp_records=%%b
for /f "tokens=1 delims= " %%b in (\\SERVERNAME\g$\Path\to\counter1.txt) do set "temp_records=%%b"
::echo %temp_records%
for /l %%a in (1,1,100) do if "!temp_records:~0,-1!" == "s/" set temp_records=!temp_records:~0,-1!