1

I have a bunch of folders that I have to move to another directory. To do this task I made a .bat. My intention is to select the folders that I want to move and drag on the .bat file. This is working with the following syntax for one folder at a time, what I have to do if I want to do the same with multiple folders at a time?

BONUS questions, how can I copy only the files that are in the folders without copying the folders itself?

xcopy %* C:\DOCUMENTALE\dms_in\ /s
  • 1
    why don't use robocopy? it has options to include/exclude files/directories https://serverfault.com/q/778763/343888 https://stackoverflow.com/q/14511537/995714 – phuclv Jan 09 '18 at 11:16

1 Answers1

0

The SHIFT command is what you are looking for.

@ECHO OFF
:LOOP
IF NOT "%1"=="" (
    xcopy %1 C:\DOCUMENTALE\dms_in\ /s
    SHIFT
    GOTO LOOP
)

When d&ding several files or folders to your batch script, their paths are being appended as arguments. %1 is the first path, %2 is the second one and so on. SHIFT does nothing but moving the index to the right so %2 becomes %1, %3 becomes %2 and so on. The script keeps on looping until it reaches the last appended path which becomes %1. The next execution of SHIFT sets %1 to an empty string, the IF condition is no longer fulfilled and the script stops.

Now let's get to your bonus question:

There are several ways to achieve this. Here one of those:

@ECHO OFF
:LOOP
IF NOT "%1"=="" (
    FOR /F %%F IN ('dir /b /s /a-d "%1"') DO (
        COPY /y %%F C:\DOCUMENTALE\dms_in\
    )
)
MichaelS
  • 5,941
  • 6
  • 31
  • 46
  • 1
    I would suggest using **`%1`**, **`%2`** & **`%3`** etc. as opposed to **`%1%`**, **`%2%`** & **`%3%`**. – Compo Jan 09 '18 at 12:53
  • @Compo Thx, you are right, this is the correct way. I'll edit my answer. However, %1% works as well. – MichaelS Jan 09 '18 at 13:09
  • 1
    @MichaelS because `IF NOT "%1%"==""` will be parsed as 2 separate variables. The first `%1` will be replaced with the first argument, but the next `%` is not followed by any valid variable name, hence the `%` will be stripped off due to [rule 1.4](https://stackoverflow.com/q/4094699/995714) – phuclv Jan 10 '18 at 13:30