• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

Removing Spaces in a Batch File

Previously I had asked a question on how I can replace the Execution date with a date entered by the user. Basically it is to write the Execution Date & Execution Month into the text file called batch_bo_report_param.txt.  The text file looks like this:

BOUSER=bouser
BOPASS=bouser
Execution Date (YYYY-MM-DD) : =2005-03-28
Execution Month (YYYY-MM) : =2005-03
Batch Bo Report Path: =C:\MDB_BATCH_BO_REPORTS\Report\
Log Batch Bo Report Path: =C:\MDB_BATCH_BO_REPORTS\Log\
Batch Result Bo Report Path: =C:\MDB_BATCH_BO_REPORTS\Result_Report\
Logo Path Name : =C:\MDB_BATCH_BO_REPORTS\Param\KLLogo.bmp


I was given the code by obda:

@echo off
setlocal
set InputFile=c:\batch_bo_report_param.txt
set OutputFile=c:\batch_bo_report_param-new.txt
set NewExecutionDate=2005-05-10
if exist "%OutputFile%" del "%OutputFile%"
for /f "tokens=1,2*" %%a in ('type "%InputFile%"') do (
 if /i "%%a %%b"=="Execution Date" (
   >>"%OutputFile%" echo Execution Date ^(YYYY-MM-DD^) : =%NewExecutionDate%
 ) else (
   >>"%OutputFile%" echo %%a %%b %%c
 )
)

Now the code works fine. It actualy creates a new batch_bo_report_param file and replaces the Execution Date. This batch_bo_report_param.txt file is actually used to pass certain data to some other file. Anyway, I normally run another batch report (CTPD950.bat) which in turn gets its Execution date from the batch_bo_report_param.txt file.

The problem is, the CTPD950.bat file will not run when this new param file is created. So I tested it out by cutting the exact lines (posted right above) and pasted it in the new batch_bo_report_param.txt file and the report CTPD950 ran. The four lines that I cut and pasted from the old batch_bo_report_param.txt file to the new one are:

BOUSER=bouser
BOPASS=bouser
Execution Date (YYYY-MM-DD) : =2005-03-28
Execution Month (YYYY-MM) : =2005-03


Is there any possible way of doing like a vbCrLf for a DOS batch file? I suspect this may be the cause, but I am uncertain. There is something about these four line that is causing the CTPD950.bat report not to run when it reads the newly created batch_bo_report_param.txt

I really need help on this as I am totally out of ideas.

Thank You
0
ben1211
Asked:
ben1211
  • 3
1 Solution
 
oBdACommented:
The script in its current version will only replace the "Execution Date" line; might it be necessary to change the "Execution Month" line as well?
Then can you post the lines that are *not* working (if necessary, replace spaces with underscores or tildes or whatever, so that they don't get lost)?
And another question: is especially the data in bopass indeed "bopass", or does this contain any special characters like <>|^%?
0
 
ben1211Author Commented:
obda,

Yes, it is necessary to change the Execution Month as well. So, it is the Execution Date & Execution Month that needs to be changed.

I believe the lines that are causing the problem are:

BOUSER=bouser
BOPASS=bouser
Execution Date (YYYY-MM-DD) : =2005-03-28
Execution Month (YYYY-MM) : =2005-03

bopass does not contain any special characters. it is meant to be that way.

obda, in a Batch File, is there any command like VBS such as vbCrLf? I believe this puts the line on the left of the file. I am not sure if this removes any spaces from the left or not. Any idea?

Thank You.

Ben
0
 
oBdACommented:
Sorry, my error; two additional spaces after the first two lines, because they didn't have any spaces in them.
This takes care of that (needs W2k or later) and changes the execution month as well:

@echo off
setlocal enabledelayedexpansion
set InputFile=c:\batch_bo_report_param.txt
set OutputFile=c:\batch_bo_report_param-new.txt
set NewExecutionDate=2005-05-10
for /f "tokens=1,2 delims=-" %%a in ("%NewExecutionDate%") do set NewExecutionMonth=%%a-%%b
if exist "%OutputFile%" del "%OutputFile%"
for /f "tokens=1,2 delims==" %%a in ('type "%InputFile%"') do (
  set aaToken=%%a
  if /i "!aaToken:~0,14!"=="Execution Date" (
    >>"%OutputFile%" echo Execution Date ^(YYYY-MM-DD^) : =%NewExecutionDate%
  ) else (
    if /i "%aaToken:~0,15%"=="Execution Month" (
      >>"%OutputFile%" echo Execution Month ^(YYYY-MM-DD^) : =%NewExecutionMonth%
    ) else (
      >>"%OutputFile%" echo %%a=%%b
    )
  )
)
0
 
oBdACommented:
Sorry, typo in the "Execution Month" line.
Replace the percent signs in this line
if /i "%aaToken:~0,15%"=="Execution Month" (
with exclamation marks:
if /i "!aaToken:~0,15!"=="Execution Month" (
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now