Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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"

share|improve this question
up vote 2 down vote accepted

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).

share|improve this answer
    
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. – PeanutsMonkey Aug 26 '12 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 '12 at 23:30

You must log in to answer this question.

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