在MS DOS 6.22中将日期附加到文本文件

时间:2014-09-23 01:38:24

标签: windows batch-file

我一直在环顾四周,似乎无法找到解决方案。我总是使用Windows命令行和脚本编写新手,所以我觉得这是一个提问我问题的好地方。

我基本上试图在我的脚本作业中添加时间戳。我们将批处理脚本作为文本文件提交,我想在文本文档中添加一个时间戳。我问我的教授,他说这是可能的,但他不确定该怎么做。我似乎无法找到我在网上寻找的解决方案。我知道如何在Windows命令行中执行此操作,但不在命令行上执行此操作。任何帮助将不胜感激!

rem此批处理文件将所有工作复制到批处理文件,然后复制到com1:

@echo off
cls
cd \
cd menu
copy *.bat mywork.bat
echo This file was created on (this is where I run in to trouble)
type mywork.bat > com1:
cd \
cls
All Files have been copied. 

3 个答案:

答案 0 :(得分:3)

这些都是MSDOS方法,并且在默认Windows中几乎不起作用。

在批处理脚本中使用Qbasic:

@echo off
echo open "!_~_!.bat" for output as #1: a$=DATE$>!_~_!.bas
echo ? #1, "SET DAY="+RIGHT$(a$,2)+LEFT$(a$,2)+MID$(a$,4,2)>>!_~_!.bas
echo SYSTEM>>!_~_!.bas
qbasic /run !_~_!.bas
call !_~_!.bat
del !_~_!.bat
del !_~_!.bas
echo %day%
ren filename.xxx %day%.xxx

获取日期的本地方式

@echo off
echo exit|"%comspec%" /k prompt set d=$D$_|find /v "exit">"%temp%.\datetmp.bat"
for %%a in (call del) do %%a "%temp%.\datetmp.bat"
echo the date is %d%

使用ASCII二进制文件:

@echo off
:: ascii assembler line processing utility by Herbert Kleebauer
:: for the source code and usage information, use this URL
:: http://groups.google.com.au/groups?q=Usage:+edl
::
echo Bj@jzh`0X-`/PPPPPPa(DE(DM(DO(Dh(Ls(Lu(LX(LeZRR]EEEUYRX2Dx=>edl.com
echo 0DxFP,0Xx.t0P,=XtGsB4o@$?PIyU WwX0GwUY Wv;ovBX2Gv0ExGIuht6>>edl.com
echo ?@}I{uNWEF~NPCkaEFAKLCmaIj@KguHaEFCKYCmavh@{HM?cCiuGGwHmYz>>edl.com
echo CgisCGH`LbuuGNO@hRgco{W?dOGg@N?]gBgoG}G?X_SgONks?GN`LBgDu}>>edl.com
echo G?I_DgGNoG?w@jgLiuuroD@?FHoGpBBDcB?1?pIoCRaICSbICn}ExvHmE?>>edl.com
echo coF?DO~yanxCqap?@?lpZrH~sa`LyNHKqDGwQVTNG`CiECICtdL{D?{esL>>edl.com
echo ysICu_{OuD@sCREGHt~F@lgNHYq`EE{S~{Hq_gC{Lr@CE{HQ}@ExuCNQmB>>edl.com
echo BwjFCs?osqs?}n`LKLj?o{}HwJvClpCSEGt~~1}HGGHCSaCU}GiuJaxLCS>>edl.com
echo c}BWuNC_FE{sCkEGFAPqCmEGNAcQNJwLECuQsa{Oe~CK~CkqCmeGmEFbCN>>edl.com
echo C?kEFbBaCGH1jnjBrz?JAcqo~O~?lJgvxs~CspajF{oFEBHijnjBrz?JAc>>edl.com
echo vx~O~?QJLqos~CspFjN{xFEByijnj@ComJcIpCSAijZNUmJaujC{U]JaJB>>edl.com
echo CcClmCJ\jbCS]GFrj~CkEGjBSookVBA_@NJBHmClnEj1JYjxCoIBrh{BFC>>edl.com
echo HtdCWECaBsCC@ZgB@WgB}fj~BsMV@NgB~chvsb{Os{AR{msDUsycsk{SK{>>edl.com
echo VQ{ZsH\sQdsq{Sj{cAICNWl{~B1CNW_K~BxVkSfCA?Cb@N}W@{=sIfjBH}>>edl.com
echo G}N}NK}NNguM@[umCqBJqD@mzDCCClmCJFuhClmC{@jJSN?`CWEG{Cs@Pt>>edl.com
echo cc?AyAFZp{CkEGjBEpEFDNCCkq=jBktx{S[zDgsjCKtl{S]zDgjjCKtc{S>>edl.com
echo _zDgajCKtG{SazDgXjCKtL{SczDgOjCKtR{SezDgFjCKtX{SYzMgFICG?K>>edl.com
echo gF@FIE?EgF}ZhziEuRN~CK}~DqgLoqo?t_ogIKEh?{JU=fCguGiuz_FrCC>>edl.com
echo sCyOjEEsjwr~EvPK~CSqCt~FS}Ha}HCGxCUqERNG]CRQa_BfsCoaoy?h@x>>edl.com
echo CGJH?w``LRaDBBobc?q?a_q?C_0x>>edl.com

:: creates a variable in YYYYMMDD format

echo.!|edl "" "set date=$tY$ty$tm$td">temp.bat
call temp.bat
del temp.bat
echo %date%
del edl.com

:: $tY          : year   - leading  2 digits        (20 for 2003)
:: $ty          : year   - trailing 2 digits        (13 for 2013)
:: $tm          : month  - 2 digits
:: $td          : day    - 2 digits
:: $tH          : hour   - 2 digits in military time (23:00 is 11pm)
:: $tM          : minute - 2 digits
:: $tS          : second - 2 digits

另一种提供各种日期和时间信息的MSDOS方法:

:: D8TIME.BAT by Larry Nelson from BATPOWER Fidonet Echo
:: Modified 17/07/1995 
 @echo off
::      goto %1
:: Setup Magic.bat with %temp% in it withour CR/LF

       echo %temp%>%temp%b4.bat
       for %%x in (rcx 3 w q) do echo %%x>>%temp%b4.scr
       ::              ^ (2h=2 chars, 3h=3 chars, 4h=4 chars, etc.)
       debug %temp%b4.bat<%temp%b4.scr>nul

 :d8ti
      echo set d8ti=%%3 %%4 >%temp%!.bat
         echo y|copy %temp%b4.bat %temp%magic.bat>nul
         dir %temp%!.bat |find "!" >>%temp%magic.bat
         call %temp%magic.bat
::      goto L8r
 :d8
      echo set d8=%%3 >%temp%!.bat
         echo y|copy %temp%b4.bat %temp%magic.bat>nul
         dir %temp%!.bat |find "!" >>%temp%magic.bat
         call %temp%magic.bat
::      goto L8r
 :time
      echo set time=%%4 >%temp%!.bat
         echo y|copy %temp%b4.bat %temp%magic.bat>nul
         dir %temp%!.bat |find "!" >>%temp%magic.bat
         call %temp%magic.bat
::      goto L8r
 :day
         echo y|copy %temp%b4.bat %temp%magic.bat>nul
         ver |date |find/i "current" >>%temp%magic.bat
         echo set day=%%3 >%temp%current.bat
         call %temp%magic.bat
::     goto L8r
 :L8r
for %%q in (magic current ! b4) do if exist %temp%%%q.bat del %temp%%%q.bat
if exist %temp%b4.scr del %temp%b4.scr
set 
pause
 :: D8TIME.bat
 :: Usage = d8time d8 (date) time (time) d8ti (date/time) day (day of week).
 :: Datetime pulls system date and/or time, or day of the
 :: week and puts it into an envar with current date/time,
 :: date, or time. Find.exe must be in path. A sample usage
 :: of Datetime is Bootlog.bat below. With { call bootlog }
 :: in your Autoexec.bat a file named Bootlog.dat will
 :: receive a record of every time your computer is booted up.
      ::
      ::BOOTLOG.BAT
      :: 
      ::         @echo off
      ::         cls
      ::               call d8time d8ti
      ::            echo %d8ti% >>bootlog.dat
      ::               set d8ti=
      ::         :L8r

答案 1 :(得分:0)

您可以使用%DATE%和/或%TIME%变量:

> echo %DATE% %TIME%
2014-09-23 13:43:03.02

或者,您可以使用DATE和/或TIME 命令

> date /t
2014-09-23
> time /t
13:56

/t开关表示“不提示设置日期/时间,只输出当前值”。您可以将命令的输出传递给如下文件:

date /t >> mywork.bat

>>表示将输出附加到文件的末尾。

答案 2 :(得分:0)

@echo off
setlocal
set "script=%~f0"
set "script=%script:\=\\%"
:: Define simple macros to support JavaScript within batch
set "beginJS=mshta "javascript:close(new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write("
set "endJS=));""
set "JS=(new ActiveXObject('Scripting.FileSystemObject').GetFile('%script%')).DateCreated"

for /f %%N in (
  '%beginJS% %JS% %endJS%'
) do set date_created=%%N

rem checks if time stamp function is created
call :getTimeStamp >nul 2>&1 || (
    echo(>>"%~f0"
    echo :getTimeStamp >>"%~f0"
    echo echo created on %date_created% >>"%~f0"
    echo goto :eof  >>"%~f0"
)
endlocal

::::::::your code goes here


cls
cd \
cd menu
copy *.bat mywork.bat
::this line is changed
call :getTimeStamp
type mywork.bat > com1:
cd \
cls
All Files have been copied. 

::::::::end of your code

goto :eof

修改

这是关于真正的dos.Just看到了评论。

这在DOS上完全不起作用 - 没有电话:lablel,没有MSHTA,没有FOR / F ......

尽管我不愿意删除我的答案 - 获取文件的创建日期并不是那么容易,并附加一个新的子程序(只有在没有的情况下)打印日期。至少是一个好的运动。