0 byte file moved

August 20, 2009 at 08:21:45
Specs: Server 2003
I have a scenario where I need to have a batch file that can scan a directory, find any 0 byte files, and move them to another location.

There will be a number of different files in the directory, and the file names will always be changing.

It would look like this:
find if any file in D:\Oldlocation = 0 bytes (file type and name need to be variable, size is the only parameter)
If no 0 byte files, exit
If a 0 byte file exists, move that file out of that dir to D:\newlocation

Where do I start?

Thanks for your help!

See More: 0 byte file moved

Report •

August 20, 2009 at 12:22:32
@echo off
pushd D:\Oldlocation
for %%j in (*.*) do if %%~zj equ 0 move "%%j" "D:\newlocation"

Report •

August 20, 2009 at 12:30:27
Perfect!!!!! It worked flawlessly!

Thank you very much for your help!

(sooner or later I'll learn this stuff for myself!)

Thanks again!


Report •

August 20, 2009 at 21:34:46
I was proud when I got my code working after more than an hour trying... But then I saw that amazingly tiny code of the master Ivo!
Well, practice is a good thing, but perusing the code of the masters can save us a lot of headache.

@echo off
setlocal enabledelayedexpansion
set /a ct=0
set source-v="C:\test"
set target-v="d:\newlocation"
set resdir=%temp%\filesf.txt
set out=!resdir:~0,2!
dir /s %source-v%\*.* |findstr /i /v "%out%"|findstr ":"|findstr /v "<DIR>" > %resdir%
for /f "tokens=3 delims= " %%a in (%resdir%) do (
set /a ct=!ct!+1
if %%a==0 call :step2
goto :end
set /a ctline=0
for /f "tokens=4-10 delims= " %%b in (%resdir%) do (
set /a ctline=!ctline!+1
if !ctline!==!ct! move %source-v%\"%%b %%c %%d %%e %%f %%g %%h" %target-v%
goto :eof

Report •

Related Solutions

August 21, 2009 at 05:51:39
Agreed! I was somewhat shocked to see a single line instead of a page. I expected to see code more like Ricardo's...

I can understand the syntax and flow of Ricardo's code, Master IVO's code looks like gibberish to me. I was surprised it worked on the first try!

Thanks again!

Report •

August 21, 2009 at 07:12:02
Some more syntax beyond the basic "FOR %%i in (set) do ..."

<<You can now use the following optional syntax:
%~i - expands %i removing any surrounding quotes (")
%~fi - expands %i to a fully qualified path name
%~di - expands %i to a drive letter only
%~pi - expands %i to a path only
%~ni - expands %i to a file name only
%~xi - expands %i to a file extension only
%~si - expanded path contains short names only
%~ai - expands %i to file attributes of file
%~ti - expands %i to date/time of file
%~zi - expands %i to size of file
%~$PATH:i - searches the directories listed in the PATH environment variable and expands %i to the fully qualified name of the first one found.
If the environment variable name is not defined or the file is not found by the search, then this modifier expands to the empty string>>

Extracted from http://www.robvanderwoude.com/ntfor... where you can see the complete For command reference.>>

A good command reference tutorial (the better I know) can the accessed in your own computer by typing:
%windir%\hh.exe ms-its:%windir% help\ntcmds.chm::/ntcmds.htm
(tested in Win XP)

I believe many people dont know this.
I also use hh.exe to open folders by a command: hh %temp%\my-tests (its usefull to me in checking results when I am testing some script that deal with files in diferent folders).

For better use put hh.exe into your %path%.

Report •

Ask Question