Batch Read Line From File Into Variable

Sep 02, 2011 Hello everyone, a friend of mine requested that I create a batch script for him that will transform a text file, full of names, into a series of folders with those same names. I know how to do this but I need help with the very first part. Reading from the text file. This is what I have so far for this fuction. Alternatively, I suppose you could slurp the file into an array variable tying each line of the file into arraylineoffileindex using bash's mapfile command. However, I am not sure if it is only for Bash3 higher or Bash4. If your set command supports the /p switch, then you can pipe input that way. Set /p VAR1= to set the value of a variable to a line of input entered by the user. Displays the specified promptString before reading the line of input. File parsing consists of reading the output, string, or file content, and then breaking it into individual lines of text and parsing each line into zero or more tokens. The for loop is then called with the iterative variable value set to the token. By default, /f passes the first blank separated token from each line of each file.

Batch file to read a text file and perform actions

I do a lot of repetitive things on Windows pcs and servers. Often, I’ll need to do the same thing on 10 or more machines, like delete old log files or database backups older than a certain age. In the past, I created a complicated batch file to do a bunch of tasks, but it was annoying to customize it for each location. I tried adding a “profile” for each machine that had path info for common programs and file locations, but that quickly got out of hand too.

I could have installed any one of a number of scripting “languages” to make it easier, but that would have just added another layer of complexity. I just needed a simple way to read a text file and pipe each line to a command. Then I re-discovered FOR…

FOR runs a command for each file in a set of files. You can specify the command and file set on the commandline itself, or read them from other files.

Here’s a couple of references to check out:
Microsoft
ss64

Read lines in a file & echo them

Updated 06/30/2012 with the most basic usage.

This does nothing except read a file line by line and echo it. “%%f” is the parameter (variable name) for the line being read and “echo %%f” is the action that DO performs. Use this as a starting point to create your own batch file.

echo off
FOR /F 'delims=|' %%f in (your_file.txt) DO echo %%f

read_do.bat

Here’s my simple batch file that reads a text file, line by line, and passes each line to another batch file to perform an action. The second variable, “%%g”, is there because DelOld expects 2 parameters and FOR interprets a space in a line as a delimiter. There is a way to avoid the space as a delimiter, but this was easier and the other programs don’t seem to care. You could also pass more variables by using “%%h”, etc.


CLS
@echo off
::#########################################
ECHO #########################################
ECHO Read file and perform action
ECHO Usage: read_do.bat (filename.txt) (action.bat)
ECHO.
ECHO Text files:
dir /B /w *.txt
ECHO.
ECHO Action (.bat) files:
dir /w /B *.bat
ECHO.
ECHO #########################################
ECHO.
REM sanity check
ECHO Text file contents:
FOR /F 'delims=|' %%f in (%1) DO echo %%f %%g
PAUSE
FOR /F 'delims=|' %%f in (%1) DO CALL %2 %%f %%g

The examples below should be self-explanatory.

Batch Read First Line From File Into Variable

Variable

Example 1 – Delete a variety of old, mostly .log, files from known locations

I use the VB version of DelOld (Michna.com) to get rid of old log files, and in some cases backups, after X number of days. DelOld expects a complete path to the file or directory to delete and a value in days for the minimum age of the files.

Into

This is a list of the commandlines I want to send to DelOld. I’ve had mixed results with quoted long paths, but 8.3 style seems to work every time.

delold.txt:

c:docume~1%USERNAME%LocalS~1Applic~12Brigh~1SyncBackLogs*.html 10
c:progra~1logmeinlmi*.log 10
c:backup*rsync*.log 10

delold.bat:

REM path to delold.exe plus filename to delete (%1) & how many days old (%2)
c:delold %1 %2

Example 2 – make several file types contiguous

Use Microsoft (Sysinternals) Contig (TechNet) to make a list of files or file types contiguous.

contigs.txt:

c:*.log
e:*.log
c:*.dat
e:*.dat

contig.bat:

REM path to contig.exe plus file(s) to contig (%1)
c:sysinternalscontig -s %1

Example 3 – copy/update a list of websites for offline browsing or backup

Use wget for Windows (SourceForge) to download a list of sites, each in its own directory and fix the links for local browsing. There’s a bewildering array of options for wget that might suit your needs better than this.

File

sitelist.txt:

http://computingconsultants.com
http://ohio-funeral-homes.com
http://spywarekillers.info
http://betta-fish-care.info

Batch Read Line From File Into Variable Excel

wget_site.bat:


SET dest=pathtosavelocation
C:wgetwget.exe -N -e robots=off -r %1 -P%dest%

Batch Read Line From File Into Variable
Joined
Oct 3, 2008
Messages
21
Line

Batch Read Line From File Into Variable Size

Hi, I want to create a batch program that I am going to run every 15 mins or so. What it does is check the folder for .pdf files, and if it finds one (its 99% guaranteed there will only ever be one) I want it to take the entire name of the file and put it into a variable.
e.g. Folder contains Script.bat and test.pdf
Script.bat gets executed, and within the script a variable is created which contains the value 'test.pdf'. I then do stuff with that variable, and finally use it to delete the pdf file.
It sounds fairly simple really, but I have no idea how to get the name of the file into a variable.
Any help very highly appreciated,