Delete rows of data from txt files as per 1.

bsharath
bsharath used Ask the Experts™
on
Hi,

Delete rows of data from txt files as per 1.
I have 1000's of txt files. Data like this in each file. Can be of any font/Size or order.
The exact match has to be deleted.

Windows xp
Windows 2003 Sp2
Microsoft project
Acrobat reader 7.0

I have such names in a lot of files. Say 100's of such software names. I want to remove some specific software names from all the txt files. Whose path is mentioned in the script.

I have 1 txt file called "input.txt" when run each software name from this file has to be deleted in all.

Regards
Sharath
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
I have not tested this. I hope it works as expected.

NOTE: change: 'c:\path\*.txt' to the source drive and folder of your text files.
           input.txt file contains a list of software names you want deleting from your source text files.




@echo off
setlocal enabledelayedexpansion

for /f "tokens=*" %%a in ('dir /a-d /b c:\path\*.txt') do (
   del file.tmp 2>nul

   for "tokens=*" %%b in ('type "%%~dpnxa"') do (
      findstr /i "%%b" input.txt
      if not !errorlevel!==1 (
         echo %%b>>file.tmp
      )
   )
   move /y file.tmp "%%~dpnxa"
)

Commented:
Updated: Added double-quotes around source filespec to account for spaces.



@echo off
setlocal enabledelayedexpansion

for /f "tokens=*" %%a in ('dir /a-d /b "c:\path\*.txt"') do (
   del file.tmp 2>nul

   for "tokens=*" %%b in ('type "%%~dpnxa"') do (
      findstr /i "%%b" input.txt
      if not !errorlevel!==1 (
         echo %%b>>file.tmp
      )
   )
   move /y file.tmp "%%~dpnxa"
)
 

Author

Commented:
Hi,

I get this

D:\>Remove_Data_Softwares.bat
"tokens=*" was unexpected at this time.
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Commented:
I missed off an '/F' in the second FOR loop... Apologies....Here's the code again




@echo off
setlocal enabledelayedexpansion

for /f "tokens=*" %%a in ('dir /a-d /b "c:\path\*.txt"') do (
   del file.tmp 2>nul

   for /f "tokens=*" %%b in ('type "%%~dpnxa"') do (
      findstr /i "%%b" input.txt
      if not !errorlevel!==1 (
         echo %%b>>file.tmp
      )
   )
   move /y file.tmp "%%~dpnxa"
)

Commented:
Try this one instead (doesn't print to the screen):



@echo off
setlocal enabledelayedexpansion

for /f "tokens=*" %%a in ('dir /a-d /b "c:\path\*.txt"') do (
   del file.tmp 2>nul

   for /f "tokens=*" %%b in ('type "%%~dpnxa"') do (
      findstr /i "%%b" input.txt >nul
      if not !errorlevel!==1 (
         echo %%b>>file.tmp
      )
   )
   move /y file.tmp "%%~dpnxa" >nul
)

Author

Commented:
I get this

D:\>Remove_Data_Softwares.bat
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.

Commented:
is it referring to this line here?:

   for /f "tokens=*" %%a in ('dir /a-d /b "c:\path\*.txt"') do (

The "c:\path\*.txt" must refer to your source folder - the folder containing your text files to be searched. Obviously, my code shows just an example path, you will need to plug in your own path to replace this.


Another possibility is this line:

   findstr /i "%%b" input.txt >nul

This points to the file INPUT.TXT which is the name of your list of filenames. Also, make sure the system knows where this file is - in other words, include it's full path it it isn't in the same folder as this batch file. You could change it to something like the following for example:

   findstr /i "%%b" "c:\path\input.txt" >nul

Finally, it would help me to include all the proper data into the batch file if you could supply the following information:

1) what is the name of the file which contains the list of software (is it INPUT.TXT)?

2) what is the full path of that file (where on the hard drive can it be found)?

3) what is the full path of the text files you want to search and amend?

4) what is the full path of where you will be saving this batch file to (and running it from)?


Author

Commented:
Here is the code that i changed..Still same message
@echo off
setlocal enabledelayedexpansion
 
for /f "tokens=*" %%a in ('dir /a-d /b "D:\Temp\*.txt"') do (
   del file.tmp 2>nul
 
   for /f "tokens=*" %%b in ('type "%%~dpnxa"') do (
       findstr /i "%%b" "D:\input.txt" >nul
 
      if not !errorlevel!==1 (
         echo %%b>>file.tmp
      )
   )
   move /y file.tmp "%%~dpnxa" >nul
)

Open in new window

Commented:
Yep, it appears to be right.... please hang on while i set up a test environment with data, and test it...

Commented:
Please try this:


@echo off
setlocal enabledelayedexpansion

set sourcefolder=d:\temp
set textfiles=%sourcefolder%\*.txt
set inputfile=d:\input.txt
set tempfile=%temp%\%~n0.tmp

echo.
for /f "tokens=*" %%a in ('dir /a-d /b "%textfiles%"') do (
   del "%tempfile%" 2>nul
   if /i not "%%a"=="%inputfile%" (
      set /p .=%%a.<nul
      for /f "tokens=*" %%b in ('type "%sourcefolder%\%%a"') do (
         set /p .=.<nul
         findstr /r /c:"\<%%b\>" "%inputfile%" >nul
         if not !errorlevel!==0 (
            echo %%b>>"%tempfile%"
         )
      )
      echo.
   )
   move /y "%tempfile%" "%sourcefolder%\%%a" >nul  
)
echo.

Author

Commented:
Thank u worked perfect....

Commented:
No probs....however, a lesson i know only too well - even the most innocent looking code can be deceptively bug-ridden.

Thank you for the points and grading.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial