Solved batch file to read all lines from txt file

August 17, 2009 at 03:56:40
Specs: Windows XP
Hi,
I need to perform following:

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


See More: batch file to read all lines from txt file

Report •


✔ Best Answer
August 19, 2009 at 06:20:52
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



#1
August 17, 2009 at 06:48:26
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


Report •

#2
August 17, 2009 at 06:49:36
for /f %%a in (filename including location) do command

There are no stupid questions, just stupid people.


Report •

#3
August 18, 2009 at 00:40:59
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 .


Report •

Related Solutions

#4
August 18, 2009 at 06:48:50
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


Report •

#5
August 19, 2009 at 02:10:18
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 .


Report •

#6
August 19, 2009 at 06:20:52
✔ Best Answer
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


Report •

#7
August 19, 2009 at 07:04:52
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"


Report •

#8
August 19, 2009 at 15:01:43
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"


Report •

#9
August 20, 2009 at 06:22:03
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


Report •

#10
August 20, 2009 at 09:58:15
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")


Report •

#11
August 20, 2009 at 12:26:01
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.


Report •

#12
August 21, 2009 at 10:47:24
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"
))


Report •


Ask Question