I have a .bat file that iterates through it's residing director and takes the file name and creates a series of insert into select openrowset statements that is written to another file for later use.
I am trying to execute this .bat file in SQL Server using the below statement:
EXEC master..xp_cmdshell 'cmd /c filepath\FileTransfers\incoming\pdfs\gensql.bat'
overall the command works, if I replace gensql.bat with helloworld.bat I get the expected output.
The problem: When I attempt to execute the command using the gensql.bat I get null output and the file is not written to.
- If I run the bat file from the command prompt it works as expected.
- If I manually run the bat file from it's folder it works as expected.
- If I attempt it to run it via PowerShell I get access denied as expected.
I have tried to run it using the SQL Server job agent and the job runs successfully but there the expected output to the file is not there.
The account I am logged into and running the bat file has the permissions to the file location because I load other files from the same file path.
Command I am executing in SQL Server
EXEC master..xp_cmdshell 'cmd /c filepath\FileTransfers\incoming\pdfs\gensql.bat'
Contents of .bat file -- all this bat file is doing is writing statements to a file
@echo off
echo. > loadFiles.sql
for /r %%i in (*.pdf) do (
echo INSERT INTO dbo.user_attachments(content, extension, recipient_id^)
echo SELECT bulkcolumn, '.pdf', '%%~ni'
echo FROM OPENROWSET(BULK '%%i', SINGLE_BLOB^) AS t;
) >> loadFiles.sql
Any help I can get with this is very much appreciated.