Solved

SQL command - please help to dsipher

Posted on 2013-01-30
3
577 Views
Last Modified: 2013-01-30
Would be greatful for help on the following, we have a routine on our SQL box following:

cmd /q /c "For /F "tokens=1 delims=" %v In ('ForFiles /P "E:\database\MSSQL.1\MSSQL\LOG" /m *_*_*_*_*.txt /d -30 2^>^&1') do if not "%v" == "ERROR: No files found with the specified search criteria." echo del "E:\database\MSSQL.1\MSSQL\LOG"\%v& del "E:\database\MSSQL.1\MSSQL\LOG"\%v"

I can pick out some of what it is doing but can anyone help in the detail, from my limited knowledge it is deleting the SQL log files which I would normaly do through a maintenance plan which deletes the records not removes the actual log file.

Note.  was writen by a DBA

Many thanks.
0
Comment
Question by:Smartin
  • 2
3 Comments
 
LVL 19

Accepted Solution

by:
Barthax earned 500 total points
ID: 38834644
The command goes through the output of the following command:

ForFiles /P "E:\database\MSSQL.1\MSSQL\LOG" /m *_*_*_*_*.txt /d -30 2^>^&1

This command runs the ForFiles executable which locates files based on the parameters given.

ForFiles: http://technet.microsoft.com/en-us/library/cc753551%28v=ws.10%29.aspx

/P is the path and /m is the file mask.  So it is searching "E:\database\MSSQL.1\MSSQL\LOG" directory for files which have the name like *_*_*_*_*.txt.  The "/d -30" limits the search to the files created in the last 30 days.  The final syntax ensures the user does not see the output of this command (2> redirects errors and > redirects normal output).

The "For" near the beginning iterates each line of the output from the "ForFiles" command.

For : http://ss64.com/nt/for_f.html

The For command looks only for the first word ("tokens=1 delims=") in the output of the ForFiles command and places that token into the %v variable.

If the %v variable does not equal "ERROR: No files found with the specified search criteria." then the following is performed:

The user is informed that the file in %v will be deleted:
echo del "E:\database\MSSQL.1\MSSQL\LOG"\%v

... and then actually performs this task:
del "E:\database\MSSQL.1\MSSQL\LOG"\%v"

Note that the & character on the line means to execute the command up to that point & then utilise the rest of the line as another command.  Thus the "echo" and the "del" are two separate commands issued based on the single line of the output from ForFiles.
0
 
LVL 19

Expert Comment

by:Barthax
ID: 38834650
Amendment:

"The For command looks only for the first word ("tokens=1 delims=") in the output of the ForFiles command and places that token into the %v variable."

Should read:

"The For command looks only for the first word of each line ("tokens=1 delims=") in the output of the ForFiles command and places that token into the %v variable."
0
 

Author Closing Comment

by:Smartin
ID: 38834678
Excellant - perfect answer!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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 Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

803 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