Run command for each file in a directory in MS-DOS

2

I am making a small bat to break a branch on an application that I am mounting, where it should execute sql scripts inside the MySQL database. The problem is that I have to edit this bat every time I insert a new script and this ends getting annoying ...

Is there any way I can get the name of all files inside a folder through the prompt and from there execute a command at the prompt for each file?

    
asked by anonymous 17.12.2015 / 00:39

2 answers

2

If you need to make a script more increment in the future you can use powershell, the line below returns the names of all the files in the specified folder.

(Get-ChildItem "C:\Program Files\MySQL\MySQL Server 5.6"  -File).Name

For a script with menu and option of which file to select follows an example:

$arquivos = (Get-ChildItem "C:\Program Files\MySQL\MySQL Server 5.6\data"  -File).Name
Write-Host "Arquivos encontrados:"
Write-Host "99 - para executa todos."
$i = 0
foreach($item in $arquivos){
    Write-Host $i "-" $item
    $i++
}

$opcao = Read-Host "selecione uma opção: "

if($opcao -eq 99){
    Write-Host "executar todos os arquivos"
}else{
    Write-Host "Arquivo selecionado " $arquivos[$opcao]
}

The result in the terminal is

    
17.12.2015 / 01:27
4

One possibility is to use FOR:

FOR %a IN (*) DO echo %a

The above syntax is to run directly in the CMD. To use in .bat use two % :

FOR %%a IN (*) DO echo %%a

Replace echo with the desired command, and add the parameters you want. Instead of * you can specify the normal

17.12.2015 / 00:54