Solved

SQL command - please help to dsipher

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

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 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

14 Experts available now in Live!

Get 1:1 Help Now