placidomaio
placidomaio

Reputation: 111

Create a bat files based of a list of files

I need to create a bat files based on a list of files.

Example of file list (the name of the files can be change):

vpn_1.1.1.1_udp_11111.ovpn
vpn_2.2.2.2_tcp_22222.ovpn
vpn_3.3.3.3_udp_33333.ovpn
vpn_10.10.10.10_tcp_1010.ovpn

I need a bat that can create a complete code based on the list of files like:

start /low /max "" "D:\Programmi Installati\Openvpn Portable\OpenVPNPortable.exe" --connect vpn_1.1.1.1_udp_11111.ovpn
ping -n 10 localhost >nul 2>&1
taskkill.exe /F /IM openvpn.exe /IM openvpn-gui.exe /IM OpenVPNPortable.exe

start /low /max "" "D:\Programmi Installati\Openvpn Portable\OpenVPNPortable.exe" --connect vpn_2.2.2.2_tcp_22222.ovpn
ping -n 10 localhost >nul 2>&1
taskkill.exe /F /IM openvpn.exe /IM openvpn-gui.exe /IM OpenVPNPortable.exe

start /low /max "" "D:\Programmi Installati\Openvpn Portable\OpenVPNPortable.exe" --connect vpn_3.3.3.3_udp_33333.ovpn
ping -n 10 localhost >nul 2>&1
taskkill.exe /F /IM openvpn.exe /IM openvpn-gui.exe /IM OpenVPNPortable.exe

start /low /max "" "D:\Programmi Installati\Openvpn Portable\OpenVPNPortable.exe" --connect vpn_10.10.10.10_tcp_1010.ovpn
ping -n 10 localhost >nul 2>&1
taskkill.exe /F /IM openvpn.exe /IM openvpn-gui.exe /IM OpenVPNPortable.exe

After create the code I need to save the bat in a prefered path.

The number and the files name can be change (in this example I write 4 files but files can be more)

All solutions provided actually for me do not work (do not recognize path of the file), U continue to search a working solution to my batch files.

Can you help me please ?

Thanks for support

Best regards

Upvotes: 0

Views: 167

Answers (2)

Aacini
Aacini

Reputation: 67216

@echo off

(for /F "delims=" %%a in (filesList.txt) do (
   echo start /low /max "" "D:\Programmi Installati\Openvpn Portable\OpenVPNPortable.exe" --connect %%a
   echo ping -n 10 localhost ^>nul 2^>^&1
   echo taskkill.exe /F /IM openvpn.exe /IM openvpn-gui.exe /IM OpenVPNPortable.exe
   echo/
)) > "prefered\path\created.bat"

EDIT: Reply to the comments

I tested my program with the data you provided. This is filesList.txt file I used in my test:

vpn_1.1.1.1_udp_11111.ovpn
vpn_2.2.2.2_tcp_22222.ovpn
vpn_3.3.3.3_udp_33333.ovpn
vpn_10.10.10.10_tcp_1010.ovpn

And this is the created.bat file:

start /low /max "" "D:\Programmi Installati\Openvpn Portable\OpenVPNPortable.exe" --connect vpn_1.1.1.1_udp_11111.ovpn
ping -n 10 localhost >nul 2>&1
taskkill.exe /F /IM openvpn.exe /IM openvpn-gui.exe /IM OpenVPNPortable.exe

start /low /max "" "D:\Programmi Installati\Openvpn Portable\OpenVPNPortable.exe" --connect vpn_2.2.2.2_tcp_22222.ovpn
ping -n 10 localhost >nul 2>&1
taskkill.exe /F /IM openvpn.exe /IM openvpn-gui.exe /IM OpenVPNPortable.exe

start /low /max "" "D:\Programmi Installati\Openvpn Portable\OpenVPNPortable.exe" --connect vpn_3.3.3.3_udp_33333.ovpn
ping -n 10 localhost >nul 2>&1
taskkill.exe /F /IM openvpn.exe /IM openvpn-gui.exe /IM OpenVPNPortable.exe

start /low /max "" "D:\Programmi Installati\Openvpn Portable\OpenVPNPortable.exe" --connect vpn_10.10.10.10_tcp_1010.ovpn
ping -n 10 localhost >nul 2>&1
taskkill.exe /F /IM openvpn.exe /IM openvpn-gui.exe /IM OpenVPNPortable.exe

The created .bat file contains exactly the same code you posted above as the requested output, so I don't understand what is your concern... :(

IMPORTANT: In order to have a common base, you must do the same thing I did, that is, copy the "Example of file list" above and paste it in a file named filesList.txt, copy my program in a .bat file and run it. Then, compare the created.bat file with the requested code you posted above. You must confirm that you completed this test in any further comment you may post...

Upvotes: 1

Magoo
Magoo

Reputation: 79983

Simple:

for /f "delims=" %%a in (textfilecontainingyourovpnfilenames) do (
 start /low /max "" "D:\Programmi Installati\Openvpn Portable\OpenVPNPortable.exe" --connect %%a
 timeout /t 10 >nul
 taskkill.exe /F /IM openvpn.exe /IM openvpn-gui.exe /IM OpenVPNPortable.exe
)

That is, read each line from the textfile and then run the procedure.

Note that timeout will provide the 10-sec delay and >nul suppress timeout's countdown prompts.

Upvotes: 0

Related Questions