1

I'm not familiar with batch, but I do know how to copy paste and move text file to another folder... only things is now I have to add text based on certain key word in all the txt file before copy out...

it goes like this, I have a folder auto generate txt file everyday. I did make a batch to copy them to another folder everyday at a certain time.

But, before doing that.. I have to edit the content of text file whichever contain A123 on 2nd row will need to add a sentence Dept. A on the last row or end of the text.. same goes if contain B123 then add a different last sentence to the text.

So what I have in mind is to filter all txt file then add the text that match the filtering.

So, can this be done?

I tried as below batch code but return error = The filename, directory name, or volume label syntax is incorrect.

@echo off
findstr /c:"^[^ ][^ ]*A123" (*.txt) do type Dept A >> (*.txt)
exit /b

what do I need to add or change?

3
  • Why not use a language other than batch?
    – Gantendo
    Commented Jun 21, 2022 at 4:39
  • which one is more easy?? I have no knowledge on other language
    – Alan.C
    Commented Jun 21, 2022 at 4:44
  • @Alan.C Can you edit your question and add by example the input and output excpected, just for one file
    – Hackoo
    Commented Jun 25, 2022 at 8:32

1 Answer 1

0

Remark : This is just for testing with only one file with drag and drop.

If we had for example the input file like this one :


Hello
Other Stuff here A123
Line 3....
Line 4.....
Line 5......
Line 6.......

With this batch code you can get as output like this one :


Hello
Other Stuff here A123
Line 3....
Line 4.....
Line 5......
Line 6.......
Dept. A

You should Drag and Drop a file for testing on this batch file :

@echo off
Title Read an InputFile line by line and match condition in order to add something at the end of file
Set "OutputFile=%~dp0OutputFile.txt"
If Exist "%OutputFile%" Del "%OutputFile%"
Set "InputFile=%1"
If Not Defined InputFile ( echo(You should Drag and Drop a file on this batch file "%~nx0" ... & Timeout /T 5 /Nobreak>nul & Exit /B)
Setlocal EnableDelayedExpansion
<"!InputFile!" (
    @for /f %%i in ('type "!InputFile!"^|find /c /v ""') do @for /l %%j in (1 1 %%i) do (
        Set "line=" & set /p "line="
        echo(!line! | find /I "A123" && Set "Word2Insert=Dept. A" && echo(!line!>>"!OutputFile!"
        ) || (
            echo(!line!>>"!OutputFile!"
        )
    )
)
echo(!Word2Insert!>>"!OutputFile!"
Start "" /MAX "!OutputFile!"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .