How to change date format in CSV file using windows cmd?

powershell change date format in csv
powershell export-csv date format

I have a csv file downloaded from salesforce dataloader and I want to change the date format of Last Modified date (YYYY-MM-DDT00:00:00.000Z) to (YYYY-MM-DD) using windows cmd.

I tried referring an old link (Changing datetime format in csv files). I am new to windows cmd and I didn't understand the batch script completely there.

Here is the batch script code which I am trying.

@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%x in ('TestCSV1.csv') do (
    for /f "usebackq tokens=1-3* delims=," %%i in ("%%~fx") do (
        for /f "delims=" %%a in ('powershell -command \"{YYYY-MM-DDT00:00:00.000Z}\" -f [datetime]^('%%k'^)') do >>"TestCSV2.csv" echo %%i,%%j, %%a
    )
)

And this the CSV file in which I have to make changes.

FIRSTNAME,LASTNAME,LASTMODIFIEDDATE,PHONE
Test,Last1,2019-01-21T19:07:36.000Z,9876543210
Test,Last2,2019-01-21T19:07:45.000Z,9876543211
Test,Last3,2019-01-22T05:56:20.000Z,9876543212

Need some help here.

Thanks

Given that none of the fields of the CSV file is empty, the following script would do what you want:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "_CSV=%~1"
set "_SEP=,"

set "HEAD="
for /F "usebackq tokens=1-4 delims=%_SEP% eol=%_SEP%" %%A in ("%_CSV%") do (
    if not defined HEAD (
        echo(%%A%_SEP%%%B%_SEP%%%C%_SEP%%%D
        set "HEAD=#"
    ) else (
        for /F "delims=T eol=T" %%E in ("%%C") do (
            echo(%%A%_SEP%%%B%_SEP%%%E%_SEP%%%D
        )
    )
)

endlocal
exit /B

Assuming it is called change-date.bat, run it and provide the CSV file as command line argument:

change-date.bat "TestCSV1.csv"

If you want to save the result in another file, use this:

change-date.bat "TestCSV1.csv" > "TestCSV2.csv"

How to change date format in CSV file using windows cmd?, I tried referring an old link (Changing datetime format in csv files). I am new to windows cmd and I didn't understand the batch script completely there. Here is the  Assuming it is called change-date.bat, run it and provide the CSV file as command line argument: change-date.bat "TestCSV1.csv" If you want to save the result in another file, use this: change-date.bat "TestCSV1.csv" > "TestCSV2.csv"

This is an improved version of Vlam's code:

@echo off
setlocal enabledelayedexpansion

set "file=testCSV1.csv"

for %%x in ("%file%") do (
  <"%%~fx" set /p "Header="
  (
    echo !Header!
    for /f "skip=1 usebackq tokens=1-4* delims=," %%i in ("%%~fx") do (
      REM for /f "delims=" %%a in ('powershell -command \"{0:yyyy-MM-dd}\" -f [datetime]^('%%k'^)') do echo %%i,%%j,%%a,%%l
      set "day=%%k"
      echo %%i,%%j,!day:~0,10!,%%l
    )
  )>"%%~nx_new.csv"
)

It copies the first line of the original file (the Header) to a variable for further use and the for loop processes the rest (skip=1). I replaced the (working) Powershell code (commented out) with a much faster substring processing. Also I redirect the whole output in one go, which also speeds things up a lot (noticeable with larger files).

Date format change in CSV file, Have a csv file with headers and date column having values Hoping to open the csv and change all the records in DATE column from m/d/yyyy format to mm/​dd/yyyy format. It would be helpful if someone can suggest an easier command to PS C:\windows\system32> [System.globalization.cultureinfo]::  In addition to date command, we also have an environment variable using which we can find today’s date. c:\>echo %date% Sun 05/17/2015 How to get only the date in MM/DD/YYYY format? You may want to exclude the day (like ‘Sun’ in the above example) and print only the date in MM/DD/YYYY format. The below command works for the same.

It is even easier in PowerShell.

Import-Csv -Path '.\fixd.csv' |
    ForEach-Object {$_.LASTMODIFIEDDATE=$_.LASTMODIFIEDDATE.Substring(0, 10); $_} |
    Export-Csv -Path '.\fixdnew.csv' -NoTypeInformation

If you must run from cmd.exe:

powershell -NoLogo -NoProfile -Command ^
    "Import-Csv -Path '.\fixd.csv' |" ^
    "ForEach-Object {$_.LASTMODIFIEDDATE=$_.LASTMODIFIEDDATE.Substring(0, 10); $_} |" ^
    "Export-Csv -Path '.\fixdnew.csv' -NoTypeInformation"

csv file - change inside the date format : PowerShell, I would like to find a way to change the date format inside a csv file. How that looks So far, I failed miserable with this command: Windows 10 Toast Designer. Let’s see some methods to change the created date, modified date, or last accessed date of files in this article. Change File Creation, Last Accessed or Modified Date Using PowerShell. To change the file date using PowerShell, here are the commands you need to run: Change the creation date/timestamp of a file named log1.txt:

This one works but you have to remove the header from your input file first.

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

Change date format of 2 columns in a delimited file. : PowerShell, r/PowerShell: PowerShell is a cross-platform (Windows, Linux, and macOS) automation tool Change date format of 2 columns in a delimited file. I have attempted to use Import-CSV with a -replace using the regex to match but that I went from not knowing anything in cmd prompt from knowing how to do it all in an hour. How to Change File Format Through CMD: THIS TRICK SHOWS YOU THAT HOW TO CHANGE FILE FORMAT THROUGH CMD WITHOUT USING ANY EXTERNAL SOFTWARES.YOU CAN CHANGE FILES LIKE:.txt to .mp3 , .mp3 to .doc , .apk to .mp3 and etc..THIS TRICK WOULD BE VERY USEFUL FOR THOSE USERS WHO WANT TO HIDE

Topic: Change date format in multiple CSV files, Hello, I have over 2000 CSV files with a mix of date formats. Here is some example data I am using while trying to get the core command to  The display in csv essentially depends on the default date format of your operating system. If you want to change the display only, you may want to change the date format from date-time settings. The other way is to convert the column to string format, though this will result in loss of date properties.

How can I change the timestamp on a file?, My specific situation is Windows 7. $(Get-Item ).creationtime=$(Get-Date "mm/​dd/yyyy hh:mm am/pm") $(Get-Item (This one is from the comments:) Set the last-access time for a file aaa.csv to the It's a bunch of useful tools in one small command line program. I believe it only works with US format times, though. General procedure to change file extensions has already been discussed in this site. You can also change file extensions using Command Prompt. It is much less time consuming. Steps to Change File Extensions Using Command prompt. 1. Press Windows Key + C to go to Windows Charms Bar-> Go to Apps Search-> type “cmd”. Search result will appear

Format Dates for CSV Files – Klaviyo, Likewise, when you import dates from an Excel or Google File, these systems may Press Command+1 or Control+1 (if you are using a macOS) or CTRL+1 (if you are using Windows). You can also change date formatting by location, or locale, under Language (location) in the Format Cells popup. "file.cmd" If you have that in the name spot it will save it as whatever is in the quotes. Of course, if you save a file as a bat file and it's not correct syntax then you're still not gonna get it working.

Comments
  • Thanks Vlam, but it didn't work. I have tried removing the headers as well.
  • I have tried and it works for me. What results did you get?