Solved

How do I read only the first line of a text file using batch

Posted on 2010-09-13
18
713 Views
Last Modified: 2012-05-10
what I'm trying to do is have a batch file check mulitple files if it finds the string I'm looking for on
the first line of the file move it to a specific location.  The below code works but if it finds F44Q
anywhere inside the file it moves it.  The first line contains the code of where to move it.

cd c:\eom\print\
findstr /m "F44Q" * >F44Q.TXT
for /f %%A in (F44Q.txt) DO MOVE %%A C:\EOM\ORGS\F44Q\
0
Comment
Question by:PCDJAYS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
  • 3
18 Comments
 
LVL 55

Expert Comment

by:Bill Prew
ID: 33661360
Here's one approach.  You can also do it with the PROMPT command, but since you asked for the FOR approach.

cd c:\eom\print\
findstr /m "F44Q" * >F44Q.TXT
for /f %%A in (F44Q.txt) DO {
  MOVE %%A C:\EOM\ORGS\F44Q\
  goto :ExitFor
}
:ExitFor

~bp
0
 
LVL 55

Expert Comment

by:Bill Prew
ID: 33661381
You can also do this without the temporary file if you wanted, reading the output of the FINDSTR directly into the FOR, like:

cd c:\eom\print\
for /f %%A in ('findstr /m "F44Q"') DO {
  MOVE %%A C:\EOM\ORGS\F44Q\
  goto :ExitFor
}
:ExitFor

more info here:

http://ss64.com/nt/for_cmd.html

~bp
0
 

Author Comment

by:PCDJAYS
ID: 33661576
I have multiple codes in the files on the first line that determine what folder it belongs in  The fix that you helped me with still found the string F44Q on other lines than the first one.  Any line after the first one may have the F44Q but doesn't belong in the F44Q directory.
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 43

Accepted Solution

by:
Steve Knight earned 500 total points
ID: 33661927
The only way I can think to do it off hand is to use findstr to find the relevant files that have it in somewhere for speed then check the first line of each file.   Skipping out of a loop within a loop can be a bit messy so I put it in CALL's subroutine.

Steve

@echo off
setlocal enabledelayedexpansion
set search=F44Q
cd /d c:\eom\print

set line=
for /f "tokens=*" %%A in ('findstr /m "%search%" *.txt') do call :check %%A

exit /b

:check
for /f "tokens=*" %%L in ('type "%~1"') do (
  set line=%%L
  if not "!line!"=="!line:%search%=!" (
    echo %1.  Do MOVE here
  )
  exit /b
)

0
 

Author Comment

by:PCDJAYS
ID: 33662349
that didn't seem to work at all.  I replaced the word here with where I wanted the files to go.  when it runs it doesn't appear to do anything
0
 

Author Comment

by:PCDJAYS
ID: 33662492
below is the code i tried from the last post

@echo off
setlocal enabledelayedexpansion
set search=F44Q
cd /d C:\users\chris.knapp\eom\print\

set line=
for /f "tokens=*" %%A in ('findstr /m "%search%" *') do call :check %%A

exit /b

:check
for /f "tokens=*" %%L in ('type "%~1"') do (
  set line=%%L
  if not "!line!"=="!line:%search%=!" (
    echo %1.  Do MOVE %1 C:\users\chris.knapp\eom\orgs\F44Q\
  )
  exit /b
)
0
 

Author Comment

by:PCDJAYS
ID: 33663034
if this helps the search word i need to find in each file is always the first second or third word on the first line
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 33663096
It should work like this, have tested it as is.  It isn't actually moving files at the moment, only echoing to the screen (sorry if obvious!).  Is it showing anything?
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 33663113
It should run findstr down each file to find the text and any it finds it in it then parses line 1 for the text.  Add an echo %~1 just after the :check line to see each file that findstr found for starters.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 33663128
If you replace the :check section as below it will show a bit more:

:check
echo ... START %~1
for /f "tokens=*" %%L in ('type "%~1"') do (
  set line=%%L
  echo ... %~1 , !line!
  if not "!line!"=="!line:%search%=!" (
    echo %1.  Do MOVE %1 C:\users\chris.knapp\eom\orgs\F44Q\
  )
  exit /b
)
0
 

Author Comment

by:PCDJAYS
ID: 33663403
it appears to be finding the correct files but doesn't move them  this is the move line
echo %1.  Do MOVE %1 C:\users\chris.knapp\eom\orgs\F44Q\

0
 

Author Comment

by:PCDJAYS
ID: 33663415
i checked and the location I'm trying to move it to is correct
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 33663853
ok then it is working but not running a move command yet, the echo is just showing you what it could do. Leave the echo if you wish then add a move command to the next line or replace the whole echo line with just a move command.

Will type it up like that for you when not on mobile in a bit!
0
 

Author Closing Comment

by:PCDJAYS
ID: 33663907
It was almost perfect had one little problem needed to remove the do statement from the move lien
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 33664125
No problem,  I had mean't it as "do your move here" rather than a command already.  Sorry if it caused confusion.

Steve
0
 
LVL 55

Expert Comment

by:Bill Prew
ID: 33668603
Steve,

I'm really swamped at the office this week, but had hoped to try this slightly differently.  Why not do the FINDSTR with the /N option?  This will produce a single output of all lines in all files, and then that couple be fed into a second FIND looking for ":1:".  That resultant list would be just the files that had the desired string in the first line.  Then that output could be processed in the FOR /F grabbing off the filename at the left.

Try this on the folder where the files are to get the idea:

findstr /N "F44Q" * | find ":1:"

Feels easier, what you think Steve?

~bp
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 33674366
Nice!  I didn't know you could use /N with the multiple files method and have it actually display the filename on each line, but it seems that it does -- I had asumed it worked the same as FIND which only shows the filename once and the found line with line number in [], i.e.

find /N "F44Q" *.txt | find "[1]" works but only shows the line not the filename...

In which case then I would scrap what you have and replace it with something as suggested by Bill:

for /f "tokens=1 delims=:" %%a in ('findstr /N "f44Q" * ^| find ":1"') do (
  echo Found line 1 as F44Q in %%a
  MOVE "%%a" etc.
)

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 33674379
Damn extra : missing there in the second find command (as otherwise it will find line 1x 1xx 1xxx etc. too)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question