Sending sqlcmd output file to directory that is a variable - batch-file

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"

Related

Multi-file script execution from command line

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.

User-specified scripts and output folders for sqlcmd in batch file

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.

deleting the input file after handbraking in batch-file

I wrote this code for HandBrakeCLI as a batch file to manipulate my videos. This code creates output files with input file name plus a "_conv" suffix.
for /R .\test %%F in (*.mov) do HandBrakeCLI -e x264 --x264-preset medium -q 35 --crop 0:0:0:0 --aencoder copy -i "%%~fF" -o "%%~pF%%~nF_conv.mp4
Then I want to delete the original file and then remove _conv part of the output file. What should be added to the code above?
I want to delete each file just after converting it, or at least when going from its containing folder to another folder, not wholly after converting all of the file (because lots of files must be converted and I may run out of space)
By the way, how can I add other formats in addition of *.mov in the code?
for /R .\test %%F in (*.mov) do (
HandBrakeCLI -e x264 --x264-preset medium -q 35 --crop 0:0:0:0 --aencoder copy -i "%%~fF" -o "%%~dpF%%~nF_conv.mp4"
if exist "%%~dpF%%~nF_conv.mp4" (
del "%%~fF"
ren "%%~dpF%%~nF_conv.mp4" "%%~nxF"
)
)
All the information is inside your original code. All that is needed is to wrap the set of commands in parenthesis so the three commands are executed for each of the input files. Also, an aditional if has been included to only delete the source file if the converted file exists.

How to batch convert .sph files to .wav with sox

I have a directory with many folders containing hundreds of .SPH files. I need to convert all .SPH files into .wav format. I have adopted the following code:
cd %~dp0
mkdir converted
FOR %%A IN (%*) DO sox -t raw -s -2 -r 22050 -c 2 %%A "converted/%%~nA.wav"
pause
However, it doesn't do anything on Windows 7. When I try the code on CMD inside a folder where some of .SPH are:
sox *.SPH output.wav
It embeds all *.SPH into output.wav file, which is not what I want. I need name1.SPH to name1.wav, name2.SPH to name2.wav
Please help.
For Linux consider this:
for f in *.SPH; do sox -t sph "$f" -b 16 -t wav "${f%.*}.wav"; done
In Windows 7, I need to provide the entire path for the destination files, and the mdkir command got a permission error even though I was running as Administrator, so pre-make your destination directory and delete the mkdir command, and then your SOX command should look similar to this (with your own flags based on the conversion or edit you are trying to accomplish):
FOR %%A IN (%*) DO sox -t raw -e mu-law -c 1 -r 8000 %%A c:\converted\%%~nA.wav
The cmd file should not be in the folder where the files are, it should be in the folder where SOX is installed, and then drag the files you want converted onto the cmd (or bat) file.
for %%a in (*.sph) do sox "%%~a" "%%~na.wav"
Make a .bat file in your sox directory with the contents
cd %~dp0
for %%a in (*.sph) do sox "%%~a" "%%~na.wav"
pause
Select and drag the files that you want to convert onto this .bat file and the .wav files with the same names as the .sph files will appear in the same folder as the .sph files.

How to append batch file command to a strings(file names) using batch file

I'm in a process of creating a batch file to list names of all SQL scripts in a folder to a text file. My plan to convert this text file to a batch file so that I can use it to execute the scripts in the server. So I would like to have the following string appended before each file name while creating the initial text file
sqlcmd -S %MSSQLSERVER_NAME% -d %MSSQLSERVER_DBNAME% -i
This is a batch file command and I would like to be appended before the each file names.
eg:
sqlcmd -S %MSSQLSERVER_NAME% -d %MSSQLSERVER_DBNAME% -i 001_ALTER_PERSON.sql
The code I'm using is
set MSSQLSERVER_NAME = "%MSSQLSERVER_NAME%"
set %MSSQLSERVER_DBNAME = "%MSSQLSERVER_DBNAME%"
set myvar=sqlcmd -S %MSSQLSERVER_NAME% -d %MSSQLSERVER_DBNAME% -i
for /r . %%g in (*.sql) do echo %myvar% %%~nxg >> D:\test.txt
pause
Out put I'm getting
sqlcmd -S -d -i 015_ALTER_vBOARD_PAPERS.sql
Let me know how to tackle this
set myvar=sqlcmd -S %%MSSQLSERVER_NAME%% -d %%MSSQLSERVER_DBNAME%% -i
should cure your problem - % escapes %
note that spaces ARE significant in variable names, so set var = somethingwill set var[space] to [space]something
set %varname%=... is rare -it sets the variablename (contents of varname).
Even more rare to have unbalanced %... not sure that will work at all...

Resources