Currently, the below For/Do loop goes through all SQL files that are in the same folder as the batch file itself and outputs the CSVs to a user-specified folder (%OUTPUTFOLDER% is actually created earlier in the batch file as a subfolder of %SCRIPTFOLDER%, which is specified by the user):
FOR /F "tokens=*" %%S IN (
'DIR /B "%SCRIPTFOLDER%\*.sql" '
) DO (
echo Reading scripts from: %SCRIPTFOLDER%\*.sql
echo Script: %%~fS
echo Output: %OUTPUTFOLDER%\%%~nS.csv
sqlcmd -b -S %INSTANCE% -d %DATABASE% -i "%%~fS" -s "|" -o "%OUTPUTFOLDER%\%%~nS.csv" -W
IF ERRORLEVEL 1 GOTO errorhandling
ECHO %%~nS.csv successfully created
)
For a visual example, if I had the folder structure
C:\Extract\extract.bat
C:\Extract\Scripts\
C:\Extract\Output\
The variable %SCRIPTFOLDER% is set by the user and is the folder that, you guessed it, holds the scripts. But the batch file has to be in that folder, too. I need to change this so that the scripts do not have to be in the same folder as the batch file. I.e. the user can specify both %SCRIPTFOLDER% and %OUTPUTFOLDER%
Due to the output of echo Script: %%fS, I'm guessing that's what I need to change - possibly what's in the FOR line as well, but I'm not seeing how exactly to do that.
Related
I have multiple .sql script files and want to execute them one by one in batch file with proper logging mechanism.
I wrote below batch command to solve the problem
#ECHO OFF
SET SQLCMD="C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\SQLCMD.EXE"
SET PATH="C:\Users\sql_scripts\"
SET SERVER="server_name"
SET DB="database"
SET LOGIN="user_name"
SET PASSWORD="password"
SET OUTPUT="C:\Users\sql_scripts\output\OutputLog.txt"
CD %PATH%
ECHO %date% %time% > %OUTPUT%
for %%f in (*.sql) do (
%SQLCMD% -S %SERVER% -d %DB% -U %LOGIN% -P %PASSWORD% -i %%~f -b >> %OUTPUT%
)
But it has few problems that I want to solve as listed below :
I want to create dynamic out put file names based on input file name. Example if input sql file
name is "test.sql" then I want to create output file in "output" folder with name "test.sql".
Currently It writes all the log to one file i.e. OutputLog.txt
I want to show output log with script of execution to identify which script produced what
output. Let's say in one script file 5 scripts present then in output file I want to show the
script and its output log.
I inherited a set of batch files.
Bat1 called bat2 which called bat3.
Bat3 created an xml, which bat2 used to create an html and bat1 used that to create a pdf.
Finally bat1 copied the pdf to an output directory.
I've recently joined the three batch files into one and everything works exactly the same up to the final copy command, which fails stating the pdf cannot be found.
#echo off
pushd %~dp0
set sourceFile=%1
set xslt=%2
set outputFile=%3
for %%i in (%sourceFile%) do (
set documentName=%%~ni
)
::BATCH FILE 2 CALLED
::BATCH FILE 3 CALLED
echo Processing source file compose
java -cp lib\saxon9.jar; net.sf.saxon.Transform -s:%sourceFile% -xsl:conversion\compose.xsl -o:compose.xml
echo Processing source file catalog
java -cp lib\saxon9.jar; -s:compose.xml -xsl:conversion\catalog.xsl -o:catalog.xml
::END OF BATCH FILE 3
echo Processing source file to HTML
java -cp lib\saxon9.jar; net.sf.saxon.Transform -s:catalog.xml -xsl:conversion\html5.xsl -o:index.html
echo Bundling publication
mkdir %documentName%
xcopy /y "conversion\webapp\*" %documentName%\ /o /x /e /h /k
move index.html %documentName%\
copy source graphics into web application img res dir
for %%f in ("*.jpg" "*.png" "*.gif" "*.svg" "*.psd" "*.ai" "*.pdf") do copy "%%f" %documentName%\asset\img\
::END OF BATCH FILE 2
echo Running AHF
AHFCmd -extlevel 4 -d %documentName%\index.html -o %documentName%.pdf -f HTML -x 4 -i tool\AHF\AHFSettings(x64).xml -stdout
::EVERYTHING WORKS FINE TO HERE; %documentName%.pdf is created just as it was previously.
echo Moving %documentName%.pdf
copy %documentName%.pdf %outputFile% ::Error - The system cannot find the file specified.
popd
The line copy %documentName%.pdf %outputFile% hasn't changed. I have tried using CALL in front of the AHFCmd and JAVA commands but that makes no difference. I have tried dir /b /a-d just before the copy command and it lists the file I want to move in the current directory. What am I doing wrong?
I want to drag and drop a file onto a batch file in order to run the below command on it. How do I go about running the command on the dropped file?
PotreeConverter.exe <dropped file> -o C:/output -p index
The path of the file, when you drop it on the BATfile, will be returned as a normal %1 argument.
so :
#echo off
PotreeConverter.exe "%~1" -o C:/output -p index
You can use %* if you drop more then 1 file
Example :
#echo off
for %%a in (%*) do echo [%%a] was dropped on me
pause
Following this easy guide.
Create a batch file test.bat with the contents
#echo off
echo The full path of the file is: %1
pause
Drag any file onto it, you will see that %1 is replaced with the full path for that file in quotes.
Now you know how to execute some command that takes a path to a file as an argument:
#echo off
some_command_that_takes_a_path_to_a_file %1
Currently, the following code in a batch file works: it runs all SQL scripts in %SCRIPTFOLDER% and sends each script's output to a CSV within the same folder as the scripts.
FOR /F "tokens=*" %%S IN (
'DIR /B "%SCRIPTFOLDER%\*.sql" '
) DO (
sqlcmd -b -S %INSTANCE% -d %DATABASE% -i "%%~fS" -s "|" -o "%%~dpnS.csv" -W
IF ERRORLEVEL 1 GOTO errorhandling
ECHO %%~nS.csv successfully created
)
What I'd like to do is allow the user to specify where the generated CSVs get sent to using a variable %OUTPUTFOLDER%.
I tried placing %OUTPUTFOLDER%, which is a full path, drive, and folders (e.g. D:\some folder\output) in various positions within %%~dpnS.csv. Specifically,
%%~dp%OUTPUTFOLDER%nS.csv
and
%%~dpn%OUTPUTFOLDER%S.csv
but they didn't work and I'm (probably obviously to you) woefully inept at batch file syntax!
I understand that dp is the drive and path and that S is the file name, but I'm not sure how to integrate that with a the variable that is the path.
The iterating variable is %%S, the modifier ~dpn forces an evaluation of drive path and name.
In this case you want to specify the drive and path yourself so depending on wether %OUTPUTFOLDER% has a trailing backslash
-o "%OUTPUTFOLDER%%%~nS"
or not use:
-o "%OUTPUTFOLDER%\%%~nS"
I am using sql server 2008 , I am developing script that get all .sql file of given path ( also serch in subfolder recursively). Thanks in advance.
You could use a batch file like this. Call it ashwin.bat (or whatever you like) and it will look for all the files in C:\tmp\so\ashwin that have a .sql extension and then invokes sqlcmd against all of those files against a named instance database of localhost\localsqla and runs them in the master database.
#echo off
For /R "C:\tmp\so\ashwin\" %%i in (*.sql) DO CALL sqlcmd.exe -E -S localhost\localSQLA -d master -i %%i
A litle enhancement for logging purposes:
#echo off
For /R "C:\Deploy\SQL" %%i in (*.sql) DO CALL echo %%i && sqlcmd.exe -E -S DB_IP -d DATABASE -i %%i -j