Solved

SQL command - please help to dsipher

Posted on 2013-01-30
3
573 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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

919 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now