Solved

FTP file list and cmd shell

Posted on 2010-08-23
8
853 Views
Last Modified: 2013-11-29
I want to use vb OR Ms SQl  to get a list of filenames from a remote sql server. I just want the file names, not the files. We've been doing this with the vb object FtpClientConnection but it does not work with several of our new partners so we're looking for an alternative. I figure the most fool proof way is just to run a cmdshell from sql or VB but I can't find anything that just retrieves a list of filenames.

Whats a good solution??
0
Comment
Question by:janmishkin
  • 4
  • 3
8 Comments
 
LVL 12

Expert Comment

by:Gary Dewrell
Comment Utility
0
 
LVL 3

Accepted Solution

by:
ModuleKev earned 500 total points
Comment Utility
In SQL using cmdshell, in a generic approach:

Create a global temp table with one field varchar(8000).

Create a temp table for the file list with
 Data varchar(8000),
 Size int,
 DateCreated datetime

Insert into the global table:

SELECT @username
UNION ALL
SELECT @password
UNION ALL
SELECT 'cd "' + @RemoteDirectory + '"'
UNION ALL
SELECT 'dir'
UNION ALL
SELECT'quit'

Export the contents to a file on the system via BCP

Then set a variable up to hold the command (varchar(8000)):
SET @Command = 'ftp -s:"' + @TheFilename + '" "' + @TheFTPHost + '"'

INSERT INTO #tFileList
(
Data
)
EXEC @ReturnVal = master..xp_cmdshell @Command

Then update the data in #tFileList to get the true filename
0
 

Author Comment

by:janmishkin
Comment Utility
Will try both soon.

Thanks a lot
0
 

Author Comment

by:janmishkin
Comment Utility
gdewrell:
We already use a similar solution. We need to switch to a command line solution as an alternative because somethings not right with one of our clients using SSIS

ModuleKev::
Your solution looks good but I'm stuck at

Exec master..xp_cmdShell 'bcp "select * from MyDB.dbo.ftpinf" queryout C:\test.txt -c -T'

C:\test.txt  is @filename

the bcp runs fine. No errors but also no file is created?!!?

heres the result
NULL
Starting copy...
NULL
4 rows copied.
Network packet size (bytes): 4096
Clock Time (ms.) Total     : 1      Average : (4000.00 rows per sec.)
NULL

I know this is a seperate concern but I can't get to the end of your solution because of it. Any ideas?

Thanks
0
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
LVL 3

Expert Comment

by:ModuleKev
Comment Utility
Sorry been off ill...

Literally running the same code as you've posted (bar table selection) works for me..

Run Exec master..xp_cmdshell 'dir c:\*.*'

In the results can you see your file? I am just making sure that you are aware that the file should be created on the machine sql is running on, not the machine you're working off. I used to do break down and recovery and it's often the simpliest thing you don't think of that is the root cause.

If not:

If you're running on later than Windows 2003, you may find it's an issue outputting to the root of a drive. Create a folder on the root and then try to output to that instead.
0
 
LVL 3

Assisted Solution

by:ModuleKev
ModuleKev earned 500 total points
Comment Utility
Create folder test on c then,

Try...

Exec master..xp_cmdShell 'bcp "select * from MyDB.dbo.ftpinf" queryout C:\test\test.txt -c -S<serverinstance> -T'

i.e:

Exec master..xp_cmdShell 'bcp "select * from MyDB.dbo.ftpinf" queryout C:\test\test.txt -c -SServer1 -T'

See http://msdn.microsoft.com/en-us/library/ms162802.aspx
0
 

Author Comment

by:janmishkin
Comment Utility
Yup. It worked when I explicitly defined the Server using the -S tag. Now on to the next steps. Thanks
0
 

Author Closing Comment

by:janmishkin
Comment Utility
Good Job.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now