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
cindylarameeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Maen Abu-TabanjehNetwork Administrator, Network ConsultantCommented:
0
Bill PrewIT / Software Engineering ConsultantCommented:
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
0
Bill PrewIT / Software Engineering ConsultantCommented:
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
0
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
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
;-).
0
Bill PrewIT / Software Engineering ConsultantCommented:
Thanks.

~bp
0
Bill PrewIT / Software Engineering ConsultantCommented:
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
0
Steve KnightIT ConsultancyCommented:
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
0
Bill PrewIT / Software Engineering ConsultantCommented:
In this case I think leading zeros aren't desired Steve.

~bp
0
Steve KnightIT ConsultancyCommented:
Probably true!
0
cindylarameeAuthor Commented:
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
0
Bill PrewIT / Software Engineering ConsultantCommented:
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
0
cindylarameeAuthor Commented:
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
0
Bill PrewIT / Software Engineering ConsultantCommented:
Okay, give this a try.

@echo off
setlocal enabledelayedexpansion

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

Set ProcessList=100 102 103 104 105 107 108 109 110 111 112 113 115 117 118 120 121 123 124 125 126 127 128 129 130 131 132 134 135 136 138 139 140 141 144 145 147 148 149 151 152 153 154 155 156 157 159 160 162 164 166 167 168 170 171 173 200 201 201 202 203 204 205 206 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 228 229 230 231 235 240

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=," %%a in (%input_file%) do (
    for /f "usebackq tokens=1-10 delims=," %%Z in ('cscript //nologo _t.vbs %%c') do (set NewDate=%%Z)

    set Process=0
    for %%p in (%ProcessList%) do (if "%%~a"=="%%~p" set Process=1)

    if "!Process!"=="1" (
        echo X%%a,%%b,!NewDate!,"23:30:00",%%e,%%h >>%output_file%
    ) else (
        if "%%~a-%%~b"=="101-1" echo X%%a,2,!NewDate!,"23:30:00",%%e,%%h >>%output_file%
        if "%%~a-%%~b"=="101-2" echo X%%a,1,!NewDate!,"23:30:00",%%e,%%h >>%output_file%
                                                                                        
        if "%%~a-%%~b"=="116-1" echo X%%a,3,!NewDate!,"23:30:00",%%e,%%h >>%output_file%
        if "%%~a-%%~b"=="116-2" echo X%%a,2,!NewDate!,"23:30:00",%%e,%%h >>%output_file%
        if "%%~a-%%~b"=="116-3" echo X%%a,4,!NewDate!,"23:30:00",%%e,%%h >>%output_file%
        if "%%~a-%%~b"=="116-4" echo X%%a,5,!NewDate!,"23:30:00",%%e,%%h >>%output_file%
        if "%%~a-%%~b"=="116-5" echo X%%a,1,!NewDate!,"23:30:00",%%e,%%h >>%output_file%
                                                                                        
        if "%%~a-%%~b"=="119-1" echo X%%a,2,!NewDate!,"23:30:00",%%e,%%h >>%output_file%
        if "%%~a-%%~b"=="119-2" echo X%%a,1,!NewDate!,"23:30:00",%%e,%%h >>%output_file%
                                                                                        
        if "%%~a-%%~b"=="122-1" echo X%%a,3,!NewDate!,"23:30:00",%%e,%%h >>%output_file%
        if "%%~a-%%~b"=="122-2" echo X%%a,1,!NewDate!,"23:30:00",%%e,%%h >>%output_file%
        if "%%~a-%%~b"=="122-3" echo X%%a,4,!NewDate!,"23:30:00",%%e,%%h >>%output_file%
        if "%%~a-%%~b"=="122-4" echo X%%a,5,!NewDate!,"23:30:00",%%e,%%h >>%output_file%
        if "%%~a-%%~b"=="122-5" echo X%%a,2,!NewDate!,"23:30:00",%%e,%%h >>%output_file%
                                                                                        
        if "%%~a-%%~b"=="123-1" echo X%%a,3,!NewDate!,"23:30:00",%%e,%%h >>%output_file%
        if "%%~a-%%~b"=="123-2" echo X%%a,1,!NewDate!,"23:30:00",%%e,%%h >>%output_file%
        if "%%~a-%%~b"=="123-3" echo X%%a,4,!NewDate!,"23:30:00",%%e,%%h >>%output_file%
        if "%%~a-%%~b"=="123-4" echo X%%a,5,!NewDate!,"23:30:00",%%e,%%h >>%output_file%
        if "%%~a-%%~b"=="123-5" echo X%%a,2,!NewDate!,"23:30:00",%%e,%%h >>%output_file%
                                                                                        
        if "%%~a-%%~b"=="158-1" echo X%%a,2,!NewDate!,"23:30:00",%%e,%%h >>%output_file%
        if "%%~a-%%~b"=="158-2" echo X%%a,1,!NewDate!,"23:30:00",%%e,%%h >>%output_file%
                                                                                        
        if "%%~a-%%~b"=="163-1" echo X%%a,3,!NewDate!,"23:30:00",%%e,%%h >>%output_file%
        if "%%~a-%%~b"=="163-2" echo X%%a,1,!NewDate!,"23:30:00",%%e,%%h >>%output_file%
        if "%%~a-%%~b"=="163-3" echo X%%a,2,!NewDate!,"23:30:00",%%e,%%h >>%output_file%
        if "%%~a-%%~b"=="163-4" echo X%%a,4,!NewDate!,"23:30:00",%%e,%%h >>%output_file%
                                                                                        
        if "%%~a-%%~b"=="165-1" echo X%%a,3,!NewDate!,"23:30:00",%%e,%%h >>%output_file%
        if "%%~a-%%~b"=="165-2" echo X%%a,1,!NewDate!,"23:30:00",%%e,%%h >>%output_file%
        if "%%~a-%%~b"=="165-3" echo X%%a,2,!NewDate!,"23:30:00",%%e,%%h >>%output_file%
        if "%%~a-%%~b"=="165-4" echo X%%a,4,!NewDate!,"23:30:00",%%e,%%h >>%output_file%
        if "%%~a-%%~b"=="165-5" echo X%%a,5,!NewDate!,"23:30:00",%%e,%%h >>%output_file%
                                                                                        
        if "%%~a-%%~b"=="227-1" echo X%%a,1,!NewDate!,"23:30:00",%%e,%%h >>%output_file%
        if "%%~a-%%~b"=="227-2" echo X%%a,3,!NewDate!,"23:30:00",%%e,%%h >>%output_file%
        if "%%~a-%%~b"=="227-3" echo X%%a,2,!NewDate!,"23:30:00",%%e,%%h >>%output_file%
        if "%%~a-%%~b"=="227-4" echo X%%a,4,!NewDate!,"23:30:00",%%e,%%h >>%output_file%
    )
)

ECHO ==DONE!==
del _t.vbs
GOTO:EOF

Open in new window

~bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
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

0
Bill PrewIT / Software Engineering ConsultantCommented:
@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.

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_27493693.html?#a37288974

~bp
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Gosh. No comment in regard of the previously asked Q. I think this one is different, as it does not use "complicated" techniques.
0
Steve KnightIT ConsultancyCommented:
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
0
Steve KnightIT ConsultancyCommented:
Oops... Qlemo... sorry!
0
cindylarameeAuthor Commented:
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
0
cindylarameeAuthor Commented:
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
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Remove the usebackq token from line 15 of http:#a37301125, it does not belong there.

BTW, the meaning of my recommendation was not to (only) to optimize, but to simplify your script, so changes are more easy to apply, and debugging is also easier. I intentionally did only very simple replacements, to not over-complicate the script.
0
Bill PrewIT / Software Engineering ConsultantCommented:
Glad that helped, thanks.

~bp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.