Solved

Batch to Delete lines

Posted on 2014-09-05
7
286 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

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…
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 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…
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…

773 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