1

I am looping trough a folder. I want to understand if there are 20 zip files. If these 20 zip files are present then another batch will call. Otherwise I need to build a powershell script to send an email to somebody as error message

setlocal enableextensions enabledelayedexpansion
set /a count = 1
for /f %%a in (\\ITWS2162\work\SCM\RawData\SSC_Proj\*.zip) do (
   set /a count += 1
   set "TRUE="
   if %%~xa=.zip && %%~xa == 20  set TRUE = 1
   ECHO "there are 20 files Matrioska Process starts"
   IF defined TRUE (
      C:\Users\RefosLeo\Desktop\Python\motherbatch.bat
      ) ELSE (
         EXIT
   )
)

Please tell me what is wrong because, actually this code does not work

avery_larry
  • 2,069
  • 1
  • 5
  • 17
  • Does replacing `IF defined TRUE (` with `if %%~xa=.zip && %%~xa == 20 (` work? You are assigning a value to variable `%TRUE%`, but doing that inside `(` `)` requires `SETLOCAL ENABLEDELAYEDEXPANSION` ( https://stackoverflow.com/questions/6679907/how-do-setlocal-and-enabledelayedexpansion-work ) – Renat Oct 30 '19 at 14:49
  • It seems it is not enough –  Oct 30 '19 at 15:23

2 Answers2

0

untested You can use find to count the number of files and just use the count directly:

for /f %%a in ('dir /b \\ITWS2162\work\SCM\RawData\SSC_Proj\*.zip ^| find /c /v ""') do (
   if /i %%a EQU 20 (
      rem do what you want here when it equals 20
      C:\Users\RefosLeo\Desktop\Python\motherbatch.bat
   )
)

This finds exactly 20 zip files. You could change EQU to GEQ if you actually want to find 20 or more .zip files.

Though you might want the inverse -- exit when less than or not equal to 20:

for /f %%a in ('dir /b \\ITWS2162\work\SCM\RawData\SSC_Proj\*.zip ^| find /c /v ""') do (
   if /i %%a LSS 20 exit
)
rem continue here with the rest of your script
C:\Users\RefosLeo\Desktop\Python\motherbatch.bat

That is anything less than 20. If you want to exit when not equal (so less than or greater than 20) you would use if /i %%a NEQ 20 exit

If you want to continue with the method you started with, I think the following might be what you were going for:

setlocal enableextensions enabledelayedexpansion
set /a count = 0
for /f %%a in (\\ITWS2162\work\SCM\RawData\SSC_Proj\*.zip) do (
   set /a count += 1
   if /i !count! EQU 20 (
      ECHO "there are 20 files Matrioska Process starts"
      C:\Users\RefosLeo\Desktop\Python\motherbatch.bat
   )
)
if /i %count% LSS 20 exit

You can't put the exit inside the for loop because you'll never know until the for loop is complete that you didn't reach 20 zip files.

avery_larry
  • 2,069
  • 1
  • 5
  • 17
  • Unlike the code in the original question, which attempts to find and count only `.zip` files, this does not. The count will relate to files whose extensions begin with `.zip`. *It may not have been important in this specific case, hence the acceptance, but I thought I'd mention it anyhow.* – Compo Oct 30 '19 at 16:14
0

There isn't actually any need for a , delayed expansion, or setting any variables, to perform your task. Here therefore is a method which uses and .

@"%__AppDir__%xcopy.exe" "\\ITWS2162\work\SCM\RawData\SSC_Proj\*.zip" . /LQ^
 | "%__AppDir__%findstr.exe" "\<20\>" 1> NUL && (
    "C:\Users\RefosLeo\Desktop\Python\motherbatch.bat") || (Exit /B)

The above has a flaw, in that it counts any files with extensions beginning with .zip as opposed to those which are exactly zip. If this is an issue to you, you could try using where.exe and instead:

@"%__AppDir__%where.exe" "\\ITWS2162\work\SCM\RawData\SSC_Proj":"*.zip" 2> NUL^
 | "%__AppDir__%find.exe" /V /N "" | "%__AppDir__%find.exe" "[20]" 1> NUL && (
    "C:\Users\RefosLeo\Desktop\Python\motherbatch.bat") || (Exit /B)

The examples above are both single line commands, split over several lines for readability. I have left the parenthesized command (Exit /B) as you had, because you'll likely want to replace Exit /B with your command(s) to build and send the email. You may, depending upon your specific circumstances, wish to Call motherbatch.bat instead.

Compo
  • 36,585
  • 5
  • 27
  • 39