我正在研究一个翻译项目,该项目的内容位于JSON文件的一级深度文件夹结构中。 本质上看起来像这样
\folder1
file1.json
file2.json
...
\folder2
file3.json
file4.json
...
我需要一些自动化的方法来实现
\folder1\file1.json
<file1 content>
\folder1\file2.json
<file2 content>
\folder2\file3.json
<file3 content>
\folder2\file4.json
<file4 content>
我正在使用Windows10。
答案 0 :(得分:0)
这可能有帮助,
for /r %I IN (*) DO echo.&echo %I>>%I
然后使用copy
实用程序将所有文件复制到单个目标,
copy /Y /V c:\folder1\file1.json+c:\folder2\file2.json c:\target\destination\result.json
答案 1 :(得分:0)
从当前工作目录向下合并在第一个目录级别中找到的所有JSON文件:
@echo off
rem // Write all output to a single file:
> "merged.json" (
rem // Walk through the first directory level:
for /D %%J in ("*") do (
rem // Walk through all files per directory:
for %%I in ("%%~J\*.json") do (
rem // Return relative path to current file preceded by `.\`:
echo .\%%~I
rem // Return content of current file:
type "%%~I"
rem // Return blank line:
echo/
)
)
)
在合并的JSON文件中,每个相对的源文件路径都以.\
开头。
从当前工作目录开始向下分解到第一目录级别的JSON文件(请注意,没有完成初始目录/文件清理):
@echo off
rem // Clear buffer holding relative path of current file:
set "FILE="
rem // Read merged file line by line:
for /F "usebackq delims= eol=#" %%K in ("merged.json") do (
rem // Store current line text:
set "LINE=%%K"
rem /* Enable delayed expansion in order to become able to
rem write and read the same variable within the loop: */
setlocal EnableDelayedExpansion
rem // Check whether current line begins with `.\`:
if "!LINE:~,2!"==".\" (
rem // Current line begins with `.\`, so treat as relative file path:
endlocal
rem // Create first directory level:
2> nul md "%%K\.."
rem // Create empty file to append to:
> "%%K" rem/
rem // Store resolved relative file path for later use:
set "FILE=%%K"
) else (
rem // No leading `.\` found, hence append current line to recent file:
>> "!FILE!" echo/!LINE!
endlocal
)
)
关键是将以.\
开头的所有行都视为单个源文件的相对路径。