Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

1 Answer 1

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.