Read command from .txt file and run it in batch - batch-file

I would like to make batch file read from a .txt and have the output be entered as a command. I have written this code, but it won't work when the command has a spaces in it.
#echo off
:READ
for /f "delims= tokens=*" %%c in (C:\file.txt) do (set command=%%c)
%command%
goto READ
What do I do to make it work?

I suppose this is what you want to do:
#echo off
for /f "tokens=*" %%c in (C:\file.txt) do %%c

I would suggest using this if you need to have the command variable to do other stuff (beside executing) with it. If this is not the case #Henrik's answer would suffice:
#echo off
setlocal EnableDelayedExpansion
:READ
for /f "tokens=* usebackq delims=" %%c in ("C:\file.txt") do (
set "command=%%c"
!command!
)
pause

Related

Batch cutting result string from command

How to properly cut result from command in batch? I wrote something like that:
#ECHO OFF
FOR /F "tokens=* USEBACKQ" %%F IN (`whoami`) DO (
#ECHO %%F%:~7,4%
)
ENDLOCAL
In my case "whoami" returns "europe\archont". After running this batch I receive "europe\archont:~7,4" instead of "arch"
Where I did mistake?
If you have no further functions to perform within your For loop:
#Echo Off
For /F "Delims=" %%A In ('WhoAmI') Do Set "IAm=%%~nxA"
Echo(%IAm:~,4%
Timeout -1
#ECHO OFF
FOR /F "tokens=* USEBACKQ" %%F IN (`whoami`) DO (
set "str=%%F"
#ECHO !str:~7,4!
)
ENDLOCAL
instead of relying on fixed string length, just use the \ as delimiter:
for /f "tokens=2 delims=\" %%F in ('whoami') do set "im=%%F"
when needed, you can cut that:
echo %im:~,4%

Unable to edit text files with shared delimiter

I am trying to write a script that takes a one lined text file containing the output from another script and removes the commas\spaces and replaces them with a carriage return.
Sample
ENTRY_1331_TFS273350_03, ENTRY_1331_TFS282928, ENTRY_1331_TFS292719,
Desired Output
ENTRY_1331_TFS273350_03
ENTRY_1331_TFS282928
ENTRY_1331_TFS292719
I have tried using variations of the below script (doesn't do anything)
for /f "tokens=* delims=, " %%a in (input.txt) do (
echo %%a>> output.txt
echo( >>output.txt
)
or (only echos the first object)
setLocal EnableDelayedExpansion
for /f "delims==, " %%A in (input.txt) do (
set string=%%A
echo !string!>>output.txt
echo( >>output.txt
)
Any help or advice would be appreciated,
Thanks!
Try this.
#ECHO OFF
(FOR /F "USEBACKQ DELIMS=" %%A IN ("input.txt"
) DO FOR %%B IN (%%A) DO ECHO=%%B)>"output.txt"

Batch file to output last line of findstr

I am trying to find a list of machines in files in folders, and print out the last line of the output only.
#echo off
for /f %%a in (computers.txt) do findstr /xs "%%a" unhealthy.txt
pause
The computers.txt file has a list of 300 machines.
I want that to output only the last line of each instance it finds.
Right now the command displays and outputs all instances of the computer name, not just the tail end. I've tried to use "tail for Windows" but am getting errors as well.
Current output:
2013\10-Oct\28\unhealthy.txt:WIN57505
2013\10-Oct\29\unhealthy.txt:WIN57505
2013\10-Oct\30\unhealthy.txt:WIN57505
2013\10-Oct\31\unhealthy.txt:WIN57505
2013\11-Nov\1\unhealthy.txt:WIN57505
2013\11-Nov\4\unhealthy.txt:WIN57505
2013\11-Nov\5\unhealthy.txt:WIN57505
2013\11-Nov\6\unhealthy.txt:WIN57505
I only want:
2013\11-Nov\6\unhealthy.txt:WIN57505
#echo off
setlocal enableextensions disabledelayedexpansion
for /f %%a in (computers.txt) do (
set "line="
for /f "tokens=*" %%b in ('findstr /xs "%%a" *') do set "line=%%b"
setlocal enabledelayedexpansion
echo(!line!
endlocal
)
pause
endlocal
setLocal enableDelayedExpansion
for /f %%a in (computers.txt) do for /f "tokens=*" %%A in ('findstr /xs "%%a"') do set lastFound=%%A
echo !lastFound!

Change letters alphabetically in a for loop to print all tokens

I have a file (MyFile.txt) with a string like this, for example:
var1,var2,asds,123,var6
This file is generated automatically, and I don't know how many words it will have.
I make a for to print all words, one in each line. And process them one by one
for /f "tokens=1-30 delims=," %%a in (MyFile.txt) do call Process.bat %%a
In Process.bat I have ping %1 for example.
This only processes the first token. My problem is, I need to process all tokens, but I need to do it automatically, is there a way to loop over all existing tokens?
In my example: var1,var2,asds,123,var6 it would be: do call Process.bat %%a, then %%b, then %%c, then %%d and %%e.
Thank you.
This will work even if you have various line in Myfile.txt
#echo off&cls
for /f "delims=" %%a in (MyFile.txt) do for %%b in (%%a) do call Process.bat %%b
What about this:
set /p var=<MyFile.txt
set var="%var%"
for %%a in ("%var:,=" "%) do call Process.bat %%a
That Should work.
Mona
you can combine for to read all your lines and call to parse the vars in the line
try this to get you started
#echo off
setlocal enabledelayedexpansion
for /f %%a in (file.txt) do (
set line=%%a
set line=!line:,= !
call :parms !line!
)
goto :eof
:parms
if "%1"=="" goto :eof
echo ping %1
shift
goto :parms
goto :eof

Issue with environment variable

I just want to echo a variable which is defined with ENABLEDELAYEDEXPANSION. It doesn't work.
Here's a small part of my long script on the issue
#echo off&setlocal enabledelayedexpansion
for /f "tokens=*" %%x in (%1) do (
set "D=%%x"
echo %%~nD
)
I have also written echo !~nD! but it doesn't work either.
my file (%1) only contains relative paths as so:
VENDOR\ford1.car
VENDOR\bmw.car
and my goal is to echoing 'ford1.car' or 'bmw.car' because I have to use them in the next steps of my script, that is only the file complete name.
Please some help and explanations. Thanks
Try:
#echo off&setlocal enabledelayedexpansion
for /f "tokens=*" %%a in (%1) do (
echo %%~nxa
)
For only does substitution on the variable used in the for. You can't set a variable within and use substitution on it.
%%~na would only give you the file name without the extension. You have to use %%~nxa to get the file name and the extension.
If you want to set the file name to a variable and do something with each file, you have to use DelayedExpansion like this:
#echo off&setlocal enabledelayedexpansion
for /f "tokens=*" %%a in (%1) do (
set d=%%~nxa
echo !d!
Do something with !d!
)
Or you could create a subroutine and not have to use a variable at all
#echo off&setlocal enabledelayedexpansion
for /f "tokens=*" %%a in (%1) do (
call :sub %%~nxa
)
goto :eof
:sub
%1 = your file name so do some processing on it.

Resources