BATCH - Copy N files from a Windows directory

0

I am doing a process to copy different files from different folders, with the particularity that I need to pass, for example, 30 files of a directory (which contains more than 100). The 30 that I must pass, no matter what they are, should be 30.

I did something similar to this, here I use it to show the names just:

set contador=0

FOR /R d:\Users\usuario\Desktop\ %%A IN (*) DO (
    IF %contador% LSS 3 (
    echo %%A
    echo %contador%        
    SET /a contador=contador+1
    )ELSE (
    EXIT
    )   
    )

What happened was that inside the FOR, the counter was always "0". If I printed it outside of the FOR, it would have the correct number. So I realized that the FOR criteria for each file inside the directory (the *) does not work for me.

How do I limit it? so that it travels only 30 files and not all those that are inside the folder?

Thanks and regards!

    
asked by Nico.C 13.04.2018 в 21:35
source

1 answer

1

I recommend using PowerShell , not only because it is simpler and easier, but for all the other things that it will provide when you start using it (syntax, objects, modules, etxc) .

For what you comment could be resolved as follows:

#Variables
$cantidadArchivos = 3
$carpetaOrigen = 'C:\Users\vmsilvamolina\Desktop\Origen\'
$carpetaDestino = 'C:\Users\vmsilvamolina\Desktop\Destino\'
#Copia de los archivos
for ($i=0; $i -lt $cantidadArchivos; $i++) {
    Write-host "Copiando:" (Get-ChildItem $carpetaOrigen)[$i].FullName 
    Copy-Item (Get-ChildItem $carpetaOrigen)[$i].FullName -Destination $carpetaDestino
}

There you can define in the first three variables what you need (number of files to copy, source and destination) and that's it!

Edited:

In batch, it is done in the following way:

@echo off

    setlocal enableextensions enabledelayedexpansion

    rem Defino las variables
    set "origen=C:\Users\vmsilvamolina\Desktop\Origen"
    set "destino=C:\Users\vmsilvamolina\Desktop\Destino"
    set num=3

    rem Bloque de ejecución
    for /F "tokens=1,2 delims=:" %%f in ('dir /b /a "%origen%\*"  ^| findstr /n "^" ') do (
        if %%f leq %num% (
            echo Copiando: "%origen%\%%g"
            copy "%origen%\%%g" "%destino%" /y > nul 
        ) else goto fin
    )

:fin
    endlocal

For more info, I recommend you read the following link: FOR / F

    
answered by 14.04.2018 / 17:00
source