?
Solved

Trying to debug a batch file

Posted on 2011-10-22
8
Medium Priority
?
466 Views
Last Modified: 2012-05-12
Thus far, whenever I've created a batch file (or mooched one from someone else), I've just saved it, run it, and hoped it worked.  Now I have a batch file that paultomasi provided to me that would be fantastic if I could get it running.  Unfortunately, it won't run.  

1. I would imagine it isn't possible to step through a batch file (maybe I'm wrong).  I suppose I could lace the file something like this as alternative.  Examining bat_debug.txt for the benchmarks that appeared and those that didn't would enable me to figure out where the file went bad.

Echo %Date%, %Time%, 'benchmark 1', >> C:\bat_debug.txt
Echo %Date%, %Time%, 'benchmark 2', >> C:\bat_debug.txt
Echo %Date%, %Time%, 'benchmark 3', >> C:\bat_debug.txt

2. It would also be wonderful to get the BAT file to throw actual error codes.  Is that possible?  

Here's the code paultomasi provided.  The objective of the code is extract files out of a complex directory structure and place copies in the parent directory.  The code also appends a "(1)", "(2)", "(3)"... to redundant file names to prevent naming conflicts.
 
@echo off
set rootfolder=Project Folder

for /d %%a in ("%rootfolder%\*") do (
   for /r "%%a" %%b in (*) do (
      if exist "%rootfolder%\%%~nxb" (
         call :duplicatefile "%%b"
      ) else (
         move "%%b" "%rootfolder%\"
      )
   )
)
exit /b


:duplicatefile
   set count=0

   :loop
      set /a count+=1
   if exist "%rootfolder%\%~n1 (%count%)%~x1" goto :loop

   move "%~dpn1 (%count%)%~x1" "%rootfolder%"
goto :eof

Open in new window

0
Comment
Question by:jdana
8 Comments
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 37011594
I'd be adding debug statements and pause to see what's happening exactly and setting echo like this:

:: @echo off
set rootfolder=Project Folder
echo DBG %rootfolder%
pause
for /d %%a in ("%rootfolder%\*") do (
   echo DBG %%a
   pause
   for /r "%%a" %%b in (*) do (
      echo DBG %%a %%b
      pause
      if exist "%rootfolder%\%%~nxb" (
	 echo DBG %rootfolder%\%%~nxb
         pause
         call :duplicatefile "%%b"
      ) else (
         move "%%b" "%rootfolder%\"
	 echo "%%b" "%rootfolder%\"
         pause
      )
   )
)
exit /b

:duplicatefile
   set count=0
   echo DBG called duplicatefile
   pause

   :loop
      set /a count+=1
      echo DBG %count%
      pause
   if exist "%rootfolder%\%~n1 (%count%)%~x1" goto :loop

   move "%~dpn1 (%count%)%~x1" "%rootfolder%"
   echo DBG move %~dpn1 (%count%)%~x1" "%rootfolder%"
   pause
goto :eof

Open in new window


Hope this  helps...
0
 
LVL 71

Accepted Solution

by:
Qlemo earned 800 total points
ID: 37011889
Since there is no trace or stepping in cmd.exe, the "traditional" debugging methods are indeed as shown above - insert debug echos and pause commands, and use @echo on and @echo off wisely to see each command as it is executed, but only for a particular segment of the code. Sadly that doesn't work well for code in parens, where you might or might not see the actual content of variables - and that is when you need to explicitely echo var values.

I would start with a simple folder structure
   a\aa\aaa\file
   a\bb\aaa\file
   a\bb\bbb\filetwo
and try to run the batch on that, to see if it is working at all.
0
 
LVL 59

Assisted Solution

by:Bill Prew
Bill Prew earned 200 total points
ID: 37012483
The script looks reasonable at a quick read, but don't you want to specify the full path in the following like:

set rootfolder=Project Folder

something like:

set rootfolder=C:\Project Folder

What is the symptom of the problem?

As mentioned ECHO and PAUSE are the two best tools in debugging BAT scripts, dieplaying various variable values along the way and inserting pauses where you can check what files have been processed, etc.

~bp
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 39

Expert Comment

by:BillDL
ID: 37014108
Original question was here:
http://www.experts-exchange.com/Q_27380361.html
Forensic company recovered 125 files but they were left in original directory structure some 6 or 7 sub-folders deep.  The request was a batch file to "flatten" the structure by moving all files in all sub-folders back to the topmost parent folder and take account of possible duplicate file names.

Example - Move "Corrected draft 61210.doc" from:
Project Folder\Active\C\$Recycle.Bin\S-1-5-21-1805092725-1900085117-2720843392-1217\Corrected draft 61210.doc
back up to:
Project Folder\Corrected draft 61210.doc

As Bill Prew has stated, don't you need to specify the drive letter in the path for the %rootfolder% variable?

As a side note, if the files are to be used as evidence of any kind you should be working with a COPY of the recovered directory structure otherwise you will probably modify time stamps, etc.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37014391
Just a question... for 125 files I could have found and moved them in explorer in maybe an hour manually... using "search" in explorer like was suggested in the last question, about 2 minutes.  

If this is a one-off do you need a script?!
0
 
LVL 39

Expert Comment

by:BillDL
ID: 37014503
Those were my thoughts too.  There are some great little (some free) programs that can batch copy files, rename if duplicates found, prefix or suffix file name patterns, and optionally retain time/date stamps .... however, it will give Paul something to work out (if you will excuse the expression) while sitting on the toilet doing unique number sequences ;-)
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37014680
haha must look back at that q i suppose.... Did you look at his hangman "for experts" q btw Bill?
0
 

Author Closing Comment

by:jdana
ID: 37046637
Thanks!
0

Featured Post

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

Question has a verified solution.

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

If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
On some Windows 7 (SP1) computers, Windows Update becomes super slow even the computer is reasonably fast.  There's one solution that seemed to have worked well for me (after trying a few other suggested solutions).
This Micro Tutorial will teach you how to the overview of Microsoft Security Essentials. This is a free anti-virus software that guards your PC against viruses, spyware, worms, and other malicious software. This will be demonstrated using Windows…
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.
Suggested Courses

864 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