Search code examples
batch-filefor-loopfindstr

Windows Batch How to delete specific words from txt file


I can't figure out how to remove a piece of text in a line.

I have a batch file that creates a list of files of interest to me in the selected folder.

dir D:\pool\template_test>U:\Desktop\list.txt
findstr "Work_T" U:\Desktop\list.txt > U:\Desktop\tamplates.txt

Output:

2013-03-13  17:24               622 Work_T_tamplate1.fdf

In final file, as you can see, presents the date, time, size, but I only need the name. How can I do this?

I tried to use the example from the other post, but it does not work:

for /f "tokens=3" %%A in (U:\Desktop\tamplates.txt) do findstr /C".dot" /C".pdf" /C".fdf" %%A

final file after using proposed code


Solution

  • So you already got an answer in comments by Stephan

    These will include the search for you, the following only returns the filename:

    cd /d "D:\pool\template"    
    (@for /f %%i in ('dir /b "Work_T*.dot" "Work_T*.pdf" "Work_T*.fdf" ') do (
        @echo "%%i"
      )
    )>"U:\Desktop\tamplates.txt"
    

    You could however also want the full path, so this would include the full path of the file:

    cd /d "D:\pool\template"    
    (@for /f %%i in ('dir /b "Work_T*.dot" "Work_T*.pdf" "Work_T*.fdf" ') do (
        @echo "%%~fi"
      )
    )>"U:\Desktop\tamplates.txt"
    

    add /s to recurse through subfolders if needed.

    Edit

    for UNC paths:

    (@for /f %i in ('dir /b /a-d "\\tuesrna02\pool\template" ^| findstr /i "Work_T"') do (
        @echo %%i
      )
    )>"U:\Desktop\tamplates.txt"