Jason Brown
Jason Brown

Reputation: 137

Batch file for /f isnt reading files

Lots of issues With batch coding... Things just don't seem to work the way i expect them to.

So I am Using a batch file to Extract Text data from .as Files. I managed to get that working However It creates a bunch of Junk/empty txt files that don't have any useful content extracted from them. So I made another batch file that gets called from main.bat and is supposed to Clean the empty files, however the variables are incorrect even though the for loop in main.bat is almost identical.

Full Copies of the batch files main.bat and Clean.bat

The Issue Is the Bit of Code below (From Clean.bat line 33) #note the following code had most of its counters and echos removed

@echo off cls setlocal enabledelayedexpansion for /r %%Z in (*.txt) do ( SET /a count=0 for /F "eol= tokens=1 delims=," %%A in ("%%Z") do ( if /i %%A==LN set /a count+=1 ) if !count! EQU 0 ( rem del "%%Z") ) pause

In this Code %%A and %%Z Are both Equal to The Full File Path of the file that should be getting read from. When %%A should be token 1 of The txt file in question. Because of this count always = 0 so it always deletes the file (thats why del is commented out).

Here's an Example of the file its supposed to read from LN,296,textE("海沿いに立つ高級フィットネスリゾート施設。"); LN,299,textE("夏休み、俺たち兄妹は、陸上の強化合宿という「名目」のもと、\nこの施設を訪れていた。"); LN,302,textE("莉 央\nすごい、すごい!!おしゃれなところだねー!");

Basically Its supposed to check each line and if LN doesnt Exist as the first token to any line it deletes the file. (Because that means the file is empty except for a Line count of the Original .as file)

Upvotes: 0

Views: 250

Answers (1)

foxidrive
foxidrive

Reputation: 41234

This may work for you given the things you have said:

I interpreted your point to mean that if the file doesn't contain LN as the first token on any line, then it is to be deleted.

@echo off
for /r %%Z in (*.txt) do (
    SET "count="
    for /F "usebackq delims=," %%A in ("%%Z") do (
        if /i "%%A"=="LN" set count=1
    )
    if not defined count echo del "%%Z"
)
pause

This is another way to do the same thing:

@echo off
for /r %%Z in (*.txt) do (
    findstr "^LN," "%%Z" >nul || echo del "%%Z"
)
pause

Upvotes: 1

Related Questions