Copy file vs Move file in batch file

Microsoft Windows xp professional w/serv...
May 26, 2010 at 06:36:20
Specs: Windows XP
I'm trying to move all the files in a network directory to a separate network directory depending on whether the file has zero bytes or not. If the file has zero bytes it goes to one directory. If has more than zero bytes it goes to a different directory. If I use a copy command the batch file will process all the files correctly. However when I use the move command only the first file in the directory gets moved and the batch job ends abruptly with no error message.

My batch file:

for /f "tokens=* delims= " %%1 IN ('dir/b \\server1\directory\*.*') do (call :file_proc \\server1\directory\%%1 )


:file_proc
if %~z1 EQU 0 move /Y %~f1 \\server2\directoryA\%~nx1
if %~z1 GTR 0 move /Y %~f1 \\server2\directoryB\%~nx1

What am I doing wrong with the move command?

Thanks!


See More: Copy file vs Move file in batch file

Report •


#1
May 26, 2010 at 07:45:14
You for got you go to EOFs...

for /f "tokens=* delims= " %%1 IN ('dir/b \\server1\directory\*.*') do (
  call :file_proc \\server1\directory\%%1 )
goto :EOF

:file_proc
if %~z1 EQU 0 move /Y %~f1 \\server2\directoryA\%~nx1
if %~z1 GTR 0 move /Y %~f1 \\server2\directoryB\%~nx1
goto :EOF

Batch Scripting does not support Object Oriented Programming so you need the GOTO :EOFs.


Report •

#2
May 26, 2010 at 08:35:30
A couple of problems. First, as ace_omega said, you forgot to exit from your main part so it falls through to the subroutine at the end.

Secondly, and the reason you are getting the problem you report, is...

if %~z1 EQU 0 move /Y %~f1 \\server2\directoryA\%~nx1

... so at this point the file %1 no longer exists. And then you say:

if %~z1 GTR 0 move /Y %~f1 \\server2\directoryB\%~nx1

But since %z1 doesn't exist, this is an error.

Try:

if %~z1 EQU 0 (
move /Y %~f1 \\server2\directoryA\%~nx1
) else (
move /Y %~f1 \\server2\directoryB\%~nx1
)


Report •

#3
May 26, 2010 at 11:15:16
@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=* delims= " %%a in ('dir/b/a-d') do (
if %%~Za equ 0 (
move /y %%a dest
) else (
move /y %%a other
)
)


=====================================
Helping others achieve escape felicity

M2


Report •

Related Solutions

#4
May 26, 2010 at 12:54:02
Thanks for the information. With your help I made the changes and it is working as I had hoped.

Report •

Ask Question