Solved

Batch to Delete lines

Posted on 2014-09-05
7
290 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 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

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 …
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

730 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