Home > front end >  Duplicate & Edit Text File using Batch File
Duplicate & Edit Text File using Batch File

Time:10-05

I'm trying to duplicate a JSON file 99 times and with each duplicate, the text for that file is edited and specific lines are changed before it loops to the next. Since the initial file labeled 0.json already exists the batch will need to start off at 1 and loop to 99 before pausing. Where it gets complicated is due to the fact that the files must be named 0 - 99 instead of 1 - 100. So some of the numbers that need to be changed inside each JSON file will vary. 2 sets of numbers will stay the same as the file name itself. However, there will be 2 other numbers that are 1 higher since the JSON files begin at 0.json.

Below is an example of the lines and the values that need to be changed.

 File name = %%A.json
 Line 2    = "name": "MY NAME #%%B",
 Line 6    = "image": "%%A.png",
 Line 12   = "{"trait_type": "Limited", "value": %%B of 100},
 Line 15   = "files": [{"uri": "%%A.png", "type": "image/png"}],

This is File 0.json that will be duplicated.

 File name = 0.json
 Line 2    = "name": "MY NAME #1",
 Line 6    = "image": "0.png",
 Line 12   = "{"trait_type": "Limited", "value": 1 of 100},
 Line 15   = "files": [{"uri": "0.png", "type": "image/png"}],

Desired Output on the 1st Loop

 File name = 1.json
 Line 2    = "name": "MY NAME #2",
 Line 6    = "image": "1.png",
 Line 12   = "{"trait_type": "Limited", "value": 2 of 100},
 Line 15   = "files": [{"uri": "1.png", "type": "image/png"}],

Desired Output on the 2nd Loop Etc..

 File name = 2.json
 Line 2    = "name": "MY NAME #3",
 Line 6    = "image": "2.png",
 Line 12   = "{"trait_type": "Limited", "value": 3 of 100},
 Line 15   = "files": [{"uri": "2.png", "type": "image/png"}],

I have been unable to successfully get the batch to work in this way. How would I be able to edit these specific lines while I am duplicating the JSON file in the batch?

Current Code

 @echo off
 for /l %%A in (1,1,100) do (
    Copy "D:\MINT MACHINES\0.json" "D:\MINT MACHINES\%%A.json"
 )
 pause>nul

CodePudding user response:

After this question was put on hiatus for over 36 hours because it was "flagged" as if I was trying to sell life insurance by a few characters, I eventually came across a method that worked. Most definitely not the most eloquent of methods but it does do the trick.

I downloaded FART (find and replace text) and placed the executable in the same folder as my BAT file.

I placed a specific keyword in the lines that needed to be modified in my initial JSON file. Example:

 Line 1: This line has the number that will be modified. Here is the #NUM1
 Line 2: This line also has a # to be modified. Here is the #NUM2
 Line 3: etc...

Then the BAT File:

 @echo off
 Setlocal EnableDelayedExpansion

 SET /A a=2
 SET /A b=1

 for /l %%A in (1,1,99) do (
    Copy "D:\MINT MACHINES\test\0.json" "D:\MINT MACHINES\test\%%A.json"

    FART "D:\MINT MACHINES\test\%%A.json" NAMEKEY !a!
    FART "D:\MINT MACHINES\test\%%A.json" #IMAGE !b!
    FART "D:\MINT MACHINES\test\%%A.json" #TRAIT !a!
    FART "D:\MINT MACHINES\test\%%A.json" #FILE !b!

    SET /A a=a 1
    SET /A b=b 1
 )
 pause>

Since our initial file starts off as the 1st file but is labeled zero. Then two sets of the numbers are 1 and the other two sets are 0. Then the variables for the BAT file start off as a=2 & b=1.

 for /l %%A in (1, 1, 99) do (
    Copy "D:\MINT MACHINES\test\0.json" "D:\MINT MACHINES\test\%%A.json"

This piece of code above begins the loop and copies the initial JSON file 99 times so that there will be a total of 100 JSON files in the end labeled 0 - 99.

While in the loop, the next 4 lines of code execute FART which finds the keywords (NAMEKEY, #IMAGE, #TRAIT, #FILE) and replaces them with the current string for the variables assigned to each line. So that:

 Line 2  changes from NAMEKEY to #2
 Line 6  changes from #IMAGE to 1
 Line 12 changes from #TRAIT to 2
 Line 15 changes from #FILE to 1

Typically, a variable in a BATCH file will display as %Variable%. However, since we're executing variables within a loop. It must be !Variable! and you must setlocal to EnableDelayedExpansion.

Finally, before repeating the loop I needed to update the variables and increase the set number by one, I did this as listed below:

 SET /A a=a 1   
 SET /A b=b 1 

This worked flawlessly and went through 99 files almost instantly. As I said above, it's not the most elegant solution. I am sure there are plenty of other more present methods that could do this without having to dip into code that hasn't been used since windows 3.1.

Hopefully, it is of some benefit to someone else in the future who can't find straight answers in layman's terms and is gun-shy from posting questions for fear of members ridiculing and/or blocking their questions from receiving that help due to their inability to clearly explain what they need help with.

  • Related