Run all SQL files in multiple directories - sql-server

I combined a couple of solutions I found online to try and make this happen.
https://stackoverflow.com/a/6504317/2471473
https://sqlandme.com/2013/03/25/sql-server-executing-multiple-script-files-using-sqlcmd/
I'm trying to run a single .cmd script (Script1.cmd) with folder locations of .sql files. That single script runs another script (Script2.cmd) to use sqlcmd to execute all the .sql files in that folder location. It mostly works, but it leaves a command window open that I have to exit from for each folder location.
Script1.cmd
start Script2.cmd "C:\Location1"
start Script2.cmd "C:\Location2"
Script2.cmd
#Echo Off
FOR /f %%i IN ('DIR %1\*.Sql /B') do call :RunScript %1 %%i
GOTO :END
:RunScript
Echo Executing Script: %2
cd %1
SQLCMD -S Server123 -d Database456 -E -i %2
Echo Completed Script: %2
:END

Official command line reference for Windows XP or for Windows Server 2003, Windows Vista (and above) seems to be too brief. Read this (extended) start command documentation:
Syntax: START "title" [/D path] [options] "command" [parameters]
Always include a TITLE this can be a simple string like "My Script" or
just a pair of empty quotes "". According to the Microsoft
documentation, the title is optional, but depending on the other
options chosen you can have problems if it is omitted.
If command is an internal cmd command or a batch file then the command
processor is run with the /K switch to cmd.exe. This means that the
window will remain after the command has been run.
Next Script1.cmd should work and close started command windows:
start "" cmd /C Script2.cmd "C:\Location1"
start "" cmd /C Script2.cmd "C:\Location2"

Related

How to change current directory inside IF statement and reference its path with environment variable CD?

I'm trying to install an app from my PC running Windows 10 using adb and I need to use command cd before I'm installing the app.
But I see a strange behavior. If I use the cd command before the if statement, it's working well. But if I use the cd command inside the if statement, it is not working.
Here is what I tried:
This one is working well:
cd %~f0\..\..\apps\app_sample\samples\sample_app\build\outputs\apk
if "%arg1%" == "aaa" (
adb install %CD%\sample_app-debug.apk
)
This one results on execution in an error:
if "%arg1%" == "aaa" (
cd %~f0\..\..\apps\app_sample\samples\sample_app\build\outputs\apk
adb install %CD%\sample_app-debug.apk
)
The error output is:
Failed to stat
C:\Users\ntuser\Documents\workspace\Team\script\sample_app-debug.apk:
No such file or directory
It looks like it did not do the cd command in this case.
Whenever Windows command interpreter cmd.exe encounters the beginning of a command block starting with ( and ending with matching ), the entire block is preprocessed before the command left to the command block is executed at all. This means all environment variables using %variable% syntax are replaced by current value of the referenced environment variable before executing the command which later executes the entire command block. This behavior can be seen on running a batch file from within a command prompt window without #echo off being usually at top of the batch file.
This means on your batch file with the IF command and the command block to execute on condition being true that %CD% is replaced by current directory before the command IF is executed and so also before the command CD is executed at all.
There are two possibilities:
explicitly enable delayed environment variable expansion and reference the environment variable in the command block with using syntax !variable!
or
don't use a command block by using GOTO or a subroutine and CALL.
Solution 1 with enabled delayed environment variable expansion:
setlocal EnableDelayedExpansion
if "%arg1%" == "aaa" (
cd "%~dp0..\apps\app_sample\samples\sample_app\build\outputs\apk"
adb.exe install "!CD!\sample_app-debug.apk"
)
endlocal
Solution 2 with not using a command block at all and instead use GOTO:
if "%arg1%" == "aaa" goto AAA
rem Other commands
goto :EOF
:AAA
cd "%~dp0..\apps\app_sample\samples\sample_app\build\outputs\apk"
adb.exe install "%CD%\sample_app-debug.apk"
goto :EOF
rem Or use a different GOTO to continue processing somewhere else in batch file.
Another solution 2 is using a subroutine and command CALL:
if "%arg1%" == "aaa" call :AAA
rem Other commands executed even if above condition is true.
rem Avoid a fall through to the subroutine below.
goto :EOF
:AAA
cd "%~dp0..\apps\app_sample\samples\sample_app\build\outputs\apk"
adb.exe install "%CD%\sample_app-debug.apk"
rem Exit the subroutine and continue on line below calling it.
goto :EOF
But I think your code can be even more easier because it is most likely not necessary to specify the *.apk file with full path.
if "%arg1%" == "aaa" (
cd "%~dp0..\apps\app_sample\samples\sample_app\build\outputs\apk"
adb.exe install sample_app-debug.apk
)
And perhaps also working is:
if "%arg1%" == "aaa" adb.exe install "%~dp0..\apps\app_sample\samples\sample_app\build\outputs\apk\sample_app-debug.apk"
It is better to use here %~dp0 which references the drive and path of the batch file ending with a backslash instead of %~f0 which is the name of the batch file with file extension and full path.
For example with batch file being C:\Temp\Test\Install.bat the command line
cd "%~f0\..\..\apps\app_sample\samples\sample_app\build\outputs\apk"
results in execution of
cd "C:\Temp\Test\Install.bat\..\..\apps\app_sample\samples\sample_app\build\outputs\apk"
and the current directory is C:\Temp\apps\app_sample\samples\sample_app\build\outputs\apk after this command with in real invalid directory name Install.bat in path which is not a directory.
The command line
cd "%~dp0..\apps\app_sample\samples\sample_app\build\outputs\apk"
is much better as it results in execution of
cd "C:\Temp\Test\..\apps\app_sample\samples\sample_app\build\outputs\apk"
which makes also C:\Temp\apps\app_sample\samples\sample_app\build\outputs\apk the current directory, but with a completely correct path in comparison to above cd command line.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /? ... explains %~dp0 and %~f0.
cd /?
endlocal /?
goto /?
if /?
rem /?
set /? ... explains delayed environment variable expansion on an IF and a FOR example.
setlocal /?
I don't have adb installed. So I can only assume that it is an executable with file extension .exe and not a batch file with file extension .bat or .cmd which would require the usage of call adb.bat ... or call adb.cmd ... in this batch file.

Batch file that run jar file on cmd

Apologies if duplicate, I want to run a jar file from command prompt, So instead of going to the specific path can i create a batch file,
Simply double click on batch file will do my task,
I found something at [1]: Run a Command Prompt command from Desktop Shortcut but it do not work in my case, I want to run java -jar squirrel-sql.jar
I would like to make a batch file that:
1)Opens cmd.exe
2)Within that Command Prompt runs java -jar squirrel-sql.jar which is present on desktop
3)Leaves the window open so that I can run additional commands if I wish to
How can I do this?
Save the following as squirrel.bat:
java -jar squirrel-sql.jar
REM /P causes SET to prompt and wait for Enter.
REM The underscore is used to ensure that "sure" is not already
REM an environment variable.
set /p _sure="Do you want to exit? [press Enter]"
REM /I causes a case-insensitive comparison
if /I NOT "_sure"=="y" (
REM /B exits the batch script but not the CMD window
exit /b
) else (
REM Any other modifications
)

Problems with Ampersand start as “Run as administrator”

I wonder how do I run a .bat or .cmd file as an administrator within a folder where the name contains the same Ampersand "&"? every time I try to run the file appears an error ... at what I did when I try to run as administrator cmd understand that & the folder name is a command.
Let me illustrate, I have a folder called Projects in Cmd & vbs and a file called Project 01.bat when I try to run it as administrator until an error occurs q I remove it from the folder that contains the "&"
if the Cmd & vbs.
Folder name: Cmd & Vbs
Script name: Project 01.bat
Form of execution: Option “Run as administered” in the context menu
Version of windows: 7
Example Script:
#Echo Off
Title Remover - [WMP] Buy online music
Color 4f
reg delete "HKEY_CLASSES_ROOT\SystemFileAssociations\Directory.Audio\shellex\ContextMenuHandlers\WMPShopMusic" /f>nul 2>&1
echo msgbox"Removed!",vbinformation> %temp%\Msg.vbs
start /wait %temp%\Msg.vbs>nul 2>&1
Del %temp%\Msg.vbs>nul 2>&1
Exit
Bill is correct, for DOS to work with the & character, you need to escape it using ^ an example might help:
C:\Temp>cd my&test
The system cannot find the path specified.
'test' is not recognized as an internal or external command,
operable program or batch file.
C:\Temp>cd my^&test
C:\Temp\my&test>
I discovered that this problem is in runas configuration and the correction is below:
Browse the regedit to:
HKEY_CLASSES_ROOT\batfile\shell\runas\command
HKEY_CLASSES_ROOT\cmdfile\shell\runas\command
Changes the value:
%SystemRoot%\System32\cmd.exe /C "%1" %*
To:
%SystemRoot%\System32\cmd.exe /C ""%1"" %*

Batch file command not working

I am trying to run this batch file remotely It will kill the IE process's but when I try to open a .lnk file it won't do it. When I go onto that machine, open up the command prompt and type in the command to run the .lnk file it works with no issues.. please help!
Code to remotely execute batch file:
psexec -u Administrator -p password -i -d \\hostname "c:\Emergency_POD\test.bat"
Code on machine to run: (Only the taskill command works.. not the for command)
cd/
taskkill /im iexplore.exe /f
for %a in ("C:\Emergency_POD\*.lnk") do #start "" "%a"
Command to run on cmd (This command works with no issues:
for %a in ("C:\Emergency_POD\*.lnk") do #start "" "%a"
You'd probably be better off with %%a in place of %a in the batch file.

How do I execute cmd commands through a batch file?

I want to write a batch file that will do following things in given order:
Open cmd
Run cmd command cd c:\Program files\IIS Express
Run cmd command iisexpress /path:"C:\FormsAdmin.Site" /port:8088 /clr:v2.0
Open Internet Explorer 8 with URL= http://localhost:8088/default.aspx
Note: The cmd window should not be closed after executing the commands.
I tried start cmd.exe /k "cd\ & cd ProgramFiles\IIS Express", but it is not solving my purpose.
So, make an actual batch file: open up notepad, type the commands you want to run, and save as a .bat file. Then double click the .bat file to run it.
Try something like this for a start:
c:\
cd c:\Program files\IIS Express
start iisexpress /path:"C:\FormsAdmin.Site" /port:8088 /clr:v2.0
start http://localhost:8088/default.aspx
pause
I think the correct syntax is:
cmd /k "cd c:\<folder name>"
This fixes some issues with Blorgbeard's answer (but is untested):
#echo off
cd /d "c:\Program files\IIS Express"
start "" iisexpress /path:"C:\FormsAdmin.Site" /port:8088 /clr:v2.0
timeout 10
start http://localhost:8088/default.aspx
pause
cmd /c "command" syntax works well. Also, if you want to include an executable that contains a space in the path, you will need two sets of quotes.
cmd /c ""path to executable""
and if your executable needs a file input with a space in the path a another set
cmd /c ""path to executable" -f "path to file""
#echo off
title Command Executer
color 1b
echo Command Executer by: YourNameHere
echo #################################
: execute
echo Please Type A Command Here:
set /p cmd=Command:
%cmd%
goto execute
start cmd /k "your cmd command1"
start cmd /k "your cmd command2"
It works in Windows server2012 while I use these command in one batch file.
cmd /k cd c:\
is the right answer
I was trying to run a couple of batch files parallely at startup, if a condition was true.
For this I made a parent batch file which should have checked for the condition and invoke the other child batch files if the condition was true.
I tried to achieve it via START but it gave me an empty black command prompt running in the directory of children batch files, instead of running the children batch files themselves
The thing which worked for me was by using a combination of START and CALL
As an example
condition ...
start call "C:\Users\Amd\conn\wsl_setup - conn1.bat"
start call "C:\Users\Amd\conn\wsl_setup - conn2.bat"
start call "C:\Users\Amd\conn\wsl_setup - conn3.bat"
I know DOS and cmd prompt DOES NOT LIKE spaces in folder names. Your code starts with
cd c:\Program files\IIS Express
and it's trying to go to c:\Program in stead of C:\"Program Files"
Change the folder name and *.exe name. Hope this helps

Resources