Solved

Batch to Delete lines

Posted on 2014-09-05
7
275 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:Wass_QA
  • 3
  • 3
7 Comments
 
LVL 68

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:Wass_QA
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 68

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:Wass_QA
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 68

Accepted Solution

by:
Qlemo earned 500 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:Wass_QA
ID: 40306967
Super,
thanks again.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now