Search code examples
batch-filebatch-rename

Renaming multiple JSON based on its content using Batch file


This is more sort of a continuation to this question, I referred to the answer section and its fine but it doesn't seem to work for JSON files. The below is my JSON

{
    "id": "ee308826-5aa6-412b-ba65-fd647d9cc8e8",
    "name": "Stub",
    "request": {
        "url": "/World/Wilfred",
        "method": "GET"
    }
}

I need the file to be renamed with "Stub_GET_World_Wilfred.json" which is of the syntax name_method_url ( without the / )

Script from the Original Question :

@echo off

for %%i in (%1) do (
  for /f "tokens=2 delims==" %%j in ('findstr /B /I "Description=" "%%i"') do (
    ren "%%i" "%%j.temp_txt"
  )
)

ren *.temp_txt *.txt

Solution

  • The following commented batch file could be used for this task:

    @echo off
    setlocal EnableExtensions DisableDelayedExpansion
    set "ErrorCount=0"
    set "FileCount=0"
    
    rem Was the batch file called with an argument string to process one file?
    if not "%~1" == "" (
        rem Is there no file (or directory) with the name passed to the batch file?
        if not exist "%~1" (
            set "ErrorCount=1"
            echo ERROR: File "%~1" does not exist.
        ) else call :ProcessFile "%~1"
        goto EndBatch
    )
    
    rem Process all files with the file extension .json in current directory.
    for /F "eol=| delims=" %%I in ('dir *.json /A-D /B 2^>nul') do call :ProcessFile "%%I"
    goto EndBatch
    
    rem Rund FINDSTR on the file (or wrong on directory) in a background command
    rem process started by FOR to find lines with the literal string "url" or the
    rem literal string "method" in any case and process this line by splitting it
    rem into substrings using comma, colon, horizontal tab and normal space as
    rem string delimiters with using first substring without the surrounding quotes
    rem as environment variable name and assigning the second substring without the
    rem surrounding quotes to the environment variable. Please note that this code
    rem does not work for a url with a colon or comma inside.
    
    :ProcessFile
    set /A FileCount+=1
    set "method="
    set "url="
    for /F "tokens=1,2 delims=,:     " %%J in ('%SystemRoot%\System32\findstr.exe /I /L /C:"\"url\"" /C:"\"method\"" "%~1"') do set "%%~J=%%~K"
    
    if not defined url (
        rem There was no string "url" found in the file (or the passed string
        rem was a folder name) or the line with "url" has not a string value.
        set /A ErrorCount+=1
        echo ERROR: Could not find a url in "%~1".
        goto :EOF
    )
    if not defined method (
        rem There was no string "method" found in the file (or the passed string
        rem was a folder name) or the line with "method" has not a string value.
        set /A ErrorCount+=1
        echo ERROR: Could not find a method in "%~1".
        goto :EOF
    )
    
    rem Create the new file name by concatenating Stub_ with the method string
    rem and with the url string read from the file with each slash replaced by
    rem an underscore in url and file extension .json appended. There is not
    rem replaced any percent encoded character in the url by the appropriate
    rem character.
    set "FileName=Stub_%method%%url:/=_%.json"
    
    rem There is nothing to do if the name of processed file
    rem has case-insensitive compared already the right name.
    if /I "%~nx1" == "%FileName%" goto :EOF
    
    rem Next is checked if a file with new name exists already in directory of the
    rem file passed to the batch file which makes it impossible to rename the file.
    if exist "%~dp1%FileName%" (
        set /A ErrorCount+=1
        echo ERROR: A file with name "%FileName%" exits already in "%~dp1".
        goto :EOF
    )
    
    rem Otherwise the file is renamed which can still fail for various reasons.
    ren "%~1" "%FileName%"
    if errorlevel 1 (
        set /A ErrorCount+=1
        echo ERROR: File "%~1" could not be renamed to "%FileName%".
        goto :EOF
    )
    echo Renamed file "%~1" to "%FileName%".
    goto :EOF
    
    
    :EndBatch
    set "Plural_S_Files="
    if not %FileCount% == 1 set "Plural_S_Files=s"
    set "Plural_S_Errors="
    if not %ErrorCount% == 1 set "Plural_S_Errors=s"
    echo(
    echo Proccessed %FileCount% file%Plural_S_Files% with %ErrorCount% error%Plural_S_Errors%.
    if not %ErrorCount% == 0 echo(& pause
    endlocal
    

    ATTENTION: There must be a horizontal tab and a space after delims=,: left to " in the batch file. Please make sure to have only these two characters and not multiple spaces in the batch file after copying and pasting the batch file code.

    To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

    • call /?
    • dir /?
    • echo /?
    • endlocal /?
    • findstr /?
    • for /?
    • goto /?
    • if /?
    • pause /?
    • rem /?
    • ren /?
    • set /?
    • setlocal /?

    See single line with multiple commands using Windows batch file for an explanation of operator &.