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

Change string in .txt dynamically

I have a batch file that does a lot of processes.  One included is reading a number from an area of the file.  It looks like this:
for /f "tokens=1,2 skip=2 delims==" %%a in ('find /i "PATIENT_PREFIX" c:\RadiologyTechnologySeeMyRad_EIRMC\FILES.txt') do @set myvar11=%%b

This is not the same number nor the same number of characters, but it needs to be.  So, how can I read the value as I now set it as myvar11 for later use, but i want it as myvar11 + preceding zeros to make it a 9 digit number.  

Examples:
4848  should be 000004848
448392 should be 000448392
1 should be 000000001
832756491    should be 832756491

I need every single number to be a 9 digit number.  Precedign zeros to be included if it is shorter for whatever reason.  Thoughts?  I am hoping we can just have another line that somehow creates myvar12 as the number or at least the correct number of '0's and I could concatenate the two variables later.
0
weklica
Asked:
weklica
  • 7
  • 3
  • 2
  • +1
1 Solution
 
paultomasiCommented:
Once you have your number in your variable myvar11, do the following:

   set myvar11=000000000%myvar11%
   set myvar11=%myvar11:~-9%

myvar11 will now be left-zero-padded.
0
 
paultomasiCommented:
Here's the full thing:
for /f "tokens=1,2 skip=2 delims==" %%a in ('find /i "PATIENT_PREFIX" c:\RadiologyTechnologySeeMyRad_EIRMC\FILES.txt') do @set myvar11=%%b
set myvar11=000000000%myvar11%
set myvar11=%myvar11:~-9%

Open in new window

That's the proper way to do it! :)
0
 
paultomasiCommented:
Another way you can do it is like this
for /f "tokens=1,2 skip=2 delims==" %%a in ('find /i "PATIENT_PREFIX" c:\RadiologyTechnologySeeMyRad_EIRMC\FILES.txt') do @set myvar11=000000000%%b
set myvar11=%myvar11:~-9%

Open in new window

Notice how I added the zeros in the FOR-loop itself... It's just a shortcut and means one less line of code.
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
Bill PrewCommented:
And just for some variety, if you had to do this a number of times in a script you could creeat a small subroutine to do the work, as in:

@echo off
setlocal EnableDelayedExpansion
for /f "tokens=1,2 skip=2 delims==" %%a in ('find /i "PATIENT_PREFIX" c:\RadiologyTechnologySeeMyRad_EIRMC\FILES.txt') do (
  set myvar11=%%b
  call :ZeroPad myvar11 9
)
goto :EOF

:ZeroPad [variable-name] [length]
  set "%~1=00000000000000000000!%~1!"
  set "%~1=!%~1:~-%~2!"
  goto :EOF

Open in new window

~bp
0
 
paultomasiCommented:
bp

May I suggest some improvements to your code above (all of which is purely academic and therefore, not really relevent to the question).

Line 4 can be:

   set myvar11=%%~b

even though we're unlikely to come across quoted text in this application here.

Lines 5 and 9 refer to :ZeroPad which would benefit from being changed to LZeroPad (as opposed to RZeroPad)... It just makes more sense this way.

Line 10 imposes a limit of 20 padded zeros. This means myvar11 can never be more than 20 characters wide. Whereas Line 5 sets a limit of just 9 characters. Would it not have been better to make this dynamic as in the following:

:LZeroPad [variable-name] [length]
  set zStr=
  for %%a in (1,1,%2) do set zStr=0!zStr!
  set zStr=!zStr!%1
  set %1=!zStr:~-%2!
goto :eof
0
 
paultomasiCommented:
bp

Oops! I left out the '/L" at the start of the FOR-loop. It meant to be:


:LZeroPad [variable-name] [length]
  set zStr=
  for /l %%a in (1,1,%2) do set zStr=0!zStr!
  set zStr=!zStr!%1
  set %1=!zStr:~-%2!
goto :eof
0
 
Bill PrewCommented:
@Paul,

Yes, I had considered taking a Fill() type approach when I originally created my little padding routine, but I settled on the hardcoded approach and just always make the padding string large enough to handle the needs of the script at hand.  It saves me the for loop logic and that can add up when calling the subroutine many times in a script.  Less "dynamic" as you point out, but I prefer the performance gain and slightly simpler code in this case.  Thanks.

~bp
0
 
Bill PrewCommented:
@Paul,

I did notice one error in your suggestion, this line:

set zStr=!zStr!%1

needs to be:

set zStr=!zStr!!%1!

since %1 is the name of a variable, not the actual string to be processed.

As to line 4, that was from the original posters code, and I used that on the assumption that he already knew it would never have quotes around it (which it won't based on what I read here).

Reflecting a little further on your comments though, I think perhaps we have just given birth to 2 new BAT subroutines for future use.  Thinking about all your comments at once, I decide that there may be times where the following routines could be useful.

:LPad [pad-character] [variable-name] [length]
  set "_Pad=!%~2!"
  for /L %%A in (1,1,%~3) do set "_Pad=%~1!_Pad!"
  set "%~2=!_Pad:~-%~3!"
  goto :EOF

:RPad [pad-character] [variable-name] [length]
  set "_Pad=!%~2!"
  for /L %%A in (1,1,%~3) do set "_Pad=!_Pad!%~1"
  set "%~2=!_Pad:~0,%~3!"
  goto :EOF

Open in new window

Running the following test driver with these:

@echo off
setlocal EnableDelayedExpansion
set Temp=12345
call :LPad "0" Temp 20
echo [%Temp%]
set Temp=12345
call :RPad "0" Temp 20
echo [%Temp%]
set Temp=12345
call :LPad " " Temp 20
echo [%Temp%]
set Temp=12345
call :RPad " " Temp 20
echo [%Temp%]
goto :EOF

Open in new window

Produced the desired result of:

[00000000000000012345]
[12345000000000000000]
[               12345]
[12345               ]

Open in new window

I like it, thanks.

~bp
0
 
paultomasiCommented:
billprew

Thank you for pointing out my error in the following line:

   set zStr=!zStr!!%1!

It's so easy at times to miss small (but VERY significant) detail... and this tends to occur more frequently when untesting code.

I particularly like the way you expanded (LZeroPad and RZeroPad) LPad and RPad to include any character as the pad-character as well as any length.

Not sure if something similar hasn't already been done but it looks good anyway.
0
 
Steve KnightIT ConsultancyCommented:
Somewhat neater and more general purpose than I did here....

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_25405798.html#a27929447
http://scripts.dragon-it.co.uk/links/batch-pad-number

Code noted and will add to that page if OK with link back here and the like.

Steve
0
 
weklicaAuthor Commented:
This is what I needed.  Two strings in my current script and it is working perfectly.  Thanks much
0
 
paultomasiCommented:
mucho gracias

0
 
Steve KnightIT ConsultancyCommented:
Is there no end to this mans talents.  Spanish too?
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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