Home > Net >  Batch file to remove folder paths from all .json files in folder
Batch file to remove folder paths from all .json files in folder

Time:12-01

I have a number of .json files that out put with folders / directories as part of asset names. I'd like a .bat file that can run through all the .jsons in a folder and remove the folder paths it finds.

example

"audio": "F:/GCProjects/HighRoller/Art/Working/Animation/Wild"

Becomes

"audio": "Wild"

example 2

{ "name": "wild_eye_whites", "bone": "wild_head", "attachment": "Wild/wild_eye_whites" },

becomes

{ "name": "wild_eye_whites", "bone": "wild_head", "attachment": "wild_eye_whites" },

is that possible?

{
"skeleton": {
    "hash": "zqXkKNuGfDk",
    "spine": "4.0.64",
    "x": -87,
    "y": -71,
    "width": 165.31,
    "height": 142,
    "images": "",
    "audio": "F:/GCProjects/HighRoller/Art/Working/Animation/Wild"

    { "name": "FrameMask", "bone": "FrameGlow", "attachment": "FrameMask" },
    { "name": "retrigger_glow", "bone": "retrigger_glow", "color": "ffffff00", "attachment": "retrigger_glow", "blend": "additive" },
    { "name": "mask_Character", "bone": "mask", "attachment": "mask_Character2" },
    { "name": "wild_cigarello", "bone": "wild_cigarello", "attachment": "wild_cigarello" },
    { "name": "wild_back_arm", "bone": "wild_back_arm", "attachment": "wild_back_arm" },
    { "name": "wild_body", "bone": "wild_character", "attachment": "Wild/wild_body" },
    { "name": "wild_hipArm", "bone": "wild_character", "attachment": "Wild/wild_hipArm" },
    { "name": "wild_hair_4", "bone": "wild_head", "attachment": "Wild/wild_hair_4" },
    { "name": "wild_eye_whites", "bone": "wild_head", "attachment": "Wild/wild_eye_whites" },
    { "name": "wild_eyes", "bone": "eyeCntrl", "attachment": "Wild/wild_eyes" },
    { "name": "wild_face", "bone": "wild_head", "attachment": "Wild/wild_face" },
    { "name": "wild_front hair", "bone": "wild_head", "attachment": "Wild/wild_front hair" },
    { "name": "wild_tiara", "bone": "wild_head", "attachment": "Wild/wild_tiara" },
    { "name": "wild_dressGlow", "bone": "wild_character", "color": "ffffff00", "attachment": "Wild/wild_dressGlow", "blend": "additive" },
    { "name": "GlowPath", "bone": "FrameGlow", "attachment": "GlowPath" }

Using visual studio code and find and replace with regular expressions I get the wanted results with ((\w|-| ) /)

I was trying to take something like

::Delete the character string 'ab' and everything before it
   SET _test=12345abcabc
   SET _result=%_test:*ab=% 
   ECHO %_result%          =cabc

and alter it to remove everything before "/"

CodePudding user response:

cmd is not the best tool to use for these kinds of tasks, but it is not impossible though. The issue is, you are accessing the file as plain text, so many things can go wrong when your file contains strings you did not consider. Anyway, that said, here is one way:

@echo off
setlocal enabledelayedexpansion
set "file=yourfilename.json"
for /f "tokens=1,*delims=]" %%i in ('type "%file%" ^| find /v /n "" ^& break^>"%file%"') do (
    set "ln=%%j"
    if not "!ln!" == "" set "ln=!ln:,=","!"
    set "res="
    for %%a in (!ln!) do (
      set "val=%%a"
      if not "!val:/=!" == "!val!" (
          set "val="%%~nxa""
     )
        set "res=!res! !val!"
        set "res=!res:","=,!"
   )
  echo(!res!>>%file%
)

CodePudding user response:

This should work:

@echo off
setlocal EnableDelayedExpansion

del *.out 2> NUL

for %%f in (*.json) do (
   echo Processing: %%f
   (for /F "tokens=1* delims=:" %%x in ('findstr /N "^" "%%f"') do (
      set "line=%%y"
      if defined line if "!line:/=!" neq "!line!" (
         for /F "delims=" %%b in ("!line:*/=!") do set "tail=%%b"
         for /F "delims=" %%b in ("!tail!") do set "head=!line:%%b=!"
         for %%b in (!head!) do (
            set "name="
            for %%c in ("%%~b!tail:~0,-1!") do if not defined name set "name=%%c"
         )
         for %%b in (!name!) do set "line=!line:%%~b=%%~Nb!"
      )
      echo/!line!
   )) > "%%~Nf.out"
)

echo/
echo Files created:
dir *.out
  • Related