[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1111
  • Last Modified:

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
0
sparker1970
Asked:
sparker1970
  • 3
  • 2
  • 2
  • +1
1 Solution
 
Steve KnightIT ConsultancyCommented:
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...
0
 
becraigCommented:
@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

0
 
becraigCommented:
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

0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Steve KnightIT ConsultancyCommented:
I meant like this.  

findstr /b "07" <infile.txt > Record07.txt
findstr /b /v "07" <infile.txt > ImportFile.txt

Open in new window


To get the date then I use my technique here normally... I would normally suggest using yyyymmdd to make it easily sortable but if you want ddmmyyyy it just needs adjusting the order:
http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html

@echo off
echo wscript.echo year(date) ^& right(100 + month(date),2) ^& right(100+day(date),2)  > "%temp%\dateparts.vbs"
for /f "tokens=1 delims=" %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do set yyyymmdd=%%a
findstr /b "07" < infile.txt > Record07_%yyyymmdd%.txt
findstr /b /v "07" < infile.txt > ImportFile_%yyyymmdd%.txt

Open in new window


ddmmyyyy would be:

@echo off
echo wscript.echo right(100+day(date),2) ^& right(100 + month(date),2) ^& year(date)  > "%temp%\dateparts.vbs"
for /f "tokens=1 delims=" %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do set yyyymmdd=%%a
findstr /b "07" < infile.txt > Record07_%yyyymmdd%.txt
findstr /b /v "07" < infile.txt > ImportFile_%yyyymmdd%.txt

Open in new window


You could also pick up a file dragged onto the icon/batch file using %~1, e.g.

@echo off
if "%~1"=="" (
  echo Drag a file onto this icon to convert it
  exit /b
)
echo wscript.echo right(100+day(date),2) ^& right(100 + month(date),2) ^& year(date)  > "%temp%\dateparts.vbs"
for /f "tokens=1 delims=" %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do set yyyymmdd=%%a
findstr /b "07" < "%~1" > Record07_%yyyymmdd%.txt
findstr /b /v "07" < "%~1" > ImportFile_%yyyymmdd%.txt

Open in new window


Steve
0
 
oBdACommented:
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

0
 
sparker1970Author Commented:
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.
0
 
Steve KnightIT ConsultancyCommented:
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
0
 
sparker1970Author Commented:
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.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now