windows batch script loop through file - shell

How do I write a batch script that loops through each line in a text file and stores the line in a variable in the loop?
For example I have:
#echo off
for /F "tokens=*" %%A in (C:\Users\pmandayam\git\devops\UserRegistrationServices\scripts\file.txt) do (
set var1=%%A
echo %var1%
)
For some reason, the output keeps printing echo is OFF. multiple times, instead of the actual line

Related

Why is my variable not passing all the data to a text file in my batch script?

I'm trying to parse a file that contains data, it has some numbers which I want to parse, store and use them as variables in the future. The problem is, these numbers are updated often so that's why I'm parsing them whenever I run my script.
The problem, is that when I collect the data as a variable in a for loop, it's not getting passed to my text file properly.
#echo off
setlocal EnableDelayedExpansion
for /f "skip=104 eol=/ tokens=4 delims=," %%A in (file location) do (
echo %%A
echo %%A > UICNumbers.txt
)
This is the output of the echo %%A command:
UIC00000701991
UIC00000710996
UIC00001701890
UIC00002701890
UIC00001701898
UIC00002701898
When I open the UICNumbers.txt file all I see listed is:
UIC00002701898
which is the last entry in the for loop variable. I tried changing the output to >> but this means every time I run the command it repeats every entry.

How to catch only the last line of another command executed inside batch script with CALL

I have to write a simple batch script for windows that have to "launch" another script and return only the last line of the output of the launched program.
I need this to catch the "status" of the launched command, that it's printing something like: "End programm with code 0" after many ( and not known number of other line )
What I need is to catch only the last line of myprogram.cmd inside my catch.cmd
I'll try to use CALL inside my catch.cmd script to run myprogram.cmd, but I don't know how to manipulate and get back the last output line.
If I use only CALL myprogram.cmd I'll get all the output inside my catch.cmd, I'll try with CALL myprogram.cmd >NULL to hide all the output inside my catch.cmd, but how to get the last line of the myprogram.cmd?
Thanks for any suggestion
I've created two files.
called.bat:
#echo off
dir c:\
caller.bat:
#echo off
for /f "tokens=*" %%f in ('call called.bat') do set lastLine=%%f
echo %lastLine%
Running caller.bat will execute called.bat "in silence" and save the last line in the variable %lastLine%.
So in your case the anser will be this line:
...
for /f "tokens=*" %%f in ('call myprogram.cmd') do set lastLine=%%f
if "%lastLine%"=="End programm with code 0" (
::DO SOMETHING
)
...

Apply a batch script to all the csv file of my directory

I have a script that allow me to extract a specific columns from a csv file.
My aim subject is to apply my script to all csv files of my directory.
#echo off>fourcol.csv
setlocal
for /f "tokens=1-22* delims=," %%1 in (ManyColumns.csv) do (
echo %%4,%%5,%%6>>fourcol.csv
)
type fourcol.csv
Apply the script to all the csv file in my directory
Remove duplicates
FileA.csv ...FileZ.csv
server1,Dell,1.0.1,server1,Dell,28/06/2016,...
server2,Hp,1.0.2,server2,Hp,29/06/2016,...
server3,Dell,1.2.1,server3,Dell,30/06/2016,...
server4,Hp,1.3.1,server4,Hp,27/06/2016,...
server3,Dell,1.2.1,server3,Dell,30/06/2016,...
server4,Hp,1.3.1,server4,Hp,27/06/2016,...
My CSV files have the same header and the same data.
Output after applying the script (without duplicates):
server1,Dell,1.0.1
server2,Hp,1.0.2
server3,Dell,1.2.1
server4,Hp,1.3.1
How do I apply the script to all the csv files in my directory?
A couple of small changes should do it:
Don't use numbers as for loop variables. They are reserved for command line parameters.
Use dir /b *.csv to get the list of files to process.
Start from token 1 not token 4 to extract the first three columns.
Use the first for loop to process each file, and a second (nested) for loop to process the lines within the file.
Note:
delims=, may not do what you want (if there are values in the file that contain commas ,).
Try the following batch file (test.cmd):
#echo off
setlocal
rem remove output file if it already exists
if exist fourcol.csv del fourcol.csv
rem loop through the csv files
for /f "usebackq" %%a in (`dir /b *.csv`) do (
rem loop through the lines in each file
for /f "usebackq tokens=1,2,3 delims=," %%b in (%%a) do (
echo %%b,%%c,%%d>>fourcol.csv
)
)
rem add code here to strip duplicates
endlocal
test.csv:
server1,Dell,1.0.1,server1,Dell,28/06/2016,...
server2,Hp,1.0.2,server2,Hp,29/06/2016,...
server3,Dell,1.2.1,server3,Dell,30/06/2016,...
server4,Hp,1.3.1,server4,Hp,27/06/2016,...
server3,Dell,1.2.1,server3,Dell,30/06/2016,...
server4,Hp,1.3.1,server4,Hp,27/06/2016,...
fourcol.csv:
server1,Dell,1.0.1
server2,Hp,1.0.2
server3,Dell,1.2.1
server4,Hp,1.3.1
server3,Dell,1.2.1
server4,Hp,1.3.1
Further Reading
An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
dir - Display a list of files and subfolders.
for /f - Loop command against the results of another command.

Script hangs when executing in windows batch file

Hello I have a batch file that is used to move text file from on directory to another. The problem is that when there is a text file larger then 7 MB the scrip hangs and freezes the process, which leads to manually force the batch to end.
Why does this bat hang when it moves larger files then 7mb?. How can I solve this to let it move any size text file?
Thank you in advance for your help.
PS. TYPE was used because the original file is in ANSI/UNIX format and the only way we found to convert it to ANSI/PC was using TYPE.
cd /d "c:\users\you\"
for %%i in (*.txt) do (
echo processing %%i
TYPE "%%i" | MORE /P > "c:\temp\%%i"
del "%%i"
)
The purpose of the more command is to present only one page worth of text at a time. When the output device is not a console, the effective page length is very long but not infinite - it will still pause after writing 65534 lines.
Instead, try this:
(for /F "delims=" %%L in (%%i) do #echo %%L) > "c:\temp\%%i"
Command line breakdown:
for /F - read the contents of a file
"delims=" - don't treat spaces or tabs as delimeters
%%L - the variable (same as the %%i in the for command from your original script)
%%i - the file to read
#echo - writes the variable to standard output
( ) > file.txt - redirects standard output to the destination file

How can i set exe file output to variable in batch file?

I have execution file (print.exe) which will print some numbers.I want to use those numbers. For that I wrote a batch file.
build.bat
set a=print.exe
FOR %I IN a DO prompt.exe %I%
I used the above 2 lines. But its not working. If, it is kernel command, the first line is working. For exe, it didn't work. How can i store the print.exe file output to variable.?
The Batch file below execute print.exe and get its output in numbers variable:
#echo off
for /F "delims=" %%a in ('print.exe') do set numbers=%%a
echo Output of print.exe is: %numbers%

Resources