简体   繁体   中英

Loop in a Windows Batch File

I'm trying to create the following batch file:

@echo off

set "list=AICourse AIStudent Teacher"
set Server="localhost"
set Username="user"
set Password="()$rDCg3st@0"

    for %%n in (%list%) do (

        Set File=c:\temp\%%n.bcp
        Set Log=c:\temp\%%n.log_exp

        ECHO Begin BCP...: %TIME% 
        ECHO waiting...

        bcp Database.dbo.%%n OUT %File% -o %Log% -S%Server% -U%Username% -P%Password% -c

        ECHO End BCP..: %TIME% 
        ECHO Log in file %Log%

    )  

However, when I try to iterate through the array, only returns the last result. How can I iterate then?

Add this at the top of your script:

SETLOCAL ENABLEDELAYEDEXPANSION

And, then change all the %Log% instances to !Log! and %File% instances to !File! .

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