?
Solved

Batch to Delete lines

Posted on 2014-09-05
7
Medium Priority
?
295 Views
Last Modified: 2014-09-05
Hello,
Can you please help,
I have few .txt files in a specific  Directory.
C:\Users\Wassim\__Task_Schedules\_Files_FTP

I need to Remove lines from text files that contains
qlcmd:
or
Error:

and save the file again. (same name)

thank you
0
Comment
Question by:W.E.B
[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
  • 3
  • 3
7 Comments
 
LVL 70

Expert Comment

by:Qlemo
ID: 40305990
@echo off
for/F "tokens=*" %%F in ('findstr /v "qlcmd: Error:" C:\Users\Wassim\__Task_Schedules\_Files_FTP\*.txt ') do (
  findstr /v "qlcmd: Error:" "%%~F" > "%%~F.$$$"
  move "%%~F.$$$" "%%~F" /y
)

Open in new window

We are using findstr twice to make sure we do not touch files not containing those lines.
0
 

Author Comment

by:W.E.B
ID: 40306056
Hi Qlemo,
Thanks  for your help,

Nothing changed when I run the batch.
lines with   qlcmd:  or Error:  are still there

thanks,
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 40306106
Sorry, forgot to remove something:
@echo off
for/F "tokens=*" %%F in ('findstr "qlcmd: Error:" C:\Users\Wassim\__Task_Schedules\_Files_FTP\*.txt ') do (
  findstr /v "qlcmd: Error:" "%%~F" > "%%~F.$$$"
  move "%%~F.$$$" "%%~F" /y
)

Open in new window

The search is case sensitive, as I think that is your intention.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:W.E.B
ID: 40306130
Hi Qlemo,

sorry, still not working,

is it because I have other texts on the same line?

thanks,
0
 
LVL 1

Expert Comment

by:rwniceing
ID: 40306815
You can try this  using forfiles to search all txt file at your directory (or some other dir such as "c:\" including sub-directory with /s option), and save command out in junk.txt file and do the findstr search for "qlcmd: Error:"  for each file from junk.txt file list, and finding output is saved with the same file name. If you  need sub-folder search, just add  "/s" option on forfiles command, and more forfiles details you can find it at  http://ss64.com/nt/forfiles.html
@echo off
forfiles /p "C:\Users\Wassim\__Task_Schedules\_Files_FTP"  /m *.txt  /C "cmd /c echo @path ">junk.txt
echo -------Start-------------------------
for /f "tokens=1 delims=" %%F in ('type C:\Users\Wassim\__Task_Schedules\_Files_FTP \junk.txt') do (
set "findtxt=qlcmd: Error:"
set inputfile=%%F
set "outputfile=tempfile"
 if exist !outputfile! del !outputfile!
 for /f "delims="  %%i in ('findstr /v  "%findtxt%" !inputfile!') do (
    set "line=%%i"
    setlocal enabledelayedexpansion
echo !line!>>"!outputfile!"
   endlocal
)
if exist !outputfile! (
      copy !outputfile! !inputfile!
		echo Message:Success !inputfile!
		del !outputfile!
	) else ( 
		echo NOTE: Nothing done
	)
	)
REM del junk.txt
echo -------Batch file Done------------------------- 

Open in new window

0
 
LVL 70

Accepted Solution

by:
Qlemo earned 2000 total points
ID: 40306945
#@~! There is another typo in my code. You should have seen error messages hinting towards that. This time the code is tested:
@echo off
for /F "tokens=*" %%F in ('findstr /m "qlcmd: Error:" C:\Users\Wassim\__Task_Schedules\_Files_FTP\*.txt ') do (
  findstr /v "qlcmd: Error:" "%%~F" > "%%~F.$$$"
  move /y "%%~F.$$$" "%%~F"
)

Open in new window

0
 

Author Closing Comment

by:W.E.B
ID: 40306967
Super,
thanks again.
0

Featured Post

Introducing Priority Question

Increase expert visibility of your issues by participating in Priority Question, our latest feature for Premium and Team Account holders. Adjust the priority of your question to get emergent issues in front of subject-matter experts for help when you need it most.

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 …
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month15 days, 14 hours left to enroll

743 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