Link to home
Start Free TrialLog in
Avatar of cindylaramee
cindylaramee

asked on

DOS batch file date issue

OK, I need more help.

I am creating a batch file that takes an input file, changes a few values, and echos the rest of the values to an output file.

One of the input file's columns is a date.  Instead of echoing the date to the output file, as is. I need to advance it by 1 day and then put it in a specific format in the output file.

Example:
input: 12/15/2011
output: 12/16/11

Code to modify: (%%c is the date field)  Output file is a .csv file.
      if "%%~a-%%~b"=="101-1" (
        >>%output_file% echo X%%a,2,%%c,"23:30:00",%%e,%%h)

Thanks,
Cindy
Avatar of Maen Abu-Tabanjeh
Maen Abu-Tabanjeh
Flag of Jordan image

Avatar of Bill Prew
Bill Prew

Does this solution need to be done in pure BAT code, or would using a small freeware utility that can do this be acceptable?

~bp
Okay, I think this is likely a relatively easy approach, given the problem you are trying to solve.  You didn't specify which variable had the date in it, so I assumed %%c for this example.  Let me know if this doesn't make sense.

@echo off

REM Add the following at the beginning of your BAT script
echo.d=cdate(wscript.arguments.item(0))+1>_t.vbs
echo.wscript.echo month(d)&"/"&day(d)&"/"&right(year(d),2)>>_t.vbs


REM Adds one to the %%c value and zero pads the result into MM/DD/YY format
if "%%~a-%%~b"=="101-1" (
    for /F "delims=* tokens=1" %%Z in ('cscript //nologo _t.vbs %%c') do (
        >>%output_file% echo X%%a,2,%%Z,"23:30:00",%%e,%%h
    )
)


REM Add the following at the end of your script
del _t.vbs

Open in new window

~bp
Line 5 above should look like this:
echo.wscript.echo month(d)^&"/"^&day(d)^&"/"^&right(year(d),2)>>_t.vbs

Open in new window

And I would not use echo. syntax, as that is superfluous here, and only confusing, if you look at
   echo.wscript.echo
versus
   echo wscript.echo
;-).
Thanks.

~bp
So it now looks like:

@echo off

REM Add the following at the beginning of your BAT script
echo d=cdate(wscript.arguments.item(0))+1>_t.vbs
echo wscript.echo month(d)^&"/"^&day(d)^&"/"^&right(year(d),2)>>_t.vbs


REM Adds one to the %%c value and zero pads the result into MM/DD/YY format
if "%%~a-%%~b"=="101-1" (
    for /F "delims=* tokens=1" %%Z in ('cscript //nologo _t.vbs %%c') do (
        >>%output_file% echo X%%a,2,%%Z,"23:30:00",%%e,%%h
    )
)


REM Add the following at the end of your script
del _t.vbs

Open in new window

~bp
Hmm, did basically the same myself when the alert came out but got called away before I submitted.  Only difference I did was to use

right(100+month(d),2)^&"/"^&right(100+day(d),2)^&"/"^&right(year(d),2)

to get the leading zeroes.

Steve
In this case I think leading zeros aren't desired Steve.

~bp
Probably true!
Avatar of cindylaramee

ASKER

OK, tried to mix your stuff with my exiting stuff and it didn't work so well.
I didn't really understand the echo code you wrote and is cscript suppose to be my input file?

Here is my code:

@echo off

set input_file="C:\Program Files (x86)\TMW Systems, Inc\TMWSuite\Inventory Service Module\ImporterConsole\Bat files for AutoSend Importer\sstexp2.csv"
set output_file="C:\Program Files (x86)\TMW Systems, Inc\TMWSuite\Inventory Service Module\ImporterConsole\Bat files for AutoSend Importer\sstexp3.csv"

if exist %output_file% del %output_file%

echo d=cdate(wscript.arguments.item(0))+1>>_t.vbs
echo wscript.echo month(d)^&"/"^&day(d)^&"/"^&right(year(d),2)>>_t.vbs

for /f "usebackq tokens=1-10 delims=," %%Z in ('cscript //nologo _t.vbs %%c') do (
    >>%output_file% echo %%a,%%b,%%Z,%%d,%%e,%%h
)

ECHO ==DONE!==
GOTO:EOF

del _t.vbs
In the above code you removed your loop over the input file that sets %%c, that's a problem.  If you want to post the full script you wanted this included into it might be easier for me to add in the new stuff for the date logic.

~bp
I created 2 bat files.  All the stuff I have been working on I put in 1 bat and then am doing just the date conversion in this bat file.

Here are all the files that I am working on.  I am sure I could have done things cleaner, but I like small chunks at a time.
Cindy
autosend.txt
ftp-autosend.txt
AutoSend-file-translator.bat.txt
change-date.bat.txt
sstexp.csv
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Bill,

to be honest, I don't like that repetition of almost the same echo code line for each IF. I would prefer just setting a var, and echoing it later:
...
    ) else (
        if "%%~a-%%~b"=="101-1" set cat=2
        if "%%~a-%%~b"=="101-2" set cat=1
...
        if "%%~a-%%~b"=="227-4" set cat=4

        echo X%%a,!cat!,!NewDate!,"23:30:00",%%e,%%h >>%output_file%
)
...

Open in new window

@Qlemo,

I totally agree Qlemo, but honestly, I came up with the related solution at the link below, and though it was a really clean, efficient and maintainable approach.  But in the end it wasn't accepted so I moved on and have decided here not to over "optimize" since the effort was not desired last time.

https://www.experts-exchange.com/questions/27493693/Variable-equal-to-multiple-values.html?&anchorAnswerId=37288974#a37288974

~bp
Gosh. No comment in regard of the previously asked Q. I think this one is different, as it does not use "complicated" techniques.
Agreed with both of you :-)  Unless other fields need changing then I would say Wlemo idea is nice half way house between "perfect" techie solution Bill offered before, and less chances of typos!

Steve
Oops... Qlemo... sorry!
Bill,
Last few days have been crazy, so I will try your  37301125 solution tomorrow.

Sorry to all of you, but I am a project manager not a developer and this is the first code I have done since college...so I am trying to keep the code simple and understandable for me.  I will need to modify this file as things in our business change and I will need to 'figure' all the code out again each time (although when I am finished I will add REMs).

So, I appreicate that you are all trying to find the 'most' efficient code, but since this file is realitively short (500 lines) and it runs once a day, I am not that concerned about effieciency as much as understandabilty.

I really do appreciate all your help with my file,
Cindy
Bill,
I substituted yoour code exactly and got my date column looking like this:
X100      1      cscript //nologo _t.vbs "12/19/11"      3:00:00      UNLEADED      6755
X100      2      cscript //nologo _t.vbs "12/19/11"      3:00:00      PREMIUM      4624
X101      2      cscript //nologo _t.vbs "12/19/11"      3:00:00      UNLEADED      9078
X101      1      cscript //nologo _t.vbs "12/19/11"      3:00:00      PREMIUM      4445
X102      1      cscript //nologo _t.vbs "12/19/11"      3:00:00      UNLEADED      5633
X102      2      cscript //nologo _t.vbs "12/19/11"      3:00:00      UNLEADED      6668
X102      3      cscript //nologo _t.vbs "12/19/11"      3:00:00      SUPER      2981
X102      4      cscript //nologo _t.vbs "12/19/11"      3:00:00      DIESEL      3006
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Glad that helped, thanks.

~bp