Link to home
Start Free TrialLog in
Avatar of sparker1970
sparker1970Flag for United States of America

asked on

Batch Script File to delete a row from a text file

I have a text file I receive daily from a client.

I want to maintain the integrity of the original file
I want 2 additional files created:    
          "Record07_mmddyyyy.txt" with all rows from the original file where the 1st 2 positions are "07"
          "ImportFile_mmddyyyy.txt" with all rows from the original file where the 1st 2 positions are NOT "07"

Files will be in the same folder as the batch file will be saved and run from.
ee-HELP-FILE.TXT
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Use findstr command x 2. Sure someone will give you exact command line, off hand you need option for beginning of line and search literal string.  One can use / v to show all but search string, one not... Quick post from mobile...
@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%A in ('findstr "07" "testinput.txt"') do (
  echo %%A >>"outFile.txt"
  goto :break
)
:break

Open in new window

Oops forgot not matching:

@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%A in ('findstr "07" "testinput.txt"') do (
  echo %%A >>"outFile.txt"
)
for /f "delims=" %%A in ('findstr /v "07" "testinput.txt"') do (
  echo %%A >>"outFile2.txt"
)

:break

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of oBdA
oBdA

findstr like that will find "07" anywhere in the line (including things like 01ev2     2F2 JONES LN           439.07).
This script uses WMI to get the date, and makes sure 07 is found only when at the beginning of a line:
@echo off
setlocal enabledelayedexpansion
set InFile=D:\Temp\test.txt

set /a Line=0
for /f "tokens=1-9" %%a in ('wmic Path Win32_LocalTime Get Day^,DayOfWeek^,Hour^,Minute^,Month^,Quarter^,Second^,WeekInMonth^,Year ^| find /v ""') do (
  set /a Line += 1
  if "!Line!"=="1" (set VarA=%%a&set VarB=%%b&set VarC=%%c&set VarD=%%d&set VarE=%%e&set VarF=%%f&set VarG=%%g&set VarH=%%h&set VarI=%%i)
  if "!Line!"=="2" (set !VarA!=%%a&set !VarB!=%%b&set !VarC!=%%c&set !VarD!=%%d&set !VarE!=%%e&set !VarF!=%%f&set !VarG!=%%g&set !VarH!=%%h&set !VarI!=%%i)
)
for %%a in (Month Day Hour Minute Second) do (if !%%a! LSS 10 set %%a=0!%%a!)
set TimeStamp=%Year%%Month%%Day%

set OutFile1=D:\Temp\Record07_%TimeStamp%.txt
set OutFile2=D:\Temp\ImportFile_%TimeStamp%.txt
type "%InFile%" | findstr /r "^07" >"%OutFile1%"
type "%InFile%" | findstr /v /r "^07" >"%OutFile2%"

Open in new window

Avatar of sparker1970

ASKER

Great solutions. I am going to experiment on the batch script that allows you to drag over the batch file to run it. This will save me the extra step of renaming the file each time to run the batch program. I did look up the findstr command and did see that the /b switch will look at the beginning of the line for the string variable. I checked that because of the follow up post that said it would pick up the "07" from anywhere in the file. I did not see this problem when I ran it even though I saw many situations where this could have occurred.
Good luck with it.  Effectively if you drag a file, directory whatever onto a batch file icon the path of the file is passed to the batch as "%1".  With batch file trickery you can get different parts of that if you want

%1 - as it was passed by explorer
%~1 - remove any quotes.  So you can add own later to make sure
%~d1 - drive
%~p1 - path
%~n1 - filename
%~x1 - extension
%~f1 - fully qualified...

If you then do:

@echo off
if "%~1"=="" (
  echo Drag a file onto this icon to convert it
  exit /b
)
pushd "%~dp1"
echo I am now in the dir of the file dragged on, a temporary drive mapped if it was a UNC: %cd%
echo So now I can work on "%~nx1" in this dir...
pause
I used your drag n drop script and it worked perfectly to accomplish my needs. I'll definitely be using that option more going forward.