Home > Enterprise >  Editing content in txt files using batch
Editing content in txt files using batch

Time:11-05

I'm needing some help with a content editing
My goal is to use a batch script and for it to find all id_? found inside txt files
and change ever letter from Uppercase to Lowercase

This is my script

EditFiles.bat

@echo off

set "replace=id_A"
set "replaced=id_a"

set "source=2Folder\File5002014.txt"
set "target=3Folder\File50020141.txt"

setlocal enableDelayedExpansion
(
   for /F "tokens=1* delims=:" %%a in ('findstr /N "^" %source%\*.txt') do (
      set "line=%%b"
      if defined line set "line=!line:%replace%=%replaced%!"
      echo(!line!
   )
) > %target%
endlocal

I managed to get it to find a pattern
the script above does work and will edit the content in a txt file but will only edit the matching name
I did try to change it to read any file but can't figure it out

I have over 100 txt files and all have different id_? content
what I need this to do is

FIND
id_AaPPle
id_A
id_BeRRy
id_B
id_CoRn
id_c...etc


REPLACE
id_apple
id_a
id_berry
id_b
id_corn
id_c...etc

Keep in mind I have over 100 different words after the id_?

and I need it to scan any txt file found in 2Folder and only edit the files inside the 2 Folder

This is what I tried and it failed My first attempt was to make it edit the files only in the 2Folder

EditFiles.bat

@echo off

set "replace=id_a"
set "replaced=id_A"

set "source=2Preparing\File5002014.txt"

setlocal enableDelayedExpansion
(
   for /F "tokens=1* delims=:" %%a in ('findstr /N "^" %source%\*.txt') do (
      set "line=%%b"
      if defined line set "line=!line:%replace%=%replaced%!"
      echo(!line!
   )
) >> %source%
endlocal

the only way I could figure out how to edit more id_? was to repeat the codes like this

@echo off

set "replace1=id_A"
set "replaced1=id_a"

set "source=2Preparing\File5002014.txt"

setlocal enableDelayedExpansion
(
   for /F "tokens=1* delims=:" %%a in ('findstr /N "^" %source%\*.txt') do (
      set "line=%%b"
      if defined line set "line=!line:%replace1%=%replaced1%!"
      echo(!line!
   )
) > %target%
endlocal

set "replace2=id_aPPle"
set "replaced2=id_apple"

set "source=2Preparing\File5002014.txt"

setlocal enableDelayedExpansion
(
   for /F "tokens=1* delims=:" %%a in ('findstr /N "^" %source%\*.txt') do (
      set "line=%%b"
      if defined line set "line=!line:%replace2%=%replaced2%!"
      echo(!line!
   )
) > %target%
endlocal

OK this set

set "replace1=id_A"
set "replaced1=id_a"

is to find every id_A and change it to id_a now not every word has mixed upper and lower case letters
For example I have the word id_Grapes here I just have to worry about the G

So I would make a script to do this

set "replace1=id_G"
set "replaced1=id_g"

Any help with this would be great

Thank you

Here is how it looks inside my files

File 1.txt
{
      "upma": "id_Apples",
      "upnn": 20,
      "upfb": true,
      "upcu": false
}
{
      "upma": "id_APpleS",
      "upnn": 25,
      "upfb": false,
      "upcu": false
}

File 2.txt
{
      "upma": "id_BeRRy",
      "upnn": 10,
      "upfb": true,
      "upcu": false
}
{
      "upma": "id_BerRy",
      "upnn": 20,
      "upfb": false,
      "upcu": false
}

File 3.txt
{
      "upma": "id_Corn",
      "upnn": 29,
      "upfb": true,
      "upcu": false
}
{
      "upma": "id_Corn",
      "upnn": 40,
      "upfb": false,
      "upcu": false
}

Pretty much this is repeated many times per file.txt

My results

File 1.txt
{
      "upma": "id_apples",
      "upnn": 20,
      "upfb": true,
      "upcu": false
}
{
      "upma": "id_apples",
      "upnn": 25,
      "upfb": false,
      "upcu": false
}

File 2.txt
{
      "upma": "id_berry",
      "upnn": 10,
      "upfb": true,
      "upcu": false
}
{
      "upma": "id_berry",
      "upnn": 20,
      "upfb": false,
      "upcu": false
}

File 3.txt
{
      "upma": "id_corn",
      "upnn": 29,
      "upfb": true,
      "upcu": false
}
{
      "upma": "id_corn",
      "upnn": 40,
      "upfb": false,
      "upcu": false
}

CodePudding user response:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following settings for directory names are those
rem that I use for testing and deliberately include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"
SET "destdir=u:\your results"
SET "alphabet=a b c d e f g h i j k l m n o p q r s t u v w x y z"
SET "indent=      "

FOR %%e IN ("%sourcedir%\F*") DO (
 rem filename in %%e
 (
 FOR /f "usebackqdelims=" %%y iN ("%%e") DO (
  rem raw line in %%y
  FOR /f "tokens=1*delims=: " %%b iN ("%%y") DO (
   IF "%%~b"=="upma" (
    CALL :lower %%c
    ECHO %indent%%%b: "!lowered!",
   ) ELSE ECHO %%y
  )
 )
 )>"           
  • Related