Run all SQL queries in a folder and its sub folders - batch-file

I am trying to run several hundreds queries that are located in many folders. All the folders are located under one "Master" directory.
I have the following batch code that allows me to run all the queries in one folder:
for %%G in (*.sql) do psql -U postgres -f "%%G" Satellites_Updated
pause
I am unsure how to make this batch file recursive so I can also check sub folders.
I have the following code, which does not work (only prints the current directory in the cmd window, then exits).
#echo off
call :treeProcess
goto :eof
:treeProcess
for %%G in (*) do (
if exist %1\* (
cd %1
call :treeProcess
cd ..
)
else (
psql -U postgres -f "%%G" Satellites_Updated
)
)
exit \b
Thanks in advance!

Here is the solution:
for /r %%G in (*.sql) do psql -U postgres -f "%%G" Satellites_Updated
pause
When it is so simple sometimes ...

Related

Find all .sql-Files in a Directory and execute them with sqlcmd

I'm still a CMD beginner and would like to import all SQL files I have in a directory structure with sqlcmd to my DB.
If I copy all *.sql into the root folder the following command works:
#ECHO ON
FOR %%G in (*.sql) DO sqlcmd /S *SERVER* /d *DB* /U *USER* /P *PW* -i"%%G" > LOG_lastrun.txt 2> LOG_errors.txt
pause
#ECHO ON
FOR /F "tokens=1 delims=sql" %%G in (*.sql) DO sqlcmd /S *SERVER* /d *DB* /U *USER* /P *PW* -i"%%G" > LOG_lastrun.txt 2> LOG_errors.txt
pause
Unfortunately, it doesn't work for me FOR /F loops. Can you help me here?
The FOR /F works differently. It runs a command. Jusr *.sql is not a command. Also, I suspect that you want to concatenate stdout and stderr output using >>.
#ECHO ON
SET "SERVER_NAME=*SERVER*"
SET "DB_NAME=*DB*"
SET "LOGIN_ID=*USER*"
SET "PASS=*PW*"
FOR /F "delims=" %%G IN ('DIR /B "*.sql"') DO (
sqlcmd -S %SERVER_NAME% -d %DB_NAME% -U %LOGIN_ID% -P %PASS% -i "%%~G" >> LOG_lastrun.txt 2>> LOG_errors.txt
)
pause

ftp command to download file by date

I am using .bat script to access sftp and download file. The ftp folder consist of a lot of file which can be uniquely identify by the file name in format YYYYMMDD. eg: 20180307.csv, 20180306.csv etc. Currently I am using mget to get all the file, then at later filter it locally. How can I modify the script to get the file on today only?
This is my .bat script.
D:
CD\myTargetFolder
psftp user#sftp.myftpserver.com -pw password -b mySftpScript.ftp
exit
This is my ftp script.
cd /myFTPSourceFolder
mget *.csv
bye
Thank you.
If you don't mind dynamically creating the ftp script then perhaps your .bat script could look like this:
#Echo Off
CD /D D:\myTargetFolder 2>Nul || Exit /B
Set "csv="
For /F "Tokens=1-3 Delims=/ " %%A In ('RoboCopy /NJH /L "\|" Null'
) Do If Not Defined csv Set "csv=%%A%%B%%C.csv"
( Echo cd /myFTPSourceFolder
Echo mget %csv%
Echo bye)>"mySftpScript.ftp"
psftp user#sftp.myftpserver.com -pw password -b mySftpScript.ftp
::Del "mySftpScript.ftp"
I've commented out the last line, remove the :: from it if you wish to delete the dynamically created ftp script.
Edit
To get yesterday's date csv then something like this, leveraging PowerShell, would be my recommendation:
#Echo Off
CD /D D:\myTargetFolder 2>Nul || Exit /B
Set "csv="
For /F UseBackQ %%A In (
`PowerShell "(Get-Date).AddDays(-1).ToString('yyyMMdd')"`
) Do Set "csv=%%A.csv"
( Echo cd /myFTPSourceFolder
Echo mget %csv%
Echo bye)>"mySftpScript.ftp"
psftp user#sftp.myftpserver.com -pw password -b mySftpScript.ftp
::Del "mySftpScript.ftp"

Batch script terminating unexpectedly before IF command

I have written the below code in a batch script.
set /p timestamp=Enter timestamp:
cd "C:\temp\%timestamp%"
for %%a in (*.rmt) do (bldtool -c COMMAND -a SPLIT -n %%a -l C:\temp\%timestamp%)
if exist "C:\temp\%timestamp%\XCLES01A.c" (xcopy /Y "C:\Program Files\CA\AllFusion Gen\GEN\extrn\src\XCLES01A.c" "C:\temp\%timestamp%")
for %%a in (*.icm) do (bldtool -c COMMAND -a BUILD -n %%a -l C:\temp\%timestamp% -f CodeMgr)
pause
When i run the above script line-by-line in cmd, it runs as expected. However, when I run it in the form of a script, it terminates before the if command is executed i.e. the window just disappears.
I have spent hours on trying to resolve this and am still stuck. Please help ! I am very new to batch scripting.
Thanks in advance.
Update: BLDTOOL is an executable software.
Try this (I haven't though):
set /p timestamp=Enter timestamp:
cd /D "C:\temp\%timestamp%"
for %%a in (*.rmt) do (
bldtool -c COMMAND -a SPLIT -n %%a -l "C:\temp\%timestamp%"
)
if exist "C:\temp\%timestamp%\XCLES01A.c" (
xcopy /Y "C:\Program Files\CA\AllFusion Gen\GEN\extrn\src\XCLES01A.c" "C:\temp\%timestamp%"
)
for %%a in (*.icm) do (
bldtool -c COMMAND -a BUILD -n %%a -l "C:\temp\%timestamp%" -f CodeMgr
)
pause

way to execute different folders' scripts using one bat file from one location

I have to execute a bulk of scripts on sql server 2008r2.These scripts are present in many folders and subfolders.I created a batch file to execute these scripts but my problem is i have to put this bat file in a folder to execute the concerned folder's scripts.So, if there are more than 10 folders so i have to put it this bat file 10 times in a folder.Is there any way so that i can put my bat file just once wherever i want(like on desktop) and execute the folder's files?
.bat file:-
for %%G in (*.sql) do sqlcmd /S %1 /d %2 -E -i"%%G" pause
Passing server name and database name using command prompt.
Add the /R option to the FOR to search recursively:
FOR /R . %%G IN (*.sql) DO ... etc ...
That will search for *.sql files anywhere below the current directory (.). Or make it FOR /R C:\Folder %%G IN (*.sql) DO ... if you want a specific folder as the root.
Assuming you wish to execute yourbatasposted from multiple directories using the same servername and databasename,
#ECHO OFF
SETLOCAL
FOR /f "delims=" %%i IN (folderlist.txt) DO (
PUSHD "%%i"
CALL yourbatasposted %*
POPD
)
where folderlist.txt is a simple textfile containing the target directory names, one to a line.
If you wish to control which servers/databases/directories are executed individually, then create controllist,txt formatted as lines of
servername database directory
#ECHO OFF
SETLOCAL
FOR /f "tokens=1,2*delims=Q" %%i IN (controllist.txt) DO (
PUSHD "%%k"
CALL yourbatasposted %%i %%j
POPD
)
Where "Q" is the delimiter between the columns in controllist.txt. Omit the delims= clause if you choose to use spaces.
Naturally, yourbatchasposted.bat should be located in any directory mentioned in the %path% variable...

How do i get my batch script to use a command in recursive dir?

example 1
#echo off
for %%a in (precomp.exe) do set pc=%%~fa
for /r %%a in (*.pcf) do (
pushd %%~dpa
echo [%pc% -r %%~nxa]
%pc% -r %%~nxa
popd
)
When i run the bat i get precomp.exe is an unknown command I have precomp
in the folder with the script. But it will only work if i copy precompt to every sub directory and every folder that contains a .pcf file
Tried a different approach using 2 scripts
script1
for /r %%i in (*.pcf) do call sr2 "%%~pi" "%%i"
script 2
pushd %1
precomp -r %2
popd
Both scripts work but only if i copy precomp into Ever folder and all sub folders. Please help as i know there must be away to make the script use the precomp in the folder with the script
You can temporarily modify the PATH environment variable to include the path of precomp:
e.g.:
setlocal
set PATH=%PATH%;%~dp0
for /r %%i in (*.pcf) do (
pushd "%%~pi"
precomp -r %%i
popd
)
or use the path of precomp directly:
for /r %%i in (*.pcf) do %~dp0\precomp -r %%i

Resources