Vald
Vald

Reputation: 23

Windows 2003 batch script with pipe

I'm trying to write a batch script that will run every 15 minutes, check number of files in a directory and if that number is bigger then set limit, move all files to another directory. I would easily do this in bash script, but this is my first batch script.

I split this task in several steps:

  1. Find number of files in directory. I managed to do this with this command:

    dir/b/a-d d:\test\test2 | find /v /c "::"

  2. Next thing is to assign output of this command to some variable so I can compare it with my desired limit. This is where problem starts.

    ECHO OFF
    setlocal enableextensions

    FOR /F "tokens=* USEBACKQ" %%a IN (`dir/b/a-d d:\test\test2 ^| find /v /c "::"`)
    DO (@SET NUMFIL=%%a)
    ECHO %NUMFIL%

    endlocal

I'm getting: "| was unexpected at this time". Obviously, pipe is getting in the way. I found that it is special character and as such must be escaped with caret. After doing so, I'm getting: "The syntax of the command was incorrect." This is Windows server 2003.

3.After getting this problem solved, I plan to insert something like this:

IF %%NUMFIL%% > 20
(move "d:\test\test2\ti*" "d:\test\test2\dir\")

That would move all that files (all of them starts with "ti") to desired directory.

So my questions would be: what to do with #2 issue and will #3 work in this case?

Upvotes: 2

Views: 736

Answers (2)

foxidrive
foxidrive

Reputation: 41257

I think this should work. Note that it does not use backticks.

ECHO OFF
setlocal enableextensions
FOR /F %%a IN (' dir /b /a-d "d:\test\test2" ^| find /c /v "" ') DO SET NUMFIL=%%a
ECHO %NUMFIL%
IF %NUMFIL% GTR 20 (move "d:\test\test2\ti*" "d:\test\test2\dir\")

Upvotes: 0

Magoo
Magoo

Reputation: 80138

Not sure ":: will work in your first case, since :: is unlikely to appear in a DIR output. A single colon would suffice, since the /c option of find counts the number of LINES in which the target string occurs.

The secret to the second problem is that the do keyword must occur on the same line as the closing-parenthesis of the IN clause. It is possible to break the structure into

FOR /F "tokens=* USEBACKQ" %%a IN (
 ' dir/b/a-d d:\test\test2 ^| find /v /c ":" '
) DO (SET NUMFIL=%%a)

Note the @ is not required - it suppresses the command's being echoed, which is turned off by the initial @echo off (the @ there suppresses the echoing of the ECHO OFF

Also, the parentheses around this set are not required. IF they are used, the open-parenthesis must occur on the same physical line as the do.

You also don't need to use usebackq since you have no need here to change the interpretation of quotes.

Third item - > is a redirector. For a comparison operator, use one of EQU GEQ LSS LEQ GTR NEQ depending on comparison required.

And again, the open-parenthesis must be on the same line as the if. With an else, the close-parenthsis before the ELSE , the ELSE keyword and the open-parenthesis after must all be on the same physical line.

Upvotes: 3

Related Questions