使用批处理文件根据其内容重命名多个 JSON

Renaming multiple JSON based on its content using Batch file

这更像是此 question 的延续,我参考了答案部分,它很好,但它似乎不适用于 JSON 文件。下面是我的JSON

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

我需要用语法 name_method_url(不带 / )的“Stub_GET_World_Wilfred.json”重命名文件

原始问题的脚本:

@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

以下注释批处理文件可用于此任务:

@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 done on name of processed file has case-insensitive
rem 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

注意:在批处理文件中delims=,:左到"之后必须有一个水平制表符和一个space。复制粘贴批处理文件代码后,请确保批处理文件中只有这两个字符,不能有多个space。

要了解使用的命令及其工作原理,请打开 command prompt window,在其中执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。

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

有关运算符 & 的说明,请参见 single line with multiple commands using Windows batch file