2016-08-16 78 views
0

我有一个非常简单的批处理文件,它运行SQL查询文件并将结果复制到打印机。即使查询结果为0行,也会打印问题。如果有数据,我怎么才能打印?批处理文件中的条件打印

cd stock 
sqlcmd -i testquery.sql -S localhost -U User -P password -o testresults.txt 
copy testresults.txt \\printserver\share 
sqlcmd -i ems_update.sql -S localhost -U User -P password 
del c:\stock\testresults.txt 
exit 

好吧,这是我现在有:

cd c:\stock 
sqlcmd -i testquery.sql -S localhost -U User -P password -o testresults.txt 
@find /c /i "0 rows" "C:\stock\testresults.txt" > NUL 
if %ERRORLEVEL% EQU 1 (
pause 
) else (
copy c:\stock\testresults.txt \\printserver\share) 
pause 
sqlcmd -i ems_update.sql -S localhost -U User -P password 

我不能复制命令立即执行。我在想什么? 感谢

回答

0

就拿你知道有0数据的文件。
for /f "delims=" %%A in ('type nodata.txt') do set nodata=%%A
然后
for /f "delims=" %%A in ('type testresults.txt') do set test_result=%%A
然后
if not %nodata%==%test_result% copy testresults.txt \printserver\share
这只有在完全相同的任何数据文件,它就像没有时间戳工作。

使用find返回%errorlevel%==1如果没有找到文件和短语0如果它。

+0

批'if'比较仅检查字符串值,以便'if'语句在100%的时间内为假。批量中的变量也被称为'%this%'。 – SomethingDark

+0

'set var_name = type contents.txt'将生成一个包含文件内容的字符串。 – MotKohn

+0

不,你需要一个'for'循环来处理命令的输出。 'for/f“delims =”%% A in('type contents.txt')do set var_name = %% A'会将变量设置为文件的内容。 – SomethingDark

0

只是一个想法 - 你可以检查文件的大小为0在bash这将是这样的:

if [ ! -s ./testresults.txt ] ; then 
    rm ./testresults.txt 
    # or do something else 
else 
    copy testresults.txt \\printserver\share 
fi 
+0

与文件大小为1kb的问题即使查询返回0行因为它将该文件写入该文件。我用数据返回的结果通常也只有1kb。 – user1879417

相关问题