Page 1 of 1
Moving files depending on their size
Posted: 07 Feb 2013 08:32
by Matt20687
Hello,
I am stuck and i hope you can help!
I am trying to create a batch file that will run on a schedule. It will check a folder and any files that are over 3072 bytes it will move it to another folder called Moved Files.
Does anyone know how to do this?
Re: Moving files depending on their size
Posted: 07 Feb 2013 08:47
by Matt20687
This is what i have so far, it moves all files in the folder rather than the ones that are under 3072 bytes. This is because i am defining the file as *.* i assume. I cannot figure out how to set the ones under 3072 bytes as a variable or something.
Code: Select all
set "dispatch=C:\Users\Matthew\Desktop\MM Image Count\Test folder"
for %%A in (%dispatch%) do if %%~zA LEQ 3072 move "*.*" "C:\Users\Matthew\Desktop\MM Image Count\Movefolder"
Re: Moving files depending on their size
Posted: 07 Feb 2013 08:58
by Squashman
%dispatch%\*.*
move %%A
Re: Moving files depending on their size
Posted: 07 Feb 2013 09:11
by Matt20687
set "dispatch=C:\Users\Matthew\Desktop\MM Image Count\Test folder"
for %%A in (%dispatch%\*.*) do if %%~zA LEQ 3072 move "%%A" "C:\Users\Matthew\Desktop\MM Image Count\Movefolder"
So it should look like this?
Re: Moving files depending on their size
Posted: 07 Feb 2013 09:42
by Squashman
Well your folder path has spaces so you will need quotes.
Re: Moving files depending on their size
Posted: 07 Feb 2013 14:44
by Matt20687
Thanks. My code which works based on it moving files which are only 3kb in size to another folder works great. This is below
Code: Select all
set "movefolder=C:\Documents and Settings\Administrator\Desktop\Rogue Images Folder"
set "dispatch=E:\Matt\Test\dispatch\*.*"
for %%A in ("%dispatch%") do (
if %%~zA LEQ 3072 move "%%A" "%movefolder%"
)
I have one last request that i cannot figure out. As i have said this runs perfectly but what i would like to happen now is when it does the check for any files that are smaller than 3kb i would like it to launch a .exe. Lets call it test.exe for sake of a name.
I have tried to figure this out but i cannot get it to run once, it runs on the loop and runs the .exe as many times as there are files under 3kb. I assumed i would have to do a count of the amount of times the above coding searches through and finds a file under 3kb and then to do a simple if %count% GEQ 1 do ( start test.exe).
Anyone have any thoughts on how i can get a .exe to only run once if the above coding finds a 3kb rather than running it as many times as there are 3kb files (e.g. 5 3kb file would run test.exe 5 times).
Thanks,
Matt
Re: Moving files depending on their size
Posted: 07 Feb 2013 14:56
by foxidrive
Code: Select all
set "movefolder=C:\Documents and Settings\Administrator\Desktop\Rogue Images Folder"
set "dispatch=E:\Matt\Test\dispatch\*.*"
set "flag="
for %%A in ("%dispatch%") do (
if %%~zA LEQ 3072 (
move "%%A" "%movefolder%"
set flag=1
)
)
if defined flag test.exe
Re: Moving files depending on their size
Posted: 07 Feb 2013 14:59
by Matt20687
Thanks for this but how does it still move the files that are under 3kb as well as running the .exe only once?
Re: Moving files depending on their size
Posted: 07 Feb 2013 15:00
by Matt20687
Ignore me lol
Re: Moving files depending on their size
Posted: 07 Feb 2013 15:13
by foxidrive
Note that I modified the code above. Now it will only run the test.exe if files are moved.