0

I'm fairly new to batch scripting, and I need to write a pretty simple .bat file that will loop through a directory. I know how to do it pretty easily using the goto command:

@echo off
:BEGIN
::set variable to data in first file
::do operations on file...
IF ::another file exists in the directory
   ::increment to next file
   GOTO BEGIN
ELSE
   GOTO END
:END
cls

The problem is that's the only way I can think of to do it. I know goto's are generally very frowned upon to use, so I was wondering if anyone knows another way to do this? Thanks!

thnkwthprtls
  • 3,287
  • 11
  • 42
  • 63
  • 3
    Batch is sort of the exception to the "don't use goto" rule, since in most cases, avoiding the use of a Goto will actually make your code more complex and harder to read. Just make sure your labels are consistent, legible, etc. – ChicagoRedSox Aug 30 '13 at 19:31
  • 2
    You can use a `FOR` loop. http://stackoverflow.com/questions/1355791/batch-file-loop – JNYRanger Aug 30 '13 at 19:38

2 Answers2

2

Replace the echo.... with your desired command. From the Command prompt:

for /R %A in (*.*) do echo.%A

In a bat file

for /R %%A in (*.*) do echo.%%A
RGuggisberg
  • 4,630
  • 2
  • 18
  • 27
0

It can be done just for the current folder too. The %%a metavariable is case sensitive and I choose to use lower case. The script will exit and quit when all files are processed.

@echo off
for %%a in (*.txt) do (
type "%%a"
pause
)
foxidrive
  • 40,353
  • 10
  • 53
  • 68