0

I want the batch file to move files %3 number of times, how can I accomplish this?

%1 is the source folder
%2 is the destination folder
%3 is the number of files
%4 is the filter.
This is the best I could come up with but it doesn't seem to work consistently.

@echo off
SETLOCAL EnableDelayedExpansion
set movedFiles=0
if [%4] EQU [] goto regular
:special
for /R  "%1" %%G in (%4) do (
    echo moving "%4"... "%%G"
    move /Y "%%G" "%2"
    set /a movedFiles+="1"
    if !movedFiles! EQU %3 GOTO endOfCopy
)
GOTO endOfCopy
:regular
for /R  "%1" %%G in (*) do (
    echo moving... "%%G"
    move /Y "%%G" "%2"
    set /a movedFiles+="1"
    if !movedFiles! EQU %3 GOTO endOfCopy
)
:endOfCopy
echo Done, %movedFiles% files Where copied successfully
ENDLOCAL
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • 4
    "it doesn't seem to work consistently" - what exactly does it do? – Stephan Apr 24 '19 at 13:29
  • Well, the %3 part does nothing. It moves the files alright, but doesn't consider %3. – A Box Of Cheese Apr 24 '19 at 14:09
  • seems to work fine for me (and consistent). – Stephan Apr 24 '19 at 14:17
  • So I didn't mess something up? Didn't expect that. Have any idea what might be the cause of my problem? – A Box Of Cheese Apr 24 '19 at 14:24
  • My apologies, I'm a newbie here as it might appear. – A Box Of Cheese Apr 24 '19 at 14:27
  • 1
    To get an idea what might cause your problem, you will have to tell us *what* problem you have. Please edit your question accordingly (how exactly are you executing the script and what exactly happens that you didn't expect?). – Stephan Apr 24 '19 at 14:27
  • No problem, so now we have that covered.. what do you get when you `echo %3` – Gerhard Apr 24 '19 at 14:28
  • `EQU` is the operator two compare two 32-bit signed integer values. It works also for string comparisons because of `cmd.exe` runs automatically a string comparison on one of the two operands cannot be successfully converted from a string to an integer value. `[` and `]` which do not have any special meaning and are therefore interpreted like `abcd` results in operands being never valid integers. So don't use `EQU` to compare case-sensitive two strings, use the operator `==` which is designed for string comparisons and makes always a string comparison even if both strings represent integers. – Mofi Apr 24 '19 at 17:16
  • So use `if "%~4" == ""` instead of `if [%4] EQU []` because of `"` has a special meaning as it results in interpreting everything between the double quotes as literal character with exception of `%` and `!` if delayed expansion is enabled. See also [Symbol equivalent to NEQ, LSS, GTR, etc. in Windows batch files](https://stackoverflow.com/a/47386323/3074564). Wrong is also `"%1"` and correct would be `"%~1"`. Wrong is `"%2" because of correct is `"%~2"` as well as `"%4"` which should be `"%~4"`. Wrong is `set /a movedFiles+="1"` and correct is `set /a movedFiles+=1`. – Mofi Apr 24 '19 at 17:24

1 Answers1

0

Perhaps you could change your code a little, (and in the meantime see if it fixes your issue):

@Echo Off & SetLocal EnableExtensions DisableDelayedExpansion
If "%~4"=="" (Call :StartMove %* "*") Else Call :StartMove %*
Pause & GoTo :EOF

:StartMove
Set "Moved=0" & For /R "%~1" %%A In ("%~4") Do (SetLocal EnableDelayedExpansion
    If !Moved! Equ %3 (EndLocal & Set "Moved=%Moved%" & GoTo EndMessage)
    Echo ...Moving "%%A" & Move /Y "%%A" "%~2" >Nul 2>&1 && Set /A Moved +=1)

:EndMessage
Echo Done, %Moved% files were copied successfully & EndLocal & Exit /B

Please note that this code, like yours, does not verify input parameters, (whether they exist, are of the correct type, in the correct order etc), I'd suggest you implement something to do so, moving forward. Even adding something as simple as the following would be a start:

If Not Exist "%~1\" Exit /B
If Not Exist "%~2\" Exit /B
If "%~3"=="" Exit /B
Compo
  • 36,585
  • 5
  • 27
  • 39