Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7191175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:40:20+00:00 2026-05-28T19:40:20+00:00

I have the need to run a .bat file to run a SQL command

  • 0

I have the need to run a .bat file to run a SQL command on demand, It needs to have an if nested within a for and the if should repeat until true.

What I have:

@echo off
cd "%UserProfile%\Desktop\Scripting\"

FOR /f "delims=" %%a in ('type queue.txt') DO (
:loop

  IF EXIST reset.sql (

  goto loop

  ) ELSE (

  ::Create SQL command
  echo USE dbname> reset.sql
  echo EXEC dbo.sp_ResetSubscription @ClientName = '%%a'>> reset.sql
  echo EXEC dbo.sp_RunClientSnapshot @ClientName = '%%a'>> reset.sql
  sqlcmd -i "reset.sql"

  if exist reset.sql del /f /q reset.sql
  )
)
if exist queue.txt del /f /q queue.txt

This bombs out when it hits the loop, if I move :loop from where it is to within the if statement it works fine, however that isn’t much use.

What I need it to do is to keep looping until reset.sql does not exist but at the same time stay within the same iteration of the loop.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:40:21+00:00Added an answer on May 28, 2026 at 7:40 pm

    You should not ever GOTO a :label within a parenthesized block of code. See https://stackoverflow.com/a/8481978/1012053 – it deals with an IF() block, but the concept is the same for a FOR..DO() block. Performing a GOTO within a FOR..DO() will abort the remainder of the FOR iterations NOTE – a FOR /L loop will silently finish counting without actually processing the DO() clause

    This modification should give you the behavior you are looking for.

    @echo off
    cd "%UserProfile%\Desktop\Scripting\"
    
    FOR /f "delims=" %%a in ('type queue.txt') DO (
      call :waitForNoReset
      ::Create SQL command
      echo USE dbname> reset.sql
      echo EXEC dbo.sp_ResetSubscription @ClientName = '%%a'>> reset.sql
      echo EXEC dbo.sp_RunClientSnapshot @ClientName = '%%a'>> reset.sql
      sqlcmd -i "reset.sql"
      if exist reset.sql del /f /q reset.sql
    )
    if exist queue.txt del /f /q queue.txt
    exit /b
    
    :waitForNoReset
      if exist reset.sql goto :waitForNoReset
    exit /b
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a batch file that I need to run within my NSIS installer.
I have a C# program that needs to spawn a .BAT command file during
I need to create a .bat file to run java program. I have a
I have maven-plagin and need to run goal, that should automatically run before plugin.
I have a requirement where i need to open command prompt and run a
Newbie to windows scripting. I need help running the .bat file on the command
Sometimes I have a batch file like this: Action.bat : Set PATH=%PATH%;C:\Some\Folder\I\Need foo.exe Bar1
I have a BAT file in a directory D:\dir1\dir2\getpath.bat when i run the bat
I have this command that i need to run cd /d C:\leads\ssh & C:\Windows\System32\cmd.exe
I have a need to run a relatively large number of virtual machines on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.