Windows Batch file to move X number of files from folder to folder

前端 未结 3 1255
没有蜡笔的小新
没有蜡笔的小新 2021-01-14 15:48

I\'m trying to use a batch file to move files in blocks of 30 if there are less than 20 files in %DataLoc%. I modified code from a prior question. The problem

相关标签:
3条回答
  • 2021-01-14 16:38
    @echo off
    
    set Source=C:\perl\Drift_Bat\IN
    set Target=C:\perl\Drift_Bat\OUT
    
    set MaxLimit=20
    
    for /f "tokens=1* delims=[]" %%G in ('dir /A-D /B "%Source%\*.*" ^| find /v /n ""') do (
        move /y "%Source%\%%~nxH" "%Target%"
        if %%G==%MaxLimit% exit /b 0
    )
    
    0 讨论(0)
  • 2021-01-14 16:45

    Your script uses incorrect syntax for the loop variable in one of the two loops:

    FOR %F IN (c:\Play\hold\*.tmp) DO …
    

    Just try changing %F to %%F. Single percent sign plus letter is the syntax for loop variables when running loops directly from the command prompt. In batch scripts you should always use double-percent references for loop variables, just like in your FOR /F %%a loop.

    0 讨论(0)
  • 2021-01-14 16:52

    I updated the code and got it to work by changing some things. Thanks Andy for the tips but I could not get it to work with the suggestions - I wouldn't be surprised if I did not follow them and that is on MY side, not yours.

      FOR /F %%G IN ('DIR /B "%HoldLoc%"\*.tmp') DO IF !SrcCount! LSS %SrcMax% (
       SET /A SrcCount += 1 
       Echo "%HoldLoc%"
       Echo "%%G%"
       Echo "%SrcCount%
       move /y "%HoldLoc%"\"%%G" "%DataLoc%"
    

    )

    Here is what I ended up with - longer but still functional:

    SETLOCAL ENABLEEXTENSIONS
    SETLOCAL ENABLEDELAYEDEXPANSION
    
    echo on
    set DataMax=50
    set DataLoc=C:\Test Data (x86)
    Set HoldLoc=C:\Test Hold
    set count=0
    FOR /F %%a in ('DIR /B "%DataLoc%"\*.tmp') do set /A count=count+1
    if %count% GEQ %DataMax% (Goto Exit) else (GOTO FMove)
    :FMove
    Echo Gather Top 30 files
    set SrcCount=0
    set SrcMax=30
    FOR /F "TOKENS=*" %%a IN ('dir /A-D /O-D /B "%HoldLoc%"\*.tmp') DO (
            SET /A SrcCount += 1
            if !SrcCount! LEQ %SrcMax% (
            MOVE /y "%HoldLoc%\%%a" "%DataLoc%"
            )
        )
    goto Exit
    :Exit
    close
    
    0 讨论(0)
提交回复
热议问题