Create batch to browse folders dynamically

1

I have a strange problem with a BAT file in Windows and I'm not sure what's happening.

I have created a bat file to go through the folders that I indicate by parameters. In the bat folder, I have an app folder and I want to get all the files that are inside it, in the folders that I indicate.

The call would be something like

mibat.bat folder1 folder2 folder3.

In the bat file I have the following code:

for %%a in (%*) do (

    echo Recorriendo la carpeta %%a
    set parameterFolder = %%a
    set parameterPath = app\%parameterFolder%

    for /R %parameterPath% %%v in (*.js) do (
        echo %%v
    )

)

The problem is that the route of the for is not taking it correctly and it removes all the files js that are inside the folder where the bat is, and not within app\folder1 , app\folder2 or app\folder3...

Does anyone know how I can solve this ???

    
asked by Alejandro 20.02.2018 в 16:03
source

2 answers

1

The problem is that in a bat file, the variables are not updated within a FOR or an IF:

That is, this code:

set FOO=BAR
if 1==1 (
  set FOO=CAMBIADO
  echo %FOO%
)

Print BAR

To solve this, you have to use the setlocal enabledelayeedexpansion directive and reference the variables between exclamation points (instead of percentages)

The code from before would look like this:

setlocal enabledelayedexpansion
set FOO=BAR
if 1==1 (
  set FOO=CAMBIADO
  echo !FOO!
)

Print CAMBIADO

So, your code should be fixed changed the following:

setlocal enabledelayedexpansion
for %%a in (%*) do (

    echo Recorriendo la carpeta %%a
    set parameterFolder = %%a
    set parameterPath = app\!parameterFolder!

    for /R !parameterPath! %%v in (*.js) do (
        echo %%v
    )

)
    
answered by 06.08.2018 в 00:50
0

Hello, good morning, I need to go through a file to read values from a configuration file, when I have a value, I pick up the server address, the username and password.

configuracion.txt
    open 10.0.0.4
    administrador
    abcd1234

set fichConexion=conexion.txt

@echo on
for /F %%A IN (Q:\PROYECTOS\ftp 10.0.0.4 practicasadmi2\ftp_conf_func_log_mail\ultima_version\conexion.txt) DO @ECHO %%A
for /F %%A IN (%fichConexion%) DO @ECHO %%A
pause>nul

My problem is when I have several users in the configuration file.

configuracion.txt
    open 10.0.0.4
    administrador
    pepito1234
    open 10.0.0.5
    administrador
    grillo1234

Once you take the user data the second step is to run a bat that collects that data connects to the server via FTP and downloads some files. Somebody could help me? I had thought to do a for inside another for

    
answered by 16.10.2018 в 09:41