1

I am looping through a particular directory and if a zip file or files exist they are moved to a secondary folder in that directory however the code below doesn't seem to work i.e. it doesn't move the file. What am I doing wrong?

for %%i in (D:\test\*.*) do if %%~xi == ".zip" move /Y "D:\test\%%~ni%%~xi" "D:\test\complete"

1 Answer 1

3

When comparing the file extensions, put the current file's extension in quotation marks as well, like so:

for %%i in (D:\test\*.*) do if "%%~xi" == ".zip" move /Y "D:\test\%%~ni%%~xi" "D:\test\complete"

I tested it, and it works fine.

To use a subroutine, try this:

for %%i in (D:\test\*.*) do call :checkextension %%i

:checkextension
if "%~x1" == ".zip" move /Y "%~1" "D:\test\complete"
goto:eof

Note how each file's path (%%i) is passed as a parameter to the :checkextension subroutine, which then accesses it as %~1 (or %~x1 when only the extension is needed).

2
  • That worked. Thanks a bundle. Are the quotation marks required? Secondly I am attempting to create a subroutine as follows for %%i in (D:\test\*.*) call :checkextension :checkextension if "%%~xi" == ".zip" move /Y "D:\test\%%~ni%%~xi" "D:\test\complete" however it doesn't work. Aug 26, 2012 at 23:23
  • You'll have to pass the filename as a parameter to the subroutine, as it cannot access the FOR command's parameters. I'll add a working example to my answer.
    – Indrek
    Aug 26, 2012 at 23:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .