Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

FTP file list and cmd shell

Posted on 2010-08-23
8
Medium Priority
?
875 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 2000 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
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…
Suggested Courses

618 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