I have following windows-batch script to loop through all files in the current directory:
FOR %%F in (%CD%\*.*) DO (
:: I am doing my process here
)
I know I can loop through a specific file type by using *.ext
, but I need to loop through all the given below file types and ignore all other types, that also in single FOR
loop only:
php
phtml
css
js
sql
xml
How can I achieve that by doing slightest modifications to my code as possible ?
I am not much of a batch-scripter, so any help would be much appreciated.
What about using this command line in your batch file?
for %%I in (*.php *.phtml *.css *.js *.sql *.xml) do echo "%%I"
Or perhaps better depending on what you want to do:
for /F "delims=" %%I in ('dir /A-D /B *.php *.phtml *.css *.js *.sql *.xml 2^>nul') do echo "%%I"
The version with DIR works also for files with hidden attribute set and is better when files in current directory are renamed, moved or deleted, i.e. the current directory files list changes due to the commands in the body of FOR loop because of list of files processed by FOR does not change once DIR command is executed which is not the case on using first solution. The first solution can easily result in an unexpected behavior like double processing a file, skipping unexpected files or an endless running loop when files are renamed, moved, modified or deleted in current directory.
Run in a command prompt window for /?
and dir /?
for help on those two commands which support both specifying multiple wildcard patterns on arguments list.