Solved

SQL command - please help to dsipher

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

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…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 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