【问题标题】:export server log file from event viewer and save into a folder without overwrite the file从事件查看器导出服务器日志文件并保存到文件夹中而不覆盖文件
【发布时间】:2015-12-28 07:21:51
【问题描述】:

我想从事件查看器中导出服务器日志文件并保存到一个文件夹中而不覆盖该文件。

然后我将通过使用任务计划来设置一个计划来触发这个批处理文件进行自动定期更新。 (我是wevtuitilcmd 命令的新手)

例子:

application_event1.evtx
application_event2.evtx
application_event3.evtx
application_event4.evtx

这是我的 bat 文件脚本

//我将从事件查看器中导出日志文件

wevtutil epl Application E:\a\testing_application.evtx

跟随脚本使evtx文件名增加个数

@echo off
setlocal enableextensions enabledelayedexpansion
set "source=E:\a"
if exist "%source%\*.evtx" (
set  increase=0
set  increase=!increase!+1
ren "%source%\*.evtx" "*.!increase!.evtx"
)
endlocal

结果:testing_application.0+1,testing_application.0+1.0+1

【问题讨论】:

  • 您解决了这个问题吗?如果是这样,请在帖子中告诉我们如何将其批准为答案。

标签: batch-file cmd overwrite file-rename


【解决方案1】:

set 中缺少 /a,因为它是算术运算。更多信息请输入set /?

你也错过了for-loop。更多请输入for /?

试试这样的。

@echo off
setlocal enableextensions enabledelayedexpansion
set "source=E:\a"

set increase=
for /f "delims=" %%a in ('dir /b /a-d "%source%\*.evtx"') do (
    set /a increase+=1
    echo ren "%%~a" "%%~na!increase!%%~xa"
)
endlocal

我认为你试图实现的目标似乎完全错了。

这里是另一个尝试:

@echo off

set "source=E:\a"
set "filename=testing_application"
wevtutil epl Application E:\a\testing_application.evtx

for /f "tokens=2 delims=-." %%a in (
  'dir /b "%source%\%filename%*.evtx" ^|sort /r ^|findstr /r [0-9]') do ( 
   set "num=%%a"
   goto done
)

ren "%source%\%filename%.evtx"  "%filename%-001.evtx"
exit /b 0

:done
rem :: remove zeros in front of numbers before incrementing
for /f "tokens=* delims=0" %%a in ("%num%") do set num=%%a

set /a num+=1
set incr=000%num%

ren "%source%\%filename%.evtx"  "%filename%-%incr:~-3%.evtx"

exit /b 0

前面零递增数字的解决方案是基于this

前面需要哪些零才能正确sort 文件。

【讨论】:

  • 其实wevtutil epl Application E:\a\testing_application.evtx 这个脚本也有问题。是的,它允许我从事件查看器中提取文件,但如果文件夹包含与 testing_application.evtx 相同的名称,那么脚本将无法正常工作。
  • 去掉ren命令前面的echo后,我得到的错误是“命令的语法不正确”
  • 你怎么看?这种方式行得通,或者你有另一种方式,与我的方式相比,步骤更少?
  • 好的,我试试。谢谢
  • 我完全按照脚本使用了两个文件夹调用 bar 和 foo。我在 bar 文件夹和 foo 文件夹中的 demo-000.txt 以及在 bar 文件夹中的 bat 文件。当我运行它时,它会显示 bar 文件夹的列表目录,找到的最高数字是 1 -1。 "系统找不到指定的文件"
猜你喜欢
  • 2018-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-23
  • 2017-10-25
  • 2019-10-10
  • 2013-07-24
  • 1970-01-01
相关资源
最近更新 更多