Solved

SQL command - please help to dsipher

Posted on 2013-01-30
3
590 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

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.
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

729 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