Solved

FTP file list and cmd shell

Posted on 2010-08-23
8
864 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
ID: 33505617
0
 
LVL 3

Accepted Solution

by:
ModuleKev earned 500 total points
ID: 33521115
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
ID: 33524448
Will try both soon.

Thanks a lot
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

Author Comment

by:janmishkin
ID: 33598768
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
 
LVL 3

Expert Comment

by:ModuleKev
ID: 33615699
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
ID: 33628587
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
ID: 33664310
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
ID: 33665464
Good Job.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

749 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