Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Old Forums
 CLOSED - General SQL Server
 Does xp_cmdShell like long filenames?

Author  Topic 

SMerrill
Posting Yak Master

206 Posts

Posted - 2004-04-09 : 17:08:40
I'm having a hard time building a string and sending it to MASTER..XP_CMDSHELL without it bombing. I try the same command line at the CMD prompt and it flies fine. I change the filenames in the built string to the old 8.3 convention and it also works fine.

My environment is SQL7 on NT4.

Perhaps it's the spaces in the filename . . . don't I use (") as the quote mark for NT4?

Here is an example:

DECLARE @CMD NVARCHAR(4000)
DECLARE @RESULT INT
SET @CMD='"C:\Directory Name\Batch File.BAT" "Parameter One"'
PRINT @CMD
EXEC @RESULT=master..xp_cmdshell @CMD
PRINT ' ... The result code is ' + CAST(@Result AS VARCHAR)
RETURN
The result will always be:

"C:\Directory Name\Batch File.BAT" "Parameter One"
output
------------------
The name specified is not recognized as an
internal or external command, operable program or batch file.

(2 row(s) affected)
... The result code is 1

What's up with this?

~ Shaun Merrill
Seattle, WA

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2004-04-09 : 17:21:27
Shaun,

command_string of xp_cmdshell "cannot contain more than one set of double quotation marks. A single pair of quotation marks is necessary if any spaces are present in the file paths or program names referenced by command_string. If you have trouble with embedded spaces, consider using FAT 8.3 file names as a workaround."


You've got two sets of double quotation marks, which is not allowed.

Tara
Go to Top of Page

derrickleggett
Pointy Haired Yak DBA

4184 Posts

Posted - 2004-04-09 : 17:23:27
Actually, you need to use this format.

exec master..xp_cmdshell 'dir c:\"Program Files"\"Outlook Express"'

And, this does work.

MeanOldDBA
derrickleggett@hotmail.com

When life gives you a lemon, fire the DBA.
Go to Top of Page

SMerrill
Posting Yak Master

206 Posts

Posted - 2004-04-09 : 19:00:06
Derrick:
GZIP is a utility that unzips .Z files, like PKUNZIP.
You properly disagree with what Tara said because this works:
exec master..xp_cmdshell 'c:\"Program Files"\"Outlook Express"\TEST.BAT "Parameter One"'
...But Tara disagrees with you, because this doesn't work:
exec master..xp_cmdshell 'C:\"Program Files"\"Pyderion CTI"\ICCS\SERVER\PROGRAMS\GZIP.EXE -dac D:\InetPub\ftproot\ACD\Loc23\040115.Z > "D:\InetPub\ftproot\ACD\Loc23_ACD.DAT"'

...this produced an error:
output
--------------
Files"Pyderion CTI\ICCS\SERVER\PROGRAMS\GZIP.EXE.gz: No such file or directory
It seems to be separating at the first space.
So what do you say now? Is it the pipe (>) command?


~ Shaun Merrill
Seattle, WA
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2004-04-09 : 19:03:01
I only quoted from SQL Server Books Online.

Tara
Go to Top of Page

SMerrill
Posting Yak Master

206 Posts

Posted - 2004-04-09 : 19:06:46
Right. I understand.
This works, but I still need to know what is going on...
exec master..xp_cmdshell '"C:\Program Files\Pyderion CTI\ICCS\SERVER\PROGRAMS\GZIP.EXE" -dac D:\InetPub\ftproot\ACD\Loc23\040115.Z > D:\InetPub\ftproot\ACD\Loc23_ACD.DAT'




~ Shaun Merrill
Seattle, WA
Go to Top of Page

derrickleggett
Pointy Haired Yak DBA

4184 Posts

Posted - 2004-04-09 : 20:37:01
I'm not sure how your command could have produced this interesting little bit:

.gz:

If my first example works, the second is the same. So it can be assumed there are other syntax problems with the first. Is there a procedure that was producing this string? If so, could we see it?

Also, I'm aware of what GZIP is. :) And arc, arj, rar, blah,blah,blah.



MeanOldDBA
derrickleggett@hotmail.com

When life gives you a lemon, fire the DBA.
Go to Top of Page

SMerrill
Posting Yak Master

206 Posts

Posted - 2004-04-12 : 12:38:06
Here you go...

SET NOCOUNT ON
DECLARE @Command NVARCHAR(4000)
DECLARE @Success TINYINT
DECLARE @Date CHAR(6) -- YYMMDD date to be processed
DECLARE @Result INT
DECLARE @Location VARCHAR(2)
DECLARE @Directory VARCHAR(30) -- Points to source data directory when @Location is appended
DECLARE @FileName VARCHAR(250) -- File name to be imported
DECLARE @ProgDir VARCHAR(250) -- Pointer to ICCS Server directory

SET NOCOUNT ON
SELECT @Date = CONVERT(char(6),isnull(max(date)+1,getdate()-1),12)
from hist_que where branch <> 'MAX'

SET @ProgDir = 'C:\"Program Files"\"Pyderion CTI"\ICCS\SERVER\'
SET @Command = N'DEL ' + @ProgDir + 'logs\PkgC2Import.SQL'

PRINT @Command
EXEC @Result = master..xp_cmdshell @Command, NO_OUTPUT
IF @Result=0
BEGIN
PRINT 'Delete successful.'
END
ELSE
BEGIN
PRINT 'Delete was unsuccessful.'
RETURN
END

SET @Location = '23'

SET @Directory = 'D:\InetPub\ftproot\ACD\Loc' + @Location
SET @FileName = @Directory + '\' + @Date + '.Z'
EXEC @Result=sp_FileExists @FileName

IF @Result=0 -- If the source data has been FTP'ed into the directory for this date and location
BEGIN
SET @Command = N'DEL ' + @Directory + '_acd.dat'
PRINT @Command
EXEC @Result = master..xp_cmdshell @Command -- Delete the output of the GZIP command for this location
PRINT '.. DEL result code was = ' + cast(@Result as Varchar)
--THIS WORKS: exec master..xp_cmdshell '"C:\Program Files\Pyderion CTI\ICCS\SERVER\PROGRAMS\GZIP.EXE" -dac D:\InetPub\ftproot\ACD\Loc23\040115.Z > D:\InetPub\ftproot\ACD\Loc23_ACD.DAT'

-- Unzip the LocXX/YYMMDD.Z file into the LocXX_ACD.DAT file, where XX is the location and YYMMDD is the date:
SET @Command = @ProgDir + 'PROGRAMS\GZIP.EXE -dac ' + @Directory + '\' + @Date + '.Z > ' + @Directory + '_ACD.DAT'
PRINT @Command
EXEC @Result = master..xp_cmdshell @Command
PRINT '.. GZIP result code was = ' + cast(@Result as VARCHAR)
END
SET NOCOUNT OFF

Thanks for your help...

~ Shaun Merrill
Seattle, WA
Go to Top of Page

SMerrill
Posting Yak Master

206 Posts

Posted - 2004-04-12 : 19:25:32
Ok, I worked around the problem, so this case is solved.

WATCH OUT: During my code experimenting, I had inadvertently created a file called "C:\Program" and the existence of this file threw the whole NT4 server into a tizzy. It couldn't start any services, because it could not find "C:\Program Files\..." at all. This caused dozens of executables to cease to be valid NT executables.

Whew!


~ Shaun Merrill
Seattle, WA
Go to Top of Page

derrickleggett
Pointy Haired Yak DBA

4184 Posts

Posted - 2004-04-12 : 20:01:23
That's kind of kewl. :)



MeanOldDBA
derrickleggett@hotmail.com

When life gives you a lemon, fire the DBA.
Go to Top of Page

byrmol
Shed Building SQL Farmer

1591 Posts

Posted - 2004-04-12 : 20:05:43
Are you aware of the good old PATH command in DOS?? Would have saved all this heartache....

DavidM

"If you are not my family or friend, then I will discriminate against you"
Go to Top of Page

SMerrill
Posting Yak Master

206 Posts

Posted - 2004-04-13 : 13:34:56
Sounds like more of a job for SUBST than PATH. What creative idea is lurking in your mind, David?
Go to Top of Page

Lavos
Posting Yak Master

200 Posts

Posted - 2004-04-13 : 21:48:24
Who knows what creative ideas lurk in the minds of men?

MS DOS.

With the deviant BASH on the loose in the city, DavidM assumes the role of the DOS to bring down the villianous society of the UNIX.


Sorry, but SMerrill's post made me think of comic books for some reason.



----------------------
"O Theos mou! Echo ten labrida en te mou kephale!"

"Where theres a will, theres a kludge." - Ken Henderson
Go to Top of Page
   

- Advertisement -