Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

converting Date in Bat-Script

Original Message
Name: codeman
Date: November 20, 2007 at 10:28:19 Pacific
Subject: converting Date in Bat-Script
OS: Windows 2003 Server
CPU/Ram: P4, 2GB RAM
Comment:
Hello

Firt of all, I'm not very used in programming, so sorry if I'm not as specific as I should be.
I wrote a Bat-Script on a Windows 2003 Server that reads the lines of a csv-file, puts it ordered in another csv-file and writes it all into a MySQL-Database. What i couldnt do so far was to change the Date and Time that it fits for the MySQL DATETIME-Field.

The lines in my SourceFile.csv look like this:
any,thing,13.11.2007 04:17:00,and,some,more

I take all these lines with the command:
FOR /F "usebackq tokens=1-9 skip=4 delims=," %%a in ("D:\SourceFile.csv") DO (
ECHO %%a_%%b,%%c,%%d,%%e,%%f,%%g,%%h,%%i >> D:\NewFile.csv )

what I need to fit the MySQL-Field is:
any,thing,2007-11-13 04:17:00,and,some,more

Can this be done in the same FOR-command? And if so, how? And if not, how then? I'm really stuck, tried it for hours today but with no success. If you have any idea or solutions, i'd appreciate it very much.

Thanks a lot!
Regards
Daniel


Report Offensive Message For Removal


Response Number 1
Name: klint
Date: November 20, 2007 at 11:47:02 Pacific
Subject: converting Date in Bat-Script
Reply: (edit)
It is better to do this in a more appropriate language, with good text-processing abilities, such as Awk, Sed, Python or Perl. All of these are available on the Windows platform.

If you must use cmd.exe (e.g. if you haven't got time to learn any of the above), read on.

Instead of using append-redirection (>>) inside the FOR loop, take the redirection outside (put it after the FOR loop's closing bracket. That way, you will only open, write, flush the buffers & close the file once. Instead of after every line.

In your code, you are successfully processing everything but the date. To process the date, just use an inner FOR /F loop:

FOR /F "tokens=1-3* delims=- " %%k in ("%%c") do echo %%a_%%b,%%m-%%l-%%k %%n,%%d,%%e,...

Note that %%k to %%n come from the inner loop and %%a to %%i come from the outer loop, but all are used in the same ECHO command.

Disclaimer: I haven't tested this.


Report Offensive Follow Up For Removal

Response Number 2
Name: Razor2.3
Date: November 20, 2007 at 19:44:09 Pacific
Subject: converting Date in Bat-Script
Reply: (edit)
Warning: this can break in so many ways, it just ain't funny. Like klint said, pick a better text parsing language. Even VBScript, if you must remain Windows native.

FOR /F "usebackq skip=4 tokens=1-5* delims=,. " %%a in ("D:\SourceFile.csv") DO (
ECHO %%a_%%b,%%e-%%d-%%c %%f)


Report Offensive Follow Up For Removal

Response Number 3
Name: codeman
Date: November 22, 2007 at 03:26:47 Pacific
Subject: converting Date in Bat-Script
Reply: (edit)
Thanks a lot KLINT and RAZOR.
Finally I took Razor's solution and it just works the way it should.

@klint
Thanks also für the hint to take the redirection outside the FOR loop.

@Razor
Dont know whats the meaning of your warning. What can break in here? Using another language is not an option at the moment cause I dont know any of them.


Thanks again guys, you helped me!
Greetings from Switzerland
Daniel


Report Offensive Follow Up For Removal




Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: converting Date in Bat-Script

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




CPU and Graphics Upgrade Questions

VIRUS ALERT in Taskbar, HELP!

DSHUB24 Connection Problems

need help with dsl and dial up

novel 3.12


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC