[英]Simple batch file not executing?
I'm trying to merge txt files in a dir using the following 我正在尝试使用以下命令将txt文件合并到目录中
@echo off
cd /d C:\textfiles\
for %f in (*.txt) do type "%f" >> C:\Users\Desktop\output.txt
But I am not getting any output (because last line I'm pretty sure) , can anyone help me out here? 但是我没有得到任何输出(因为我很确定,因为最后一行),有人可以帮我吗?
Also how can I make the output filename automatically append text or date for the date eg output_random.txt or output_19.06.14.txt ? 另外,如何使输出文件名自动为日期添加文本或日期,例如output_random.txt或output_19.06.14.txt?
Edited code (thanks to Stephan) but not appending date: 编辑代码(感谢斯蒂芬),但不附加日期:
@echo off
cd /d "C:\TextFiles"
for /f %%i in ('wmic os get localdatetime ^|find "20"') do set dt=%%i
set dt=%dt:~0,8%
for %%f in (*.txt) do type "%%f" >> C:\TextFiles\output-%dt%.txt
Inside batchfiles, you have to use %%f
instead of %f
, which works on commandline only: 在批处理文件内部,您必须使用
%%f
而不是%f
,后者只能在命令行上使用:
for %%f in (*.txt) do type "%%f" >> C:\Users\Desktop\output.txt
for the "Random or Date" part: 对于“随机或日期”部分:
As you say "random or date", I assume, the date don't have to have a specific format. 正如您所说的“随机或日期”一样,我认为日期不必具有特定的格式。 Best way (because independent of locale settings) would be:
最好的方法(因为与语言环境设置无关)是:
for /f %%i in ('wmic os get localdatetime ^|find "20"') do set dt=%%i
set dt=%dt:~0,8%
%dt%
will be in the format YYYYMMDD. %dt%
的格式为YYYYMMDD。 You can easily add the time with set dt=%dt:~0,14%
(YYYYMMDDhhmmss) and output to ...output-%dt%.txt
您可以通过
set dt=%dt:~0,14%
(YYYYMMDDhhmmss)轻松添加时间并将其输出到...output-%dt%.txt
for formatting %dt%
to DD-MM-YYYY: 用于将
%dt%
格式化为DD-MM-YYYY:
set dt=%dt:~6,2%-%dt:~4,2%-%dt:~0,4%
Extended Reading - How does the Windows Command Interpreter (CMD.EXE) parse scripts? 扩展阅读-Windows命令解释器(CMD.EXE)如何解析脚本?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.