3

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 is in the FMove section of the file. No matter what I put in the for line, it gives me an error.

I want this to have the %HoldLoc% value, but have been hard coding it because of errors I get.

The environment is Windows 2008 R2 server.

Variations I have tried, as well as with and without quotes in the parentheses:

FOR %F IN (%HoldLoc%)
FOR %F IN (%%HoldLock%) 
FOR %F IN (c:\Play\hold\*.tmp) 

My Code:

SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION

echo on
set DataMax=20
set DataLoc=C:\Play\Data
Set HoldLoc=C:\Play\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
echo %HoldLoc%
FOR %F IN (c:\Play\hold\*.tmp) DO IF !SrcCount! LSS %SrcMax% (
    SET /A SrcCount += 1
    move /y %F  "%DataLoc%"
    )

Problem is that I get this in the output window, why won't the C be seen?

C:>set /A count=count+1

C:>if 19 GEQ 20 (Goto Exit )  else (GOTO FMove )

C:>Echo Gather Top 30 files
Gather Top 30 files

C:>set SrcCount=0

C:>set SrcMax=30

C:>echo C:\Play\Hold
C:\Play\Hold
\Play\hold\*.tmp) was unexpected at this time.

C:>FOR \Play\hold\*.tmp) DO IF !SrcCount! LSS 30 (

C:>
CarenRose
  • 1,266
  • 1
  • 12
  • 24
John
  • 31
  • 1
  • 1
  • 3

3 Answers3

4
@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
)
Robie Nayak
  • 819
  • 10
  • 18
0

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.

Andriy M
  • 76,112
  • 17
  • 94
  • 154
  • I tried that but it does not work. I updated the text as I need to include for spaces in the path but tried to make it down to the simpilest to find the error. I am sure it is something that simple but I just dont see it. The error returned with this updated loop: set SrcCount=0 set SrcMax=30 echo %HoldLoc% FOR %%G IN (c:\Data\**.tmp) DO IF !SrcCount! LSS %SrcMax% ( SET /A SrcCount += 1 move /Y %G "%DataLoc%" ) is now: FOR %F IN (c:\Play\hold\*.tmp) DO IF !SrcCount! LSS %SrcMax% ( – John Feb 03 '12 at 11:42
  • OK, sorry. This is the code and that DID help, but I'm still missing something. FOR /F %%G IN ('DIR /B %HoldLoc%\*.tmp') DO IF !SrcCount! LSS %SrcMax% ( SET /A SrcCount += 1 move /y %G %%DataLoc% ) goto Exit but it is not giving out the path for the source: C:\Play\Ho ld>set SrcMax=30 C:\Play\Ho ld>FOR /F %G IN ('DIR /B C:\play\H old\*.tmp') DO IF !SrcCount! LSS 30 ( SET /A SrcCount += 1 move /y C:\play\D ata ) The system cannot find the file specified. C:\Play\Ho ld>REM echo !SrcCount! move /y DataLoc" C:\Play\Ho ld>goto Exit – John Feb 03 '12 at 11:52
  • @John: 1) Enclose `%HoldLoc%*.tmp` in double quotes: `FOR /F %%G IN ('DIR /B "%HoldLoc%*.tmp"') DO …`. 2) On the line where the `move` command is: 2.1) change `%G` to `%%G`; 2.2) enclose `%%G` in double quotes 2.3) remove an extra `%` at the beginning of `%%DataLoc%`; 2.4) enclose `%DataLoc%` in double quotes. The resulting command would be: `move /y "%%G" "%DataLoc%"`. – Andriy M Feb 03 '12 at 14:51
  • @John: And 3) Please be sure to ask questions as relevant to your actual problems as possible. The one you've asked in the original post seems to be too simplified, because, as I can see now, you have to deal with paths/names that include spaces, and that was not obvious in your original question. If I knew as much at first, I would surely advise you to use double quotes right away. I mean, it would be more helpful for other people if all your issues were presented in the original post rather than in comments. – Andriy M Feb 03 '12 at 14:57
0

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
John
  • 1