You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

批处理脚本批量生成递增Unix时间戳实现求助

批处理时间戳批量修改脚本实现

核心功能说明

  • 读取指定目录下所有*.txt文件,定位"timestamp":字段
  • 按规则替换值:以初始Unix时间戳为基准,每4个对象用同一值,每完成4个后基准值递增25
  • 支持两种操作模式:从源目录复制文件到目标目录后修改,或直接修改目标目录现有文件

完整脚本代码

@echo off
setlocal enabledelayedexpansion

:: 可配置参数
set "source_dir=New folder 1"
set "target_dir=New folder 2"
set "batch_size=4"
set "increment=25"

:: 操作模式选择
:mode_select
cls
echo 选择操作模式:
echo 1. 从%source_dir%复制文件到%target_dir%后修改
echo 2. 直接修改%target_dir%中的文件
set /p "mode=输入选项(1/2):"
if "%mode%"=="1" goto copy_modify
if "%mode%"=="2" goto direct_modify
echo 无效选项,请重新输入
pause >nul
goto mode_select

:: 复制+修改模式
:copy_modify
if not exist "%target_dir%" mkdir "%target_dir%"
xcopy "%source_dir%\*.txt" "%target_dir%\" /Y /Q
goto process_files

:: 直接修改模式
:direct_modify
if not exist "%target_dir%" (
    echo 错误:目标目录%target_dir%不存在
    pause >nul
    exit /b 1
)
goto process_files

:: 获取初始Unix时间戳(纯批处理实现)
:get_unix_timestamp
for /f %%a in ('wmic os get localdatetime ^| findstr /r "^[0-9]"') do set "dt=%%a"
set "year=!dt:~0,4!"
set "month=!dt:~4,2!"
set "day=!dt:~6,2!"
set "hour=!dt:~8,2!"
set "minute=!dt:~10,2!"
set "second=!dt:~12,2!"

:: 计算从1970-01-01到当前时间的秒数
set /a "yy=year-1970"
set /a "mm=month-1"
set /a "days=yy*365 + yy/4 - yy/100 + yy/400"
for %%m in (0 31 59 90 120 151 181 212 243 273 304 334) do (
    if !mm! gtr %%m set /a "days+=31" else if !mm! equ %%m set /a "days+=%%m"
)
set /a "days+=day-1"
if !month! gtr 2 if !yy!%%4 equ 0 if not !yy!%%100 equ 0 if !yy!%%400 equ 0 set /a days+=1
set /a "unix_ts=days*86400 + hour*3600 + minute*60 + second"
exit /b

:: 批量处理文件
:process_files
call :get_unix_timestamp
set "current_ts=%unix_ts%"
set "count=0"

echo 开始处理文件...
for %%f in ("%target_dir%\*.txt") do (
    set "temp_file=%%~dpnf_temp.txt"
    del /f /q "!temp_file!" 2>nul
    
    :: 逐行扫描替换
    for /f "usebackq delims=" %%l in ("%%f") do (
        set "line=%%l"
        echo !line! | findstr /c:"\"timestamp\":" >nul
        if not errorlevel 1 (
            :: 替换timestamp值
            set "line=!line:"timestamp": 0="timestamp": !current_ts!"
            set /a count+=1
            :: 每4个完成后递增时间戳
            if !count! equ %batch_size% (
                set /a current_ts+=increment
                set count=0
            )
        )
        echo !line! >> "!temp_file!"
    )
    
    :: 替换原文件
    del /f /q "%%f"
    ren "!temp_file!" "%%~nxf"
    echo 已处理:%%~nxf
)

echo 所有文件处理完成!
pause >nul
endlocal

关键细节说明

  1. Unix时间戳计算:通过wmic获取系统时间,手动计算从1970年以来的秒数,无需依赖外部工具
  2. 延迟变量扩展:启用enabledelayedexpansion确保循环中变量实时更新,避免批量处理时的变量缓存问题
  3. 批量计数逻辑:用count变量跟踪已处理的timestamp数量,达到设定的批量数后自动递增时间戳
  4. 文件安全处理:使用临时文件逐行替换,避免直接修改原文件导致的内容损坏

整合到现有菜单脚本的方法

将上述代码作为独立子程序,在现有菜单中添加对应选项,通过call调用即可:

:: 现有菜单示例
:main_menu
cls
echo 主菜单
echo 1. 批量处理时间戳文件
echo 2. 其他功能1
echo 3. 其他功能2
echo 4. 退出
set /p "choice=输入选项:"
if "%choice%"=="1" call :timestamp_processing
if "%choice%"=="2" goto function1
if "%choice%"=="3" goto function2
if "%choice%"=="4" exit /b
goto main_menu

:: 插入时间戳处理子程序
:timestamp_processing
setlocal enabledelayedexpansion
:: 这里放入前面的配置参数、模式选择、处理逻辑等代码(去掉开头的@echo off和endlocal)
endlocal
goto main_menu

注意事项

  • 确保源/目标目录路径不含特殊字符,若有空格需用引号包裹
  • 脚本会覆盖目标目录的同名文件,操作前建议备份原始文件
  • 若原文件中"timestamp":后的数值不是0,需修改替换逻辑中的"timestamp": 0为对应匹配值

内容的提问来源于stack exchange,提问作者JR Santos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 13:16:17