?
Solved

FTP file list and cmd shell

Posted on 2010-08-23
8
Medium Priority
?
870 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
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!

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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
Suggested Courses

765 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