Splash
Splash

Reputation: 161

Windows batch file that reads commands from txt file

I have this test.txt file with the following content:

@echo off
wget -q http://subs.ro/get/21518
move 21518 %userprofile%/Desktop/21518.zip
%userprofile%/Desktop/21518.zip

This file is generated by a javascript and the content keeps changes. I have the following text.bat file :

for /F "eol=; tokens=1* delims=" %%i in ( test.txt ) do %%i

the problem is that the link to the desktop is not recognized because the system variable %userprofile% is not recognized, is pasted as a txt string. I am using this setup because I want to convert the bat file to a exe and create an invisible application that does everything in the background.

Upvotes: 1

Views: 4918

Answers (2)

Joey
Joey

Reputation: 354356

Why not rename the file to test.cmd and run it directly?

The following should work, though:

@echo off
for /F "eol=; tokens=1* delims=" %%i in ( test.txt ) do call :run %%i
goto :eof
:run
%*
goto :eof

The reason here is that for itself doesn't expand environment variables in its variables. Probably the only point in batch where this is the case. So I'm just handing the line to a subroutine (run), which does the executing for me.

Upvotes: 3

PA.
PA.

Reputation: 29339

just rename test.txt to test.bat and run it.

Upvotes: 0

Related Questions