Batch file to move files if gtr 0

CompaqProLiant
September 26, 2007 at 09:16:24
Specs: Win2000k, 2Gigs
Hey, just stuck on a little something that I'm overlooking obviously. I'm using the following to check for files in a specified directory. If the size is greater than 0, I want to move those that are greater than 0 to an archive folder.

As you can see, what it is doing is moveing every file to the archive not just those greater than zero size. So I'm obvioulsy missing something...any thoughts?

@echo off
Rem Echo Filename & Size
cls
for %%a in (dir C:\CoreFTPOutgoing\*.txt) do (
if %%~za GTR 1 (
move C:\CoreFTPOutgoing\*.txt C:\CoreFTPOutgoing\archive\
echo %%a %%~za
)
)


See More: Batch file to move files if gtr 0

Report •


#1
September 26, 2007 at 16:22:56
It's moving every file because your script is telling it too with this:
move C:\CoreFTPOutgoing\*.txt C:\CoreFTPOutgoing\archive\

Try this:

@echo off
Rem Echo Filename & Size
cls
for %%a in ('dir C:\CoreFTPOutgoing\*.txt') do (
if %%~za GTR 1 (
move %%a C:\CoreFTPOutgoing\archive\
echo %%a %%~za
)
)

I'm also baffled how your script works, because your not enclosing the parsed command in your FOR set in single quotes, but it still works.

"Computer security." — Oxymoron


Report •

#2
September 26, 2007 at 18:47:50
Simple! He's envoking the orginal use of FOR. He's running the loop once for dir, and once for every C:\CoreFTPOutgoing\*.txt

My version (untested):
@ECHO OFF
PUSHD C:\CoreFTPOutgoing
FOR %%a IN (*.txt) DO IF %%~Za NEQ 0 MOVE "%%a" archive
POPD


Report •

#3
September 26, 2007 at 22:16:45
What do you mean by the original use of FOR?

"Computer security." — Oxymoron


Report •

Related Solutions

#4
September 26, 2007 at 22:42:46
H:\scripts>for /?
Runs a specified command for each file in a set of files.

FOR %variable IN (set) DO command [command-parameters]

%variable Specifies a single letter replaceable parameter.
(set) Specifies a set of one or more files. Wildcards may be used.
command Specifies the command to carry out for each file.
command-parameters
Specifies parameters or switches for the specified command.


Report •

#5
September 26, 2007 at 23:14:59
Rofl.

C:\toolbox>for /?
...snipped
FOR /F ["options"] %variable IN (file-set) DO command [command-parameters]
FOR /F ["options"] %variable IN ("string") DO command [command-parameters]
FOR /F ["options"] %variable IN ('command') DO command [command-parameters]
...snipped

Maybe I should have asked, when do you know you should use single quotes and when they are not necessary?


"Computer security." — Oxymoron


Report •

#6
September 27, 2007 at 03:06:28
Look for the /F?

Report •


Ask Question