0

This is probably the exact opposite of this question.

Anyways, I have a batch file with such content:

a.bat

@echo %random%

And I tries to read the a.bat through another batch file:

for /f "delims=" %%p in ('dir *.bat') do (
    rem loop through files
    for /f "delims=" %%q in ('type %%~p') do echo %%q 
) 2>nul

And it outputs:

@echo (random number)

How can I make it outputs @echo %random%? Any help will be appreciated.

  • I'm not sure what you are testing, but your described result can't be the output of your code – jeb May 23 '17 at 10:48
  • Weird. On my Win7 it outputs `@echo (the random number)` –  May 23 '17 at 10:49
  • Is it always the same number? What is the real content of x.bat? – jeb May 23 '17 at 11:42
  • The number isn't always same as it is `%random%`. The real content would be too long to fit here, but anyways I used another workaround and removed the old script `:(` –  May 23 '17 at 11:43

1 Answers1

3

Your code works here (win 8). Only added the /B switch to dir command.

for /f "delims=" %%p in ('dir /B a.bat') do (
    rem loop through files
    for /f "delims=" %%q in ('type %%~p') do echo %%q 
) 2>nul

But you can also try

SetLocal DisableDelayedExpansion
for /f "delims=" %%p in ('dir /B a.bat') do (
  for /F "tokens=1,* delims=[]" %%1 in ('"type "%%~p"|find /N /V """') do echo/%%2
)
EndLocal

In my computer both give @echo %ramdon%

elzooilogico
  • 1,659
  • 2
  • 17
  • 18
  • Typo in the last sentence... By the way, the second solution works like a charm :) –  May 23 '17 at 10:53
  • It fails, when a line starts with `[`, `]` or `?`. See also [SO:How to read a file](https://stackoverflow.com/a/4531090/463115) – jeb May 23 '17 at 11:45