If /I not - question on correct use of

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

If /I not - question on correct use of

#1 Post by Jer » 07 Mar 2016 01:47

In trying to avoid a goto, I am testing batch code to get user input of
a file name ending with .bat, which will lead to writing a batch file with that name.
I could not make it work with this:

Code: Select all

If not /I "%oFile:~-4,4%"==".BAT" Set "oFile=%oFile%.bat"


The issue with the code below is what to do if the name already contains .bat.
In that case I just want to continue.

Code: Select all

@Echo Off

set "oFile=somefile.bat"
echo before: %oFile%
If /I "%oFile:~-4,4%"==".BAT" (Echo ok with .bat) Else Set "oFile=%oFile%.bat"
echo after: %oFile%

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: If not /I - question on correct use of

#2 Post by miskox » 07 Mar 2016 02:40

Can it help if you delete ".bat" from the string first and then add it?

Code: Select all

set oFile=%oFile:.bat=%.bat


Saso

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: If not /I - question on correct use of

#3 Post by foxidrive » 07 Mar 2016 03:40

Code: Select all

@echo off
set "oFile=somefile.bat"
for %%a in ("%oFile%") do if /i not "%%~xa"==".BAT" Set "oFile=%oFile%.bat"

Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

Re: If not /I - question on correct use of

#4 Post by Jer » 08 Mar 2016 10:35

Learned something...put the /I before the 'not'. Also that miskox' solution
is case insensitive, so .bat or .BAT get changed.

As a matter of curiosity, my time measurement test did not show any of these
three methods to have an advantage:

Code: Select all

@echo off

set "oFile=somefile"
set startTime=%time%
for %%i in (1,1,100000) do if /i not "%oFile:~-4,4%"==".BAT" Set "oFile=%oFile%.bat"
Echo If /i not... scenario start and end times: %startTime% - %time%

set "oFile=somefile.BAT"
set startTime=%time%
for %%i in (1,1,100000) do Set oFile=%oFile:.bat=%.bat
Echo remove and add back scenario start and end times: %startTime% - %time%


set "oFile=somefile"
set startTime=%time%
for %%i in (1,1,100000) Do (
   for %%a in ("%oFile%") do if /i not "%%~xa"==".BAT" Set "oFile=%oFile%.bat"
)
Echo for loop with if /i not... scenario start and end times: %startTime% - %time%


If /i not... scenario start and end times: 8:28:54.37 - 8:28:54.37
remove and add back scenario start and end times: 8:28:54.37 - 8:28:54.37
for loop with if /i not... scenario start and end times: 8:28:54.37 - 8:28:54.37

Resolved and thankyou.

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: If not /I - question on correct use of

#5 Post by miskox » 08 Mar 2016 10:52

You should use FOR /L.

Saso

Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

Re: If not /I - question on correct use of

#6 Post by Jer » 08 Mar 2016 14:43

Oops! forgot the /L. Thanks for pointing that out. What a difference with
100,000 iterations:
If /i not... scenario start and end times: 12:34:12.18 - 12:34:15.43
remove and add back scenario start and end times: 12:34:15.43 - 12:34:17.83
for loop with if /i not... scenario start and end times: 12:34:17.83 - 12:34:35.04

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: If not /I - question on correct use of

#7 Post by foxidrive » 08 Mar 2016 15:15

Jer wrote:What a difference with 100,000 iterations:
If /i not... scenario start and end times: 12:34:12.18 - 12:34:15.43
remove and add back scenario start and end times: 12:34:15.43 - 12:34:17.83
for loop with if /i not... scenario start and end times: 12:34:17.83 - 12:34:35.04


The last test is flawed as you're calling cmd every one of those 100,000 times to parse the file name.

Using this as the last test it is actually the fastest routine:

Code: Select all

set "oFile=somefile" & call :routine "%oFile%"
pause & goto :EOF
:routine
set startTime=%time%
for /L %%i in (1,1,100000) Do if /i not "%~x1"==".BAT" Set "oFile=%oFile%.bat"
Echo for loop with if /i not... scenario end time: %time%
pause


startTime: 8:17:49.44
If /i not... scenario end time: 8:17:51.65
remove and add back scenario end time: 8:17:53.58
for loop with if /i not... scenario end time: 8:17:53.92

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: If not /I - question on correct use of

#8 Post by sambul35 » 08 Mar 2016 16:34

Jer wrote:my time measurement test did not show any of these
three methods to have an advantage.


I the above user input scenario, I can't see reason in performance tuning due to user interaction being much longer than the time to process input. However, in other code scenarios where FINDSTR and REG QUERY or large directory search is involved for example, code optimization makes sense. Hence the question: how did you measure time of processing user input?

Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

Re: If /I not - question on correct use of :(thread title edited)

#9 Post by Jer » 08 Mar 2016 23:38

...Foxidrive's code wins, which I am using at batch level. This is just a one-time
check to generate a batch file name and extension from a command line entry,
after scrubbing spaces and problem characters out of the name. Only letters
and numbers and the underscore are allowed, to avoid glitches in the project.

I had no issue with timing, but thanks for the tip and example of how a routine can
optimize processing time.
Jerry

Post Reply