如何随时重定向输出新文件

问题描述 投票:0回答:1

我有一个问题我得到这个脚本

for /f "tokens=* " %%d in ('tracert -4 -d 8.8.8.8') do (
echo %%d
) >>test.txt

但是我想要以下条件1.当我随时运行此脚本时,请保存新文件例子

run 1 > create new file1
run 2 > create new file2
run 3 > create new file3

ever
  1. 脚本继续运行1分钟

  2. 如果我需要文件名是日期时间,反正还有

感谢回答

batch-file
1个回答
0
投票

Windows 10 64位。

如何在Windows 10 CMD和%time%中使用秒。毫秒为文件名加上时间戳。 %time%的早期版本(XP 32位?)必须以不同的方式使用。参见echo %time%

CMD:

@rem Traceroute output to text file. Timestamp filename. Windows 10 64-bit. Does not require admin privileges. 
cmd /q /e /v:off
for /f "tokens=* " %g in ('tracert -4 -d 8.8.8.8') do echo %g>>test%TIME:~10,5%.txt

脚本:

@rem Traceroute output to text file. Timestamp filename. Windows 10 64-bit. Does not require admin privileges.
@echo off 
setlocal enableextensions disabledelayedexpansion
for /f "tokens=* " %%g in ('tracert -4 -d 8.8.8.8') do echo %%g>>test%TIME:~10,5%.txt
exit /b 

结果:

screenshot of test13.77.txt

环境变量编辑:

  1. 解析字符串变量:

https://ss64.com/nt/syntax-substring.htmlhttps://ss64.com/nt/syntax-replace.htmlhttps://ss64.com/nt/syntax-args.html

%name:~n% Skips the first n letters and returns the rest
%name:~n,m% Skips n letters and returns the next m
%name:~,m% First (leftmost) m letters
%name:~,-m% Last (rightmost) m letters
Using the environment variable var=ABCDEFG, here are some examples:
Command Prints
ECHO %var% ABCDEFG
ECHO %var:~2% CDEFG
ECHO %var:~2,3% CDE
ECHO %var:~,3% ABC
ECHO %var:~,-3% ABCD
© www.soinside.com 2019 - 2024. All rights reserved.