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

Asked

Viewed 880 times

3

I’m making a small bat to break a branch over an application I’m mounting, where it should run sql scripts within the Mysql database. The problem is that I have to edit this bat every time I insert a new script and it ends up getting boring...

Is there any way to make through the prompt I can pick up the name of all the files inside a folder and from this run a command at the prompt for each file?

  • 1

    Which command will you run at the prompt?

  • mysql -u root < %cd% scripts\ sql filename.

2 answers

4

One possibility is to use FOR:

FOR %a IN (*) DO echo %a

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

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

Replace the echo by the desired command, and add the parameters you want. Instead of the * you can specify the wildcards system normal, for file name and extension.

If you want to do some operation with listing in text files, you can get the directory names like this:

DIR *.* /b > listagem.txt

The /b flag is to take the file path. Use the /s flag if you want to include subdirectories.

  • could give me an example using the command mysql -u root < %cd% scripts filename.sql ? remembering that before I need to get his name with the is that you informed...

  • 1

    @Leandroluk your problem is completely different from what you asked. If you need to run multiple sqls within Mysql, it makes no sense to depend on .bat. But if you do bat anyway, it’s more or less: FOR %a IN (*) DO mysql -u root < %a only you’ll have to put the password open.

2


If you need to make a script plus increment in the future you can use the 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 from 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

inserir a descrição da imagem aqui

  • 1

    Do you have a problem with the answer?

  • actually don’t need it, and even so I have to consider that we only have the native powershell in windows 8 forward if I’m not mistaken... also don’t need menu, just need it to read the files in the folder and then run them in Mysql

  • 1

    @Leandroluk powershell is available in version 2 for XP and vista, V3 can be installed in windows 7 and is already installed in 8 and 2012. This information is in documentation near the end of the page.

  • OK, I understand I’m going to give a read using the example you gave me...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.