更改CSV文件中的日期时间格式 [英] Changing datetime format in csv files

查看:1700
本文介绍了更改CSV文件中的日期时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个具有以下结构的csv文件:

I've got multiple csv files with this structure:

text, text, 01/27/2001 01:00:00 PM

我需要使用此日期时间格式更新所有csv文件:

I need to update ALL csv files with this datetime format:

text, text, 27-Jan-2001 13:00:00

是否可以通过批处理文件/另一种自动方式来做到这一点?

Is there a way of doing this via Batch file/ another automated way?

谢谢

推荐答案

借助单行powershell命令,可以在批处理文件中很好地完成此操作. Powershell内置在所有较新的OS(在Win7,Win2008之后)中,也可以安装在旧OS上.

This can be done well within batch file, with the help of a single line powershell command. Powershell is inbuilt in all newer OS's (after Win7, Win2008) and can be installed on legacy OS as well.

下面是代码和示例输出-

below is the code and sample output -

@echo off
for /f "delims=" %%x in ('dir /b *.csv') do (
for /f "usebackq tokens=1-3* delims=," %%i in ("%%~fx") do (
for /f "delims=" %%a in ('powershell -command \"{0:dd-MMM-yyyy HH:mm}\" -f [datetime]^('%%k'^)') do >>"%%~nx_new.csv" echo %%i,%%j, %%a
)
)

已测试的输出-

C:\Scripts>type input1.csv
text, text, 01/27/2001 10:00:00 PM
text, text, 01/27/2001 11:00:00 AM
text, text, 01/27/2001 02:00:00 PM
text, text, 01/27/2001 12:00:00 AM
text, text, 01/27/2001 01:00:00 PM
C:\Scripts>type input2.csv
text, text, 01/27/2001 10:00:00 PM
text, text, 02/27/2002 11:00:00 AM
text, text, 03/27/2003 02:00:00 PM
text, text, 04/27/2004 12:00:00 AM
text, text, 05/27/2005 01:00:00 PM

C:\Scripts>draft.bat


C:\Scripts>type input1_new.csv
text, text, 27-Jan-2001 22:00
text, text, 27-Jan-2001 11:00
text, text, 27-Jan-2001 14:00
text, text, 27-Jan-2001 00:00
text, text, 27-Jan-2001 13:00

C:\Scripts>type input2_new.csv
text, text, 27-Jan-2001 22:00
text, text, 27-Feb-2002 11:00
text, text, 27-Mar-2003 14:00
text, text, 27-Apr-2004 00:00
text, text, 27-May-2005 13:00

干杯G

这篇关于更改CSV文件中的日期时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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