Changing datetime format in csv files

久未见 提交于 2019-12-24 08:35:31

问题


I've got multiple csv files with this structure:

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

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?

Thanks,


回答1:


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
)
)

Tested output -

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

Cheers, G




回答2:


The Batch file below should run faster because it does not use any external file (like the 470KB size powershell.exe file).

@echo off
setlocal EnableDelayedExpansion

set i=100
for %%a in (Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec) do (
   set /A i+=1
   set "month[!i:~1!]=%%a"
)

for /F "delims=" %%n in ('dir /A-D /B *.csv') do (
   (for /F "usebackq tokens=1-3 delims=," %%x in ("%%n") do (
      for /F "tokens=1-7 delims=/: " %%a in ("%%z") do (
         set hour=%%d
         if "%%g" equ "PM" (
            set /A hour=1!hour!+12-100
            if !hour! equ 24 set "hour=0"
            if !hour! lss 10 set "hour=0!hour!"
         )
         echo %%x,%%y, %%b-!month[%%a]!-%%c !hour!:%%e:%%f
      )
   )) > "%%~Nn_new.csv"
)



回答3:


This can be done with 24 Perl regular expression replaces in files with text editor UltraEdit put into an UltraEdit macro.

InsertMode
ColumnModeOff
HexOff
PerlReOn
ReplInFiles MatchCase RegExp Log "C:\Temp\" "*.csv" "01/([0-3]\d)/([12][09]\d\d)" "\1-Jan-\2"
ReplInFiles MatchCase RegExp Log "C:\Temp\" "*.csv" "02/([0-3]\d)/([12][09]\d\d)" "\1-Feb-\2"
ReplInFiles MatchCase RegExp Log "C:\Temp\" "*.csv" "03/([0-3]\d)/([12][09]\d\d)" "\1-Mar-\2"
ReplInFiles MatchCase RegExp Log "C:\Temp\" "*.csv" "04/([0-3]\d)/([12][09]\d\d)" "\1-Apr-\2"
ReplInFiles MatchCase RegExp Log "C:\Temp\" "*.csv" "05/([0-3]\d)/([12][09]\d\d)" "\1-May-\2"
ReplInFiles MatchCase RegExp Log "C:\Temp\" "*.csv" "06/([0-3]\d)/([12][09]\d\d)" "\1-Jun-\2"
ReplInFiles MatchCase RegExp Log "C:\Temp\" "*.csv" "07/([0-3]\d)/([12][09]\d\d)" "\1-Jul-\2"
ReplInFiles MatchCase RegExp Log "C:\Temp\" "*.csv" "08/([0-3]\d)/([12][09]\d\d)" "\1-Aug-\2"
ReplInFiles MatchCase RegExp Log "C:\Temp\" "*.csv" "09/([0-3]\d)/([12][09]\d\d)" "\1-Sep-\2"
ReplInFiles MatchCase RegExp Log "C:\Temp\" "*.csv" "10/([0-3]\d)/([12][09]\d\d)" "\1-Oct-\2"
ReplInFiles MatchCase RegExp Log "C:\Temp\" "*.csv" "11/([0-3]\d)/([12][09]\d\d)" "\1-Nov-\2"
ReplInFiles MatchCase RegExp Log "C:\Temp\" "*.csv" "12/([0-3]\d)/([12][09]\d\d)" "\1-Dec-\2"
ReplInFiles MatchCase RegExp Log "C:\Temp\" "*.csv" "01(:[0-5]\d:[0-5]\d) PM" "13\1"
ReplInFiles MatchCase RegExp Log "C:\Temp\" "*.csv" "02(:[0-5]\d:[0-5]\d) PM" "14\1"
ReplInFiles MatchCase RegExp Log "C:\Temp\" "*.csv" "03(:[0-5]\d:[0-5]\d) PM" "15\1"
ReplInFiles MatchCase RegExp Log "C:\Temp\" "*.csv" "04(:[0-5]\d:[0-5]\d) PM" "16\1"
ReplInFiles MatchCase RegExp Log "C:\Temp\" "*.csv" "05(:[0-5]\d:[0-5]\d) PM" "17\1"
ReplInFiles MatchCase RegExp Log "C:\Temp\" "*.csv" "06(:[0-5]\d:[0-5]\d) PM" "18\1"
ReplInFiles MatchCase RegExp Log "C:\Temp\" "*.csv" "07(:[0-5]\d:[0-5]\d) PM" "18\1"
ReplInFiles MatchCase RegExp Log "C:\Temp\" "*.csv" "08(:[0-5]\d:[0-5]\d) PM" "20\1"
ReplInFiles MatchCase RegExp Log "C:\Temp\" "*.csv" "09(:[0-5]\d:[0-5]\d) PM" "21\1"
ReplInFiles MatchCase RegExp Log "C:\Temp\" "*.csv" "10(:[0-5]\d:[0-5]\d) PM" "22\1"
ReplInFiles MatchCase RegExp Log "C:\Temp\" "*.csv" "11(:[0-5]\d:[0-5]\d) PM" "23\1"
ReplInFiles MatchCase RegExp Log "C:\Temp\" "*.csv" "([01]\d:[0-5]\d:[0-5]\d) [AP]M" "\1"

This UltraEdit macro modifies the date and time strings in all CSV files in directory C:\Temp.

I do not use Notepad++. But I nevertheless think that those 24 regular expression replaces can be executed also with Notepad++ on all CSV files of a directory.



来源:https://stackoverflow.com/questions/25595779/changing-datetime-format-in-csv-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!