Understandings a Batch file

G

Guest

Hello,

Line bye line, can someone please tell me what is going on in this bat file.
I not sure what is going on in each step. I have a general understanding of
what the bat is doing, but have to test it on a Windows2000/XP platform and
want to completely understand it first. The commands and variables. It is
currently running on a Windows 98. The user also runs this from a command
prompt. This bat file is named C. This is what what the user types at the
command prompt. "c 05212007". The number following c is a folder created
prior to running this script.



@echo off
cls
if "%1"=="" goto bad
if not exist g:\%1\*.* goto bad
dir g:\%1\*.txt /on > c:\windows\temp\dir.xls
g:
cd\%1
echo Record Counts: > c:\windows\temp\records.txt
for %%1 in (*.txt) do find /i /c "ACCT#" %%1 >> c:\windows\temp\records.txt
c:
cd\windows\temp
"c:\program files\microsoft office\office\excel.exe" records.txt dir.xls
formula.xls
goto end
:bad
echo You must enter a valid G drive directory format(MMDDYYYY) Example: %0
01192006
:end
 
L

Lem

SEgerton said:
Hello,

Line bye line, can someone please tell me what is going on in this bat file.
I not sure what is going on in each step. I have a general understanding of
what the bat is doing, but have to test it on a Windows2000/XP platform and
want to completely understand it first. The commands and variables. It is
currently running on a Windows 98. The user also runs this from a command
prompt. This bat file is named C. This is what what the user types at the
command prompt. "c 05212007". The number following c is a folder created
prior to running this script.



@echo off
cls
if "%1"=="" goto bad
if not exist g:\%1\*.* goto bad
dir g:\%1\*.txt /on > c:\windows\temp\dir.xls
g:
cd\%1
echo Record Counts: > c:\windows\temp\records.txt
for %%1 in (*.txt) do find /i /c "ACCT#" %%1 >> c:\windows\temp\records.txt
c:
cd\windows\temp
"c:\program files\microsoft office\office\excel.exe" records.txt dir.xls
formula.xls
goto end
:bad
echo You must enter a valid G drive directory format(MMDDYYYY) Example: %0
01192006
:end

RTFM -->
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds.mspx?mfr=true
 
G

Guest

I've put a comment above each line explaining what the line does.

REM Turn off command echoing
@echo off
REM Clear the screen
cls
REM If no directory specified jump to :bad
if "%1"=="" goto bad
REM If directory is empty jump to :bad
if not exist g:\%1\*.* goto bad
REM Put list of files sorted by name in tmp file dir.xls
dir g:\%1\*.txt /on > c:\windows\temp\dir.xls
REM Change to the G: drive
g:
REM Change to the directory specified
cd\%1
REM Create header in tmp file records.txt
echo Record Counts: > c:\windows\temp\records.txt
REM Add filenames containing ACCT# to records.txt
for %%1 in (*.txt) do find /i /c "ACCT#" %%1 >> c:\windows\temp\records.txt
REM Change to the C: drive
c:
REM Change to tmp directory containing records.txt
cd\windows\temp
REM Run Excel specifying 3 files for Excel to open
"c:\program files\microsoft office\office\excel.exe" records.txt dir.xls
formula.xls
REM Jump to :end
goto end
REM Location to jump if there is an error
:bad
REM Display a use message
echo You must enter a valid G drive directory format(MMDDYYYY) Example: %0
01192006
REM End of the batch file
:end
 
A

AJR

John - nice explanation - learned something!

John Hensley said:
I've put a comment above each line explaining what the line does.

REM Turn off command echoing
@echo off
REM Clear the screen
cls
REM If no directory specified jump to :bad
if "%1"=="" goto bad
REM If directory is empty jump to :bad
if not exist g:\%1\*.* goto bad
REM Put list of files sorted by name in tmp file dir.xls
dir g:\%1\*.txt /on > c:\windows\temp\dir.xls
REM Change to the G: drive
g:
REM Change to the directory specified
cd\%1
REM Create header in tmp file records.txt
echo Record Counts: > c:\windows\temp\records.txt
REM Add filenames containing ACCT# to records.txt
for %%1 in (*.txt) do find /i /c "ACCT#" %%1 >>
c:\windows\temp\records.txt
REM Change to the C: drive
c:
REM Change to tmp directory containing records.txt
cd\windows\temp
REM Run Excel specifying 3 files for Excel to open
"c:\program files\microsoft office\office\excel.exe" records.txt dir.xls
formula.xls
REM Jump to :end
goto end
REM Location to jump if there is an error
:bad
REM Display a use message
echo You must enter a valid G drive directory format(MMDDYYYY) Example: %0
01192006
REM End of the batch file
:end
 
S

Shenan Stanley

SEgerton said:
Line bye line, can someone please tell me what is going on in this
bat file. I not sure what is going on in each step. I have a
general understanding of what the bat is doing, but have to test it
on a Windows2000/XP platform and want to completely understand it
first. The commands and variables. It is currently running on a
Windows 98. The user also runs this from a command prompt. This bat
file is named C. This is what what the user types at the command
prompt. "c 05212007". The number following c is a folder created
prior to running this script.

@echo off
cls
if "%1"=="" goto bad
if not exist g:\%1\*.* goto bad
dir g:\%1\*.txt /on > c:\windows\temp\dir.xls
g:
cd\%1
echo Record Counts: > c:\windows\temp\records.txt
for %%1 in (*.txt) do find /i /c "ACCT#" %%1 >>
c:\windows\temp\records.txt c:
cd\windows\temp
"c:\program files\microsoft office\office\excel.exe" records.txt
dir.xls formula.xls
goto end
echo You must enter a valid G drive directory format(MMDDYYYY)
Example: %0 01192006

Search using Google!
http://www.google.com/
(How-to: http://www.google.com/intl/en/help/basics.html )

1. @echo off

Turns echoing to the screen off. The @ keeps this command from echoing as
well.

2. cls

Clears the screen.

3. if "%1"=="" goto bad

If there was no variables inputed at the command line by the user - skip
down to "bad".

4. if not exist g:\%1\*.* goto bad

If there is nothing in the directory
G:\WhateverTheUserInputttedAtTheCommandLine\ - skip down to "bad".

5. dir g:\%1\*.txt /on > c:\windows\temp\dir.xls

Get a directory listing (alphabetical, by filename) of all the text files in
the G:\WhateverTheUserInputttedAtTheCommandLine\ directory and write that
out to an XLS file in C:\Windows\Temp.

6. g:

Change to the G drive.

7. cd\%1

Change Directories into the "WhateverTheUserInputttedAtTheCommandLine"
directory. (Remember - you're in G now.)

8. echo Record Counts: > c:\windows\temp\records.txt

Put "Record Counts:" into the file c:\windows\temp\records.txt (Literally.)

9. for %%1 in (*.txt) do find /i /c "ACCT#" %%1 >>
c:\windows\temp\records.txt

Parse the text files and find the text "ACCT#" and export the count of how
many times it appears in them to "records.txt"

10. c:

Change to the C drive.

11. cd\windows\temp

Change to the Windows Temp directory...

12. "c:\program files\microsoft office\office\excel.exe" records.txt dir.xls
formula.xls

I assumed that was all one line... Looks like it opens all the documents in
excel. Badly scripted too.

13. goto end

Skips to the "END"

14. :bad

Label for the 'bad' routine...

15. echo You must enter a valid G drive directory format(MMDDYYYY) Example:
%0 01192006

Echos this to the screen/window.

16. :end

Label for the 'end' routine...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top