x

Using xp_cmdshell to view the time of the latest folder created

Hi,

There is a folder in the drive.

Using xp_cmdshell I want to view the timing of the latest folder created. How to know the timing of the folder created?

This Timing I have to assign to a variable.

more ▼

asked Oct 11, 2010 at 12:29 AM in Default

mandara gravatar image

mandara
39 10 10 10

(comments are locked)
10|1200 characters needed characters left

1 answer: sort newest

Doing this with xp_cmdshell is not the best way to do this.(Powershell, SSIS, C# and many other languages/tools can do this much better).

Saying that, you can still do it! You would proceed as follows:

-- Variable you want to assign the date to
DECLARE @creationdate datetime

-- Create a temp table to store the results of xp_cmdshell 
-- (I'm going with varchar(8000) as you don't mention SQL Server version)
CREATE TABLE #results (Col1 varchar(8000))

-- Insert the results into the temp table
INSERT #results
        (Col1)
-- dir with the options /TC will show creation date
--/0-D will sort them by date descending
EXEC master.dbo.xp_cmdshell 'dir c:\Temp\ /TC /O-D' 

-- Select the first entry that you find that refers to a directory
SELECT TOP 1 @creationdate = LEFT(Col1,20) FROM #results R WHERE col1 LIKE '%<DIR>%'
SELECT @creationdate
more ▼

answered Oct 11, 2010 at 12:52 AM

WilliamD gravatar image

WilliamD
25.9k 17 19 41

Thx for your anS William

Now the data in the table is in below format :-

10/11/2010 03:45 PM 1,212,928 name of the file .ext

From this above row i just want the Timing to be extravted :- ie 03:45 AM .

Kindly suggest
Oct 11, 2010 at 01:09 AM mandara

There is nothing like trying yourself! Just put a RIGHT() around the LEFT() command to grab what you want:

SELECT TOP 1 @creationdate = RIGHT(LEFT(Col1,20),8) FROM #results R WHERE col1 LIKE '%<DIR>%'

That should get you what you want
Oct 11, 2010 at 01:13 AM WilliamD
Thx Willaim, Its done.
Oct 11, 2010 at 01:54 AM mandara

@William... I realize that this is an old post but you said...

"Doing this with xp_cmdshell is not the best way to do this.(Powershell, SSIS, C# and many other languages/tools can do this much better)."

What's makes those 3 methods "better"?
Jun 21, 2013 at 04:14 PM Jeff Moden
@Jeff Moden I am not sure you could properly say that they are better, but more general purpose languages like Python or even Powershell can do this more elegantly and I would use those unless there was a strong reason in the work flow to use SQL. In Powershell, its relatively simple to make this a one liner for instance.
Jun 21, 2013 at 05:30 PM TimothyAWiseman
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x14

asked: Oct 11, 2010 at 12:29 AM

Seen: 1552 times

Last Updated: Jun 21, 2013 at 05:30 PM