Solved

FTP file list and cmd shell

Posted on 2010-08-23
8
865 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to shrink a transaction log file down to a reasonable size.

705 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