locked
IF condition within FOR loop not executing RRS feed

  • Question

  • The code below is supposed to iterate through each .txt file in TEST folder. And skipping each (i+1 and i+2) iteration; copy the file to the same folder, removing the last numeric value of the filename, and appending the current iteration loop value.

    This perhaps may illustrate it: when iteration starts on filename1.txt, (create copies of filename1.txt, renamed filename2.txt and filename3.txt)... when fourth iteration is reached (create copies of filename4.txt, renamed filename5.txt and filename6.txt)... so on and so forth.

    My issue is the  batch exits without warning when the for loop starts!!! I've check everything, what am I missing? (I AM AWARE POWERSHELL IS THE BETTER TOOL FOR THIS - But I'm in love with batch, and enjoy learning to push it every which way :-)).

    @echo off

    setlocal enabledelayedexpansion


    set iter=1
    set count=1

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

        if %count%equ%iter% (
        set /a count+=1

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

        set /a count+=1

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

        set /a count+=1

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

    pause



    • Edited by F_Gordon Saturday, March 28, 2015 10:44 PM
    Saturday, March 28, 2015 9:59 PM

Answers

  • You need to replace
    if %count%equ%iter%
    with
    if %count% equ %iter% (because the statement "if 5equ6" makes no sense!)
    or with
    if %count%==%iter%

    You must also replace the curly bracket at the end with a round one.

    • Marked as answer by F_Gordon Sunday, March 29, 2015 12:20 AM
    Sunday, March 29, 2015 12:04 AM

All replies

  • Have you considered the fact that batch is obsolete?

    ¯\_(ツ)_/¯

    Saturday, March 28, 2015 11:20 PM
  • So is vinyl, but it still plays music.
    Sunday, March 29, 2015 12:03 AM
  • You need to replace
    if %count%equ%iter%
    with
    if %count% equ %iter% (because the statement "if 5equ6" makes no sense!)
    or with
    if %count%==%iter%

    You must also replace the curly bracket at the end with a round one.

    • Marked as answer by F_Gordon Sunday, March 29, 2015 12:20 AM
    Sunday, March 29, 2015 12:04 AM
  • So is vinyl, but it still plays music.

    If you consider scratches music.

    I still have a copy of "Dark Side of the Moon" as a half speed master.  Do you want to buy it?


    ¯\_(ツ)_/¯

    Sunday, March 29, 2015 12:10 AM
  • @jrvThe crackling, the raw quality, it's all part of the charm and character... I've already got a Floyd collection on CD thanks! :D 

    @Frederik Long... I am blind, and probably stupid aswell! :D thanks!
    Sunday, March 29, 2015 12:20 AM
  • @jrv The crackling, the raw quality, it's all part of the charm and character... I've already got a Floyd collection on CD thanks! :D 

    Bravo - be well.


    ¯\_(ツ)_/¯

    Sunday, March 29, 2015 12:39 AM