question

slash65 avatar image
slash65 asked

How do you substring a physical file?

I have this .txt files from Server A, i need to do Routine A with it, if the .txt files is older than 1 day, Routine A will be executed... I have this existing code, but only counts for the number of files then perform rountine A... Example file from Server A: file_item.20151109.txt - since its dated November 9, i need the Routine A to be executed... any idea how I can based the execution of Routine A from the physical file? and not based on rowcount? Here's what I got: -- if chk source folder has NEW file `IF object_id('tempdb.dbo.#tblFiles') > 0` `BEGIN` `DROP TABLE #tblFiles` `END` `CREATE TABLE #tblFiles (FileNames varchar(1000))` `SET @CmdShell = 'master.dbo.xp_cmdshell ''' + 'Dir ' + @SourceCode + ' /b'' '` `INSERT INTO #tblFiles exec (@CmdShell)` `Set @RowCnt = @@RowCount - 1` `IF @RowCnt = 3 ` `BEGIN` `--Perform Routine A` any idea how I can modify my existing syntax? thanks
tsqlsql server 2008 r2xp-cmdshell
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
erlokeshsharma08 avatar image
erlokeshsharma08 answered
Refer this link :- http://www.codeproject.com/Tips/866934/Extracting-the-filename-from-a-full-path-in-SQL-Se 1. Use function GetFileName 2. With this syntax get the date part of your filename - select SUBSTRING('file_item.20150101.txt',CHARINDEX('.','file_item.20150101.txt')+1,8) 3. Use datediff and if else condition to trigger routine A
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.