Nested IF condition inside FOR loop not executing

I have attached an image which should illustrate the issue. Basically the script (bottom), when run, should Make two copies of a file determined by the "iter" and "count" variables, copy them to the same folder, and rename them according to the existing sequence.

From left to right: CMD displays script running - TEST folder before script runs - TEST folder after script runs

As one can see the TEST folder (right) displays the correct output up until the "iter" reaches "10". The second nested IF statement in the FOR loop is never executed.

I quickly wrote this WORKING batch to test multiple if conditions:

@echo off

set var=101

if %var% lss 10 (
echo var is less than 10
)
if %var% geq 10 if %var% leq 99 (
echo var is between 9 and 100
)
if %var% gtr 99 if %var% leq 200 (
echo var is between 99 and 200
) else (
echo %var% is beyond bounds
)
pause

So why then is my script below not working as it should?

@echo off

setlocal enabledelayedexpansion


set iter=1
set count=1

for %%i in (C:\TEST\*.txt) do (

set name=%%~dpni

pause

    if %count%==%iter% (
       
       
        if !iter! lss 9 (
        set /a count+=1
      
        copy "%%i" "!name:~0,-1!!count!%%~xi"

        set /a count+=1

        copy "%%i" "!name:~0,-1!!count!%%~xi"

        set /a count+=1

        )

        if !iter! gtr 9 if !iter! lss 100 (
        set /a count+=1

echo "!name:~0,-2!!count!%%~xi"
pause
        copy "%%i" "!name:~0,-2!!count!%%~xi"

        set /a count+=1

        copy "%%i" "!name:~0,-2!!count!%%~xi"

        set /a count+=1

        )
       
    ) else (
    set /a iter+=1
    )
)

pause



  • Edited by CMJR1979 Sunday, March 29, 2015 5:53 PM
March 29th, 2015 5:48pm

Okay... I think I've misunderstood my own "iter". It reaches double digits in the last part of the filename when it reaches 4 not 10... I will try modifying and report back.

UPDATE: Nope, still not working, mind is numbing!

UPDATE: I mixed up !iter! with !count! in the IF conditions. Now its WORKS!!!

...I know Powershell! :D

Free Windows Admin Tool Kit Click here and download it now
March 29th, 2015 6:02pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics