3

I'm playing around with batch files and delayed expansions.
I am simply attempting to print the file/folder names of all items within a directory.

@ECHO off

SET title=%1
SET mp4="mp4"
SET mkv="mkv"
SET avi="avi"
SET minimumSize=300

CD "E:\Documents\Films\Need_compressing"
SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%i IN (*) DO (
    SET item = %%i
    ECHO !item!
    :: print dir/file name
    :: if "!i!" == "%title%" (
    ::  echo "found file!"

    :: 'if' directory
    ::  if exist "%title" (
    ::      echo "is a directory"
    ::      cd %title%
    ::      echo "title: " %title%
    ::      for %%f in (*.*) do (

    ::          set file = %%f
    ::          set extension = %%~xf
    ::          set fileSize = %%~zf

    ::          if %extension% == %mp4% if %extension% == %mkv% if %extension% == %avi% (
    ::              if %fileSize% gtr %minimumSize% (
    ::                  move %file% %compressFolder%
    ::              )
    ::          ) 
    ::      )
    ::  )
    ::)
)
ENDLOCAL

When I do this, I would get the error:

The syntax of this command is incorrect

The only thing I can think of is that I'm using the delayed expansion wrongly.

wmash
  • 4,032
  • 3
  • 31
  • 69

1 Answers1

3

The reason for the syntax error is because :: are not for comments. They're actually label markers. Use REM for comments.

Other issues...

Remove the spaces around the =

SET item=%%i

You also don't have to use delayed expansion if you use call.

CALL ECHO %%item%%

Also, keep in mind this FOR command will only show you files (no folders). If you want folder and files, just use DIR /B (no FOR). If you want to process them, you can use FOR /F like this:

FOR /F "delims=" %%a IN ('DIR /B') DO ( echo do stuff with "%%~a" )

(edit) Based on your updated file, you might also wish to use CD /D which will switch to your E: (unless you're already on E:, in which case you don't need it).

soja
  • 1,587
  • 8
  • 8
  • Then you're not including your whole batch file. It works here. – soja Oct 17 '16 at 00:01
  • I will amend the post to include the whole batch file. – wmash Oct 17 '16 at 00:02
  • I looked at the whole batch file and I don't see an issue (beyond what is already mentioned). It still works fine for me, assuming I change the folder path to something that exists on my system, and I fix the `SET item=%%i` line. Can you try exiting your cmd window and retrying? Are you sure there isn't anything else? – soja Oct 17 '16 at 00:11
  • @soja I think the cause of the problem must be something else. If it was just the spaces around "=", then it would just print "!item!" as it would be an undefined variable but not an error. – Klitos Kyriacou Oct 17 '16 at 00:12
  • I have just found the problem. For some reason, a commented block was causing the issue. I will mention it in my post and maybe you can figure out why it's causing an issue – wmash Oct 17 '16 at 00:13
  • I'm accepting this answer as you provided very useful knowledge on the comments and also on how to write the `FOR` loop correctly for directories as well as files. Thank you :) – wmash Oct 17 '16 at 09:16