'Makes an on-the-fly copy of BAT Or CMD files with SET /P 'statements before every line, then runs it so you 'can debug the batch file. Your actual batch file *is* 'replaced by the modified version, but is put back To 'normal afterwards. (Your batch file is backed up With 'a ".bak" file extension while the debug version runs) Option Explicit Main Sub Main Dim fs 'As Scripting.FileSystemObject Dim tsIn, tsOut, fol, fil, fils Dim strLine, strNextLine, intLabel, strOriginalFile Dim wsh 'As Wscript.Shell Const WshNormalFocus = 1 Const ForReading = 1 Const ForWriting = 2 Set fs = CreateObject("Scripting.FileSystemObject") Set wsh = CreateObject("Wscript.Shell") 'Make sure we have a valid BAT or CMD argument If WScript.Arguments.Count <> 1 Then WScript.Quit If Not(fs.FileExists(WScript.Arguments(0))) Then WScript.Quit If ((Lcase(Right(WScript.Arguments(0), 4)) <> ".bat") And (Lcase(Right(WScript.Arguments(0), 4)) <> ".cmd")) Then WScript.Quit 'Get the long name of the file (since dropped files have short names). strOriginalFile = LongName(WScript.Arguments(0)) 'Make a copy of the batch file If fs.FileExists(strOriginalFile & ".bak") Then If MsgBox("Delete existing """ & strOriginalFile & ".bak""?", vbYesNo) = vbYes Then fs.DeleteFile strOriginalFile & ".bak" Else WScript.Quit End If End If fs.MoveFile strOriginalFile, strOriginalFile & ".bak" 'Verify the copy If ((fs.FileExists(strOriginalFile)) Or (Not(fs.FileExists(strOriginalFile & ".bak")))) Then MsgBox "Unable to copy """ & strOriginalFile & """ to BAK file extension" WScript.Quit End If 'Open the in and out files Set tsIn = fs.OpenTextFile(strOriginalFile & ".bak", ForReading) Set tsOut = fs.OpenTextFile(strOriginalFile, ForWriting, True) 'Display the usage header tsOut.WriteLine Usage(strOriginalFile) 'Read the in file line-by-line, writing it to the out file strNextLine = tsIn.ReadLine intLabel = 0 Do Until tsIn.AtEndOfStream intLabel = intLabel + 1 strLine = strNextLine strNextLine = tsIn.ReadLine AppendToBatchFile tsOut, strLine, strNextLine, intLabel Loop 'Catch the last line AppendToBatchFile tsOut, strNextLine, "", intLabel + 1 tsIn.Close tsOut.Close 'Now run the debugging batch file If Lcase(Right(Wscript.FullName, 12)) = "\cscript.exe" Then WScript.Echo "Do not close this window. It will close after your batch file stops." End If If Left(WScript.Arguments(0), 1) <> """" And InStr(WScript.Arguments(0), " ") <> 0 Then wsh.Run """" & WScript.Arguments(0) & """", WshNormalFocus, True Else wsh.Run WScript.Arguments(0), WshNormalFocus, True End If 'All done. Move the file back to the correct name fs.DeleteFile strOriginalFile fs.MoveFile strOriginalFile & ".bak", strOriginalFile End Sub Sub AppendToBatchFile(tsBatch, strLine, strNextLine, intLabel) 'Ignore blank lines If IsBlank(strLine) Then Exit Sub 'Ignore commented lines If IsComment(strLine) Then Exit Sub 'Ignore ERRORLEVEL lines because they are processed when they are strNextLine If IsErrorLevel(strLine) Then Exit Sub 'Process a normal line If Not(IsLabel(strLine)) And Not(IsErrorLevel(strNextLine)) Then tsBatch.WriteLine "@echo " & SafeToEcho(strLine) tsBatch.WriteLine "@set /p yc_in= [Enter to run, any key plus Enter to skip] " tsBatch.WriteLine "@if not [%yc_in%]==[] goto YC" & Right("000000" & CStr(intLabel), 6) If IsVbs(strLine) Then tsBatch.WriteLine strLine & " //x" Else tsBatch.WriteLine strLine End If tsBatch.WriteLine ":YC" & Right("000000" & CStr(intLabel), 6) Exit Sub End If 'Process a line preceeding an ERRORLEVEL line If IsErrorLevel(strNextLine) Then tsBatch.WriteLine "@echo " & SafeToEcho(strLine) tsBatch.WriteLine "@echo " & SafeToEcho(strNextLine) tsBatch.WriteLine "@set /p yc_in= [Enter to run, any key plus Enter to skip] " tsBatch.WriteLine "@if not [%yc_in%]==[] goto YC" & Right("000000" & CStr(intLabel), 6) If IsVbs(strLine) Then tsBatch.WriteLine strLine & " //x" Else tsBatch.WriteLine strLine End If If IsVbs(strNextLine) Then tsBatch.WriteLine strNextLine & " //x" Else tsBatch.WriteLine strNextLine End If tsBatch.WriteLine ":YC" & Right("000000" & CStr(intLabel), 6) Exit Sub End If 'Process a label If IsLabel(strLine) Then tsBatch.WriteLine strLine tsBatch.WriteLine "@echo " & SafeToEcho(strLine) Exit Sub End If End Sub Function Usage(strFileName) Dim strUsage strUsage = "@echo off" strUsage = strUsage & vbCrLf & "@title Windows 2000 YC_Debugging: " & strFileName strUsage = strUsage & vbCrLf & "@echo On-screen line display will be indented 4 spaces with prompts indented" strUsage = strUsage & vbCrLf & "@echo 8 spaces. VBS files started with START /W will be run with //X." strUsage = strUsage & vbCrLf & "@echo Comments and labels will not be prompted. Lines using ERRORLEVEL will " strUsage = strUsage & vbCrLf & "@echo not be prompted to avoid modifying the error level of the preceeding line." strUsage = strUsage & vbCrLf & "@echo Environment variables will be expanded for on-screen prompts." strUsage = strUsage & vbCrLf & "@echo For on-screen prompts, the following character substitutions will be made:" strUsage = strUsage & vbCrLf & "@echo "">"" becomes """ & SafeToEcho(">") & """" strUsage = strUsage & vbCrLf & "@echo ""<"" becomes """ & SafeToEcho("<") & """" strUsage = strUsage & vbCrLf & "@echo ""&"" becomes """ & SafeToEcho("&") & """" strUsage = strUsage & vbCrLf & "@echo ""|"" becomes """ & SafeToEcho("|") & """" strUsage = strUsage & vbCrLf & "@echo." strUsage = strUsage & vbCrLf & "@echo Enter ""Ctrl-C"" to abort the entire process, ""Y"" or ""N"" to step single lines." strUsage = strUsage & vbCrLf & "@echo." strUsage = strUsage & vbCrLf & "@echo." Usage = strUsage End Function Function LongName(strFullPathAndFile) Dim strOriginalFile Dim fs, fil, fils, fol Dim blnFound Set fs = CreateObject("Scripting.FileSystemObject") blnFound = False strOriginalFile = fs.GetFile(strFullPathAndFile).ShortPath Set fol = fs.GetFolder(fs.GetParentFolderName(strFullPathAndFile)) Set fils = fol.Files For Each fil In fils If fil.ShortPath = strOriginalFile Then strOriginalFile = fil.Path blnFound = True Exit For End If Next If blnFound Then LongName = strOriginalFile Else LongName = strFullPathAndFile End If End Function Function IsErrorLevel(strData) If strData = "" Then IsErrorLevel = False Exit Function End If If InStr(1, strData, "errorlevel", vbTextCompare) = 0 Then IsErrorLevel = False Else IsErrorLevel = True End If End Function Function IsLabel(strData) Dim blnIsLabel blnIsLabel = False If Len(strData) > 1 Then If Left(strData, 1) = ":" Then If Left(strData, 2) <> "::" Then blnIsLabel = True End If End If End If IsLabel = blnIsLabel End Function Function IsBlank(strData) If Trim(strData) = "" Then IsBlank = True Else IsBlank = False End If End Function Function IsComment(strData) Dim intCount Const SPECIAL_CHARS = "<>|&" If Len(strData) > 1 Then If Left(strData, 2) = "::" Then 'double colons kill everything on the line IsComment = True Exit Function End If End If If Lcase(Trim(strData)) = "rem" Then 'Nothing except a rem on the line. A do-nothing line. IsComment = True Exit Function End If If Len(Trim(strData)) > 3 Then If Lcase(Left(Trim(strData), 4)) = "rem>" Then 'Method used to create empty files. Looks like a remark, but it isn't! IsComment = False Exit Function End If End If If Len(Trim(strData)) > 3 Then If Lcase(Left(Trim(strData), 4)) = "rem " Then 'Could be a remark unless special characters are used later For intCount = 1 To Len(SPECIAL_CHARS) If InStr(strData, Mid(SPECIAL_CHARS, intCount, 1)) <> 0 Then IsComment = False Exit Function End If Next End If End If IsComment = False End Function Function IsVbs(strData) 'As Boolean 'Not just VBS, but VBS that doesn't already use the //d or //x option 'that was started with the START command and the /Wait option (which 'suggests the output of the script is important to the rest of the batch Dim blnVbs blnVbs = False If strData <> "" Then If InStr(1, strData, "start ", vbTextCompare) <> 0 Then If InStr(1, strData, "/w", vbTextCompare) <> 0 Then If InStr(1, strData, ".vbs", vbTextCompare) <> 0 Then If InStr(1, strData, "//x", vbTextCompare) = 0 Then If InStr(1, strData, "//d", vbTextCompare) = 0 Then blnVbs = True End If End If End If End If End If End If IsVbs = blnVbs End Function Function SafeToEcho(strText) Dim strSafeToEcho strSafeToEcho = strText strSafeToEcho = Replace(strSafeToEcho, ">", Chr(&HF2)) strSafeToEcho = Replace(strSafeToEcho, "<", Chr(&HF3)) strSafeToEcho = Replace(strSafeToEcho, "&", Chr(&HEB)) 'strSafeToEcho = Replace(strSafeToEcho, "%", Chr(&HF6)) strSafeToEcho = Replace(strSafeToEcho, "|", Chr(&HB3)) SafeToEcho = strSafeToEcho End Function