0

How to run a command with two variables change? One is incremented and the other is a variable determined by some path.

The problem is that the command does not allow to load multiple path in one command. I have over 10 folders for each check and I have to generate an unique filename incremented for each command for it is not overwritten by the next command.

Here an example might be more understandable but it does not work because there does not increment and thus it overwrites the previous file.

    @echo off

    SET "basename=file"
    SET /a outname=0

    :genloop
    SET /a outname+=1

    IF EXIST "%basename%-%outname%.csv" GOTO genloop
    SET "outname=%basename%-%outname%.csv"

    command.exe /out %outname% /LoadDirectory "C:\Dir-EXAMPLE-1"
    command.exe /out %outname% /LoadDirectory "C:\Dir-EXAMPLE-ABC"
    command.exe /out %outname% /LoadDirectory "C:\Dir-EXAMPLE-XYZ"
    command.exe /out %outname% /LoadDirectory "C:\Dir-EXAMPLE-ETC"

The issue is related to this one How to create a unique output filename for Windows Script?

How can I do this in a simple and elegant way?

Thanks in advance.

Edit

Thanks to @MC ND & @ths, both solutions work perfectly and especially because you were very helpful and attentive. I do not know if it is possible to approve the two solutions, but if I have to make a choice I w'll chose the second option because it is closest to the question about the two variables and is well commented. I do not forget @Magoo because I appreciated his encouragement, thanks.

PS: Given my questions, the script is perfect as is, but maybe in the near future I will ask a new question related to it to make it more sophisticated in some scenarios. (in the possibilities of script interpreter)

PPS: Sorry for other people if my question was not very clear and concise. Feel free to modify the content and tags to make it more intelligible.

5
  • What does "it does not work" mean? Can you describe the behavior you are seeing? Commented Sep 25, 2014 at 17:42
  • @SLawson I edited the content of my question. Thanks Commented Sep 25, 2014 at 18:03
  • Please provide a clear example of how these "over 10" directories are determined. Are they in a file, perhaps? Are they subdirectories matching some sort of a pattern? (A list of three or four will probably suffice. We can normally extrapolate to ten+.) What is you desired outname in each case? Go on - go absolutely mad! Ask for something ourageous if you like. Perhaps file.anumber.csv would be all that can be achieved - but if we don't know what your heart's desire is, we aren't even going to attempt to grant your wish. Do you want the files generated in the nominated directory? Commented Sep 25, 2014 at 23:47
  • Hi @Magoo thank you :) Yes I want to do more, but the problem is difficult enough for me for now. Ideally I would like the script determines a list of directories from a string search by keywords and then executes a command in each directory. But for now I'll do it manually. No, I Don't want the files generated in the nominated directory. Commented Sep 26, 2014 at 2:50
  • @Magoo For your question, no, I don't want the files generated in the nominated directory but where the batch is launched. Please look this example with the command "dir" pastebin.com/HY4TmWCj Commented Sep 26, 2014 at 2:56

2 Answers 2

1
@echo off
    setlocal enableextensions disabledelayedexpansion

    rem configure script 
    set "basename=file"
    set "counter=1000000"
    set folders="C:\Dir-EXAMPLE-1" "C:\Dir-EXAMPLE-ABC" "C:\Dir-EXAMPLE-XYZ" "C:\Dir-EXAMPLE-ETC" 

    rem Search the last file in the sequence
    for /f "tokens=2 delims=-.0" %%a in ('
        dir /b /a-d /o-n "%basename%-??????.csv" 2^>nul
    ') do set /a "counter=1000000+%%a" & goto done
    :done

    rem Iterate the folders list
    for %%a in (%folders%) do (

        rem increment file counter
        set /a "counter+=1"

        rem get access to the counter with delayed expansion, but
        rem to prevent problems with possible exclamations in paths
        rem disable delayed expansion before executing the commands
        rem so, we need the `for` to store the counter data into %%b

        setlocal enabledelayedexpansion
        for %%b in (!counter:~-6!) do (
            endlocal 

            rem just for testing, generate the output file
            rem remove this line in real code
            type nul >"%basename%-%%b.csv" 

            rem execute the command - command is only echoed to console
            rem if the output is correct, remove the echo
            echo command.exe /out "%basename%-%%b.csv" /LoadDirectory "%%~a"
        )
    )
Sign up to request clarification or add additional context in comments.

Comments

1

you basically have most required components already, i'll add the concept of subroutine:

@echo off

SET "basename=file"
SET /a outnum=0

call :genloop
command.exe /out %outname% /LoadDirectory "C:\Dir-EXAMPLE-1"
call :genloop
command.exe /out %outname% /LoadDirectory "C:\Dir-EXAMPLE-ABC"
call :genloop
command.exe /out %outname% /LoadDirectory "C:\Dir-EXAMPLE-XYZ"
call :genloop
command.exe /out %outname% /LoadDirectory "C:\Dir-EXAMPLE-ETC"

goto :eof

:genloop
SET /a outnum+=1

IF EXIST "%basename%-%outnum%.csv" Call :genloop
SET "outname=%basename%-%outnum%.csv"
goto :eof

3 Comments

Hi, it only generates two files file-1.csv & file-file-2.csv.csv Here is the code followed by the log pastebin.com/HY4TmWCj pastebin.com/qPeFJMgx
ah, because you used outname as count variable as well as file name. see update.
Just a mistake, you miss one ":" between call and genloop, after that, it works perfectly. Again, thanks for your help @ths

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.