Solved

Batch to Delete lines

Posted on 2014-09-05
7
288 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
  • 3
  • 3
7 Comments
 
LVL 69

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 69

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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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 69

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:W.E.B
ID: 40306967
Super,
thanks again.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

827 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