使用BAT脚本在文件名中插入日期/时间戳 [英] Inserting date/time stamp in file name using BAT script

查看:1230
本文介绍了使用BAT脚本在文件名中插入日期/时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个BAT脚本,该脚本使用SQL存储过程中的数据创建文件.我想做的是用日期/时间戳重命名该文件,但尚未在Google搜索中找到可行的解决方案.

I have a BAT script which creates a file with data from a SQL stored procedure. What i would like to do is rename that file with a date/Time stamp but have not found a workable solution yet in google search.

示例.当前文件名Pcnt.txt将其更改为PcntYYYYMMDDHHMM.csv,我能够将格式更改为csv,但文件只是重命名为Pcnt.csv,并且需要添加一些代码来插入日期/时间戳.

example. current file name Pcnt.txt change it to PcntYYYYMMDDHHMM.csv, I am able to get the format to change to csv but the file is just renamed to Pcnt.csv and would need to add some code which will insert the date/time stamp.

请帮助

谢谢

推荐答案

此代码的前四行将为您提供XP Pro及更高版本中可靠的YY DD MM YYYY HH Min Sec变量.

The first four lines of this code will give you reliable YY DD MM YYYY HH Min Sec variables in XP Pro and higher.

将ren命令与变量一起使用-请参见 REN/?

Use the ren command with the variables - see the help from REN /?

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%"
set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
echo fullstamp: "%fullstamp%"
pause

这篇关于使用BAT脚本在文件名中插入日期/时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆