Computing.Net > Forums > Programming > batch file to read all lines from txt file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

batch file to read all lines from txt file

Reply to Message Icon

Name: Pooja-singh
Date: August 17, 2009 at 03:56:40 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

Hi,
I need to perform following:

To read a txt file and perform some operation for each line.
Could any one help me.



Sponsored Link
Ads by Google

Response Number 1
Name: gtaion
Date: August 17, 2009 at 06:48:26 Pacific
Reply:

Is each line just a command to be run? If so I would use a VB Script that looks similar to this:

Option Explicit
Dim objFS, lines, line, WshShell
Const StrFileName = "c:\YourTextFile.txt"

Set objFS = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("Wscript.Shell")

lines = Split(objFS.OpenTextFile(StrFileName).ReadAll, vbNewLine)
For each line in lines
WshShell.run line
next


0

Response Number 2
Name: xterm11
Date: August 17, 2009 at 06:49:36 Pacific
Reply:

for /f %%a in (filename including location) do command

There are no stupid questions, just stupid people.


0

Response Number 3
Name: Pooja-singh
Date: August 18, 2009 at 00:40:59 Pacific
Reply:

Hi,
May be it is stupid question for you, but I m new in batch programming. Thanks for your support but need some more help.
I need following:

1. check all the .ctl file in a directory and write into abc.txt
2. open abc.txt and fetch each record
3. for each record (which is actually file name) check if size = 0 then put date

Could you please help .


0

Response Number 4
Name: gtaion
Date: August 18, 2009 at 06:48:50 Pacific
Reply:

Not 100% sure what you meant by statment 3, but this VBScript will list all of the files in "objStartFolder" with the extension of .clt in a text named "abc.txt" and any values that are = to 0 will have a date after them.

Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\your\folder"
objTextFile = objStartFolder & "\abc.txt"

If not objFSO.FileExists(objTextFile) then
objFSO.CreateTextFile(objTextFile)
end if

Set objFolder = objFSO.GetFolder(objStartFolder)

Set colFiles = objFolder.Files

For Each objFile in colFiles
if objFile.size = 0 then
strFileSize = (objFile.size & " " & (Date))
else
strFileSize = objFile.size
end if
if lcase(Right(objfile.name,4)) = ".clt" then
objFSO.OpenTextFile(objTextFile, 8).WriteLine (objFile.name & " " & strFilesize)
end if
Next


0

Response Number 5
Name: Pooja-singh
Date: August 19, 2009 at 02:10:18 Pacific
Reply:

Hi,

Thanks for ur reply but I need this in bat file not in VB script.
For more clearification I am writing again:

I need following:

1. check all the .ctl file in a directory and write into abc.txt
2. open abc.txt and fetch each record
3. for each record of abc.txt (which is actually file name) check if size = 0 then put date

Could you please help .


0

Related Posts

See More



Response Number 6
Name: gtaion
Date: August 19, 2009 at 06:20:52 Pacific
Reply:

This is not the prettiest batch, personally I'm a bigger fan of VB Script, I'm still not 100% sure how you want this thing formatted but this will write names to a text, then run back through the names and append the empty files to the end of the text with a date. All you have to do is change folder path it is looking for.

Hint: The Dir /O:S is sorting the files by size, smallest at top.

@echo off
set strFolderPath "C:\Your Folder\"

dir /B /O:S %strFolderPath%*.ctl > %strfolderpath%abc.txt
for /f %%A in (%strFolderPath%abc.txt) do if %%~zA equ 0 echo "%%A %date%" >> %strFolderPath%abc.txt


0

Response Number 7
Name: gtaion
Date: August 19, 2009 at 07:04:52 Pacific
Reply:

Im just not a big fan of how that other batch output's the information, The following outputs the Empty Files to a .csv file with the date. I'm not sure what you need this for, but in my opinion having the output in a .csv makes the information a lot more presentable.

@echo off
set strFolderPath=C:\YourDirectory\Here\
set today=%date:~-4%%date:~4,2%%date:~7,2%

dir /B /O:S "%strFolderPath%"*.ctl > "%strfolderpath%abc.txt"
If not Exist "%strFolderPath%abc.csv" Echo FileName,Date > "%strFolderPath%abc.csv"
for /f "tokens=*" %%A in (%strFolderPath%abc.txt) do if %%~zA equ 0 Echo %%A,%today% >> "%strFolderPath%abc.csv"


0

Response Number 8
Name: gtaion
Date: August 19, 2009 at 15:01:43 Pacific
Reply:

Now that I look at it, you could skip writing to the text file, then reading from it, I think to do exactly what you want you could simply just read directly from the directory and only writing the files that are size 0 to a file. If you notice I changed the "dir sortorder" to sort alphabetically (dir /o:n)

To abc.txt:
@echo off
set strFolderPath=C:\your\directory\
set today=%date:~-4%%date:~4,2%%date:~7,2%
for /f "tokens=*" %%A in ('dir /b /o:n %strFolderPath%*.ctl') do if %%~zA equ 0 Echo %%A - %today% >> "%strFolderPath%abc.txt"

Or you could write to a .csv
@echo off
set strFolderPath=C:\your\directory\
set today=%date:~-4%%date:~4,2%%date:~7,2%
If not Exist "%strFolderPath%abc.csv" Echo FileName,Date > "%strFolderPath%abc.csv"
for /f "tokens=*" %%A in ('dir /b /o:n %strFolderPath%*.ctl') do if %%~zA equ 0 Echo %%A,%today% >> "%strFolderPath%abc.csv"


0

Response Number 9
Name: Pooja-singh
Date: August 20, 2009 at 06:22:03 Pacific
Reply:

Hi gtaion,
ur response 6 is working fine. Thanks alot for this .

Could you please extend ur help for FTP server too as bellow:

now my path is changed from "C:\Your Folder\" to "\\ABCD\DEF\..."

I have tried to do this code after logging into FTP server through FTP command but Set operator is not working on this ..Please help me


0

Response Number 10
Name: gtaion
Date: August 20, 2009 at 09:58:15 Pacific
Reply:

I'll be honest, you are having trouble becuase I was still in the mind frame of writing VBS. But even if I use Set properly I can't get it to seem to work correctly, for some reason I can't get it to read from the text file properly. The following returns the information in the same format where is lists all of the files and then appends the empty files to the end with a date. But it's not reading from the text file. I'll take another look at it after luch but this should get you by.

the strFolderPath needs to end with a \

echo off
set strFolderPath=\\ABCD\DEF\...\

dir /B /O:n "%strFolderPath%*.vbs" > "%strfolderpath%abc.txt"
for /f "tokens=*" %%A in ('dir /b /o:n "%strFolderPath%*.vbs"') do (
if %%~zA equ 0 Echo %%A %date% >> "%strFolderPath%abc.txt")


0

Response Number 11
Name: gtaion
Date: August 20, 2009 at 12:26:01 Pacific
Reply:

Now that I look at it, I can't even get it to read from the textfile, I'm not familiar enough with the DOS commands to figure out where I went wrong, I hope somebody can shed some light on this.

The only way I can get it to read from the list that is inside abc.txt is actually to not have the path to abc.txt included in, meaning that either the batch file needs to be in the same directory as the abc.txt or the batch needs to do a "CD" to the directory containing the abc.txt which it doesn't like the UNC path.


0

Response Number 12
Name: gtaion
Date: August 21, 2009 at 10:47:24 Pacific
Reply:

Okay I think I'm getting somewhere here, (RIF) Reading is Fundamental. I was reading the FOR help file and found that to use the path name with "" I needed to specify Usebackq to specify a filenameset. Then that being said, it was reading through the text file seeing the filenames as strings not files, so I couldn't check the filesize cause it wasn't a file, so I had to pass each filename back through the For /F command but it couldn't find the file 'cuase the batch isn't in the same directory, so I set the VAR variable to be the FolderPath and the Filename then ran that through the For /F command to check the size, and then if it met the parameters of being a zero size it appends the filename to the end of the ABC.txt. Also please make sure the strFolderPath ends with "\" (I didn't do it and it through me off for awhile)

Please let me know if this works for you, my curiousity is peaked.

There has got to be a better way to do this?

@echo off
set strFolderPath=\\ABCD\EFG\HIJ\

setlocal ENABLEDELAYEDEXPANSION
dir /B /O:S "%strFolderPath%*.ctl" > "%strfolderpath%abc.txt"
for /f "tokens=* usebackq" %%A in ("%strFolderPath%abc.txt") do (
set VAR=%strFolderPath%%%A
for /f "tokens=* usebackq" %%B in ('!VAR!') do (
If %%~zB equ 0 echo %%A %date% >> "%strFolderPath%abc.txt"
))


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: batch file to read all lines from txt file

Read all lines from a txt file www.computing.net/answers/programming/read-all-lines-from-a-txt-file/19395.html

Read lines from .txt file in DOS? www.computing.net/answers/programming/read-lines-from-txt-file-in-dos/15219.html

Batch to del specific lines in txt www.computing.net/answers/programming/batch-to-del-specific-lines-in-txt/13482.html