0

I'm trying to automate my folder-tree creation, where I request to user the year (that can be more than one), and after that I create folder from 01 to 12 (January to December) inside the year folder. Into of each month's folders, I'd can be create another folders according to my necessity, to mantain organization.

When I run the .bat script in my Windows environment, I receive command syntax error but, reviewed the whole script, and I can't find where is the error. I think that what I'm want to do is not so difficult, therefore I'll put below the whole script imagining that, with basic knowledge of reading and writing scripts, you from community be able to understand that I want to do.

@echo off
setlocal EnableDelayedExpansion
setlocal EnableExtensions
PUSHD "%~dp0"
set "curpath=%~dp0"

set /p years=Insert a year (in case more than one value, years can be separated by comma.Ex.:2018,2019):

REM For each year that the user insert(separated by comma) the loop for creates a respective folder
for %%G in (%years%) do (

    set "srcpath=%curpath%%%G"
    mkdir "!srcpath!"

    set /a loopcount=12
    set /a increment=1

    REM Beginning here the loop where will create month folder(01 to 12), inside year folder
    :beginloop
        if !increment! LSS 10 (
            set "fmtinc=0!increment!"
        ) else (
            set "fmtinc=!increment!"
        )

        mkdir "!srcpath!\!fmtinc!\DCTF"
        mkdir "!srcpath!\!fmtinc!\Despesas"
        mkdir "!srcpath!\!fmtinc!\DeSTDA"
        mkdir "!srcpath!\!fmtinc!\Folha Pagamento"
        mkdir "!srcpath!\!fmtinc!\GFIP"
        mkdir "!srcpath!\!fmtinc!\Notas Fiscais"
        mkdir "!srcpath!\!fmtinc!\Notas Fiscais\NF Entrada"
        mkdir "!srcpath!\!fmtinc!\Notas Fiscais\NF Entrada\PDF"
        mkdir "!srcpath!\!fmtinc!\Notas Fiscais\NF Entrada\XML"
        mkdir "!srcpath!\!fmtinc!\Notas Fiscais\NF Obra"
        mkdir "!srcpath!\!fmtinc!\Notas Fiscais\NF Saida"
        mkdir "!srcpath!\!fmtinc!\Notas Fiscais\NF Saida\PDF"
        mkdir "!srcpath!\!fmtinc!\Notas Fiscais\NF Saida\XML"
        mkdir "!srcpath!\!fmtinc!\Notas Fiscais\NFS Prestados"
        mkdir "!srcpath!\!fmtinc!\Notas Fiscais\NFS Prestados\PDF"
        mkdir "!srcpath!\!fmtinc!\Notas Fiscais\NFS Prestados\XML"
        mkdir "!srcpath!\!fmtinc!\Notas Fiscais\NFS Tomados"
        mkdir "!srcpath!\!fmtinc!\Notas Fiscais\NFS Tomados\Outros Municipios"
        mkdir "!srcpath!\!fmtinc!\Notas Fiscais\Relatorio Faturamento Mensal"
        mkdir "!srcpath!\!fmtinc!\RAIS"
        mkdir "!srcpath!\!fmtinc!\GIA"
        mkdir "!srcpath!\!fmtinc!\SPED"
        mkdir "!srcpath!\!fmtinc!\SPED\EFD ICMS e IPI"
        mkdir "!srcpath!\!fmtinc!\SPED\EFD Contribuições"

        set /a increment+=1
        set /a loopcount-=1

        REM While the value of decrement variable loopcount not iquals to 0(zero) the loop continues creating new folder for next month, redirecting program flow of here to :beginloop
        if !loopcount! == 0 ( goto exitloop )

        goto beginloop

    :exitloop

)
PAUSE

In that case, user must put and run the script directly in the folder that he want to create all the new folders. For that, in the beginning is stored the path where script is located in the variable curpath.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • 1
    Is the syntax error a secret? Did you think to run the script with `echo on` from the Command Prompt? Did you consider adding `pause` commands, to create temporary breakpoints to help you to isolate command lines? That said, you cannot have labels inside your [tag:for-loop]! – Compo Aug 24 '19 at 23:43

1 Answers1

0
@echo off
setlocal EnableExtensions EnableDelayedExpansion
PUSHD "%~dp0"
set "curpath=%~dp0"

set /p years=Insert a year (in case more than one value, years can be separated by comma.Ex.:2018,2019):

REM For each year that the user insert(separated by comma) the loop for creates a respective folder
for %%G in (%years%) do (

    set "srcpath=%curpath%%%G"
    mkdir "!srcpath!"
    rem month numbers+100
    FOR /L %%m IN (101,1,112) DO (
      SET /a monthnumber=%%m
      FOR %%t IN (
        "DCTF"
        "Despesas"
        "DeSTDA"
        "Folha Pagamento"
        "GFIP"
        "Notas Fiscais"
        "Notas Fiscais\NF Entrada"
        "Notas Fiscais\NF Entrada\PDF"
        "Notas Fiscais\NF Entrada\XML"
        "Notas Fiscais\NF Obra"
        "Notas Fiscais\NF Saida"
        "Notas Fiscais\NF Saida\PDF"
        "Notas Fiscais\NF Saida\XML"
        "Notas Fiscais\NFS Prestados"
        "Notas Fiscais\NFS Prestados\PDF"
        "Notas Fiscais\NFS Prestados\XML"
        "Notas Fiscais\NFS Tomados"
        "Notas Fiscais\NFS Tomados\Outros Municipios"
        "Notas Fiscais\Relatorio Faturamento Mensal"
        "RAIS"
        "GIA"
        "SPED"
        "SPED\EFD ICMS e IPI"
        "SPED\EFD Contribuições"
      ) DO mkdir "!srcpath!\!monthnumber:~-2!\%%~t"
    )
)

POPD

GOTO :EOF

Your major problem is that labels are not allowed within a code block (parenthesised sequence of lines).

It's possible to combine the setlocal statements as shown.

See for /? from the prompt for documentation about for /L Essentially, for /L %%x in (startnumber,increment,endnumber) sets %%x to each number in the range in turn.

monthnumber is thus set to 101..112 - always 3 digits; we want the last 2 of these. Note that the metavariable %%m must be assigned to a standard environment variable to allow substringing. set /a is used as this is a numeric assignment, not a string assignment.

The for %%t loop assigns each quoted name to %%t in turn.

Then all that's required is to create the directory - built by joining the base directoryname srcpath to the last 2 characters of the month-number monthnumber and the leaf-name %%t where the ~ removes the enclosing quotes " from the string in %%t.

Append 2>nul to the mkdir command to suppress error messages (eg. when the directory already exists)

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • You can probably lose another six lines; there's no need to make directory branches when making others deeper into it. `"Notas Fiscais"`, `"Notas Fiscais\NF Entrada"`, `"Notas Fiscais\NF Saida"`, `"Notas Fiscais\NFS Prestados"`, `"Notas Fiscais\NFS Tomados"`, and `"SPED"`. – Compo Aug 25 '19 at 00:16
  • Thank you so much!!! I didn't knew about the rule of the `goto` command, that when we put this one inside a `for` loop, the `for` is cancelled. – Luis Gustavo Aug 25 '19 at 18:23