Question

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!

Was it helpful?

Solution

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

OTHER TIPS

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
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top