Move files with condition

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
zagix
Posts: 68
Joined: 16 Oct 2013 23:19

Move files with condition

#1 Post by zagix » 11 Jan 2014 14:34

Hello,

How to move the files from sub-folders with a script which monitors the sub-folders every 1 minute and imposing
a restriction to move only when there .DONE files are found.

For example the file is BPL_001.IMG and BPL_001.XML
and its DONE file is BPL_001.IMG.DONE and BPL_001.XML.DONE, and create a log file of all moved files.

Start Folder/Folder-01/folder-AAA/folder-containting files
Start Folder/Folder-02/folder-AAA/folder-containting files

Your Help is required.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Move files with condition

#2 Post by aGerman » 11 Jan 2014 16:54

What should happen with the .DONE files where the related .IMG and .XML files are already moved? Are the .DONE files created in the same sub folder as the related .IMG and .XML files? Where do you want to move the files? What informations do you want to save in the LOG file and where should it be saved?

Regards
aGerman

zagix
Posts: 68
Joined: 16 Oct 2013 23:19

Re: Move files with condition

#3 Post by zagix » 12 Jan 2014 01:43

Hi,

Thanks for reply, condition is that if matched .DONE files of .IMG & .XML are found in same folder at G:\Collection then move the .IMG & .XML files first and onwards their.DONE files to C:\Generated.

The script to continuosly monitor the sub folders every 1 minute and if the above conditions are met move the files

Regarding contents of log file. The log file will be saved at c:\log with file names moved with date & time and log file saved on files moved sub folder name + current date and time. Creating new log files every time with moved files.

Help required.

Thanks in advance.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Move files with condition

#4 Post by foxidrive » 12 Jan 2014 02:46

This is untested - timeout is a command from Vista and later.

It assumes that the name of the *.img and *.xml files are unique so that the same filenames don't already exist in c:\Generated

Code: Select all

@echo off
setlocal enabledelayedexpansion
:loop
for /r "g:\collection" %%a in (*.img) do (
if exist "%%~dpa\%%~na.img" if exist "%%~dpa\%%~na.xml" if exist "%%~dpa\%%~na.img.done" if exist "%%~dpa\%%~na.xml.done" (
set d=!date!
set t=!time!
echo !d! @ !t! moving "%%~dpa" files
move "%%~dpa\%%~na.img" "C:\Generated" >nul
move "%%~dpa\%%~na.xml" "C:\Generated" >nul
move "%%~dpa\%%~na.img.done" "C:\Generated" >nul
move "%%~dpa\%%~na.xml.done" "C:\Generated" >nul
for %%b in ("%%~dpa\.") do >"c:\logs\%%b - !d! @ !t!.log" echo !d! @ !t! moved files to "C:\Generated" "%%~dpa\%%~na.img", "%%~dpa\%%~na.xml","%%~dpa\%%~na.img.done","%%~dpa\%%~na.xml.done"
)
)
timeout /t 60 /nobreak
goto :loop

siberia-man
Posts: 208
Joined: 26 Dec 2013 09:28
Contact:

Re: Move files with condition

#5 Post by siberia-man » 12 Jan 2014 03:50

It is not clear from the first post whether .IMG and .XML should be moved consequentially or separately. So the code below implements moving of all available files FILENAME having the pair file called FILENAME.DONE. As I understood from the discussions .DONE files will be removed after movement of source files.

Code: Select all

@echo off

setlocal enabledelayedexpansion

set "move_src=%~dp0src"
set "move_dst=%~dp0dst"
set "move_ext=.DONE"

for /f "tokens=*" %%f in ( 'dir /b "%move_src%\*%move_ext%" 2^>nul' ) do (
   set "move_donename=%move_src%\%%~f"
   set "move_filename=%move_src%\%%~nf"
   if exist "!move_filename!" (
      echo:Moving !move_filename!
      move "!move_filename!" "!move_dst!" >nul && del "!move_donename!"
   )
)

endlocal

zagix
Posts: 68
Joined: 16 Oct 2013 23:19

Re: Move files with condition

#6 Post by zagix » 12 Jan 2014 06:20

foxidrive wrote:It assumes that the name of the *.img and *.xml files are unique so that the same filenames don't already exist in c:\Generated


Thanks you Foxidrive for help.

Actually the .img and .img.done are unique/identical ones & .xml and .xml.done are identicals.
For example: VAT_000000001.img and VAT_000000001.img.done
and for same set xml are SRL_000000001.xml and SRL_000000001.xml.done.
In this case the file are not moved becasue of unique condition applied to all.

On testing if found no log files are created in c:\logs

Hope to have a favourable reply.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Move files with condition

#7 Post by foxidrive » 12 Jan 2014 06:54

Are you saying that the filenames are not the same?

Why did you say they were the same in the original post?

zagix
Posts: 68
Joined: 16 Oct 2013 23:19

Re: Move files with condition

#8 Post by zagix » 12 Jan 2014 08:41

Hello,

I have similar / identical files as well as non-identicals also this works with unique sets only if we create uniques of two then this codes will solve the problem in single go.
Like in .img and its .done files as a set & secondly xml. and it done.as a set.

Sorry for troubling you.
Thanks in advance.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Move files with condition

#9 Post by Squashman » 12 Jan 2014 09:59

zagix wrote:
foxidrive wrote:It assumes that the name of the *.img and *.xml files are unique so that the same filenames don't already exist in c:\Generated


Thanks you Foxidrive for help.

Actually the .img and .img.done are unique/identical ones & .xml and .xml.done are identicals.
For example: VAT_000000001.img and VAT_000000001.img.done
and for same set xml are SRL_000000001.xml and SRL_000000001.xml.done.
In this case the file are not moved becasue of unique condition applied to all.

On testing if found no log files are created in c:\logs

Hope to have a favourable reply.

I am confused. They both have a .done file. Why are they not supposed to be moved?

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: Move files with condition

#10 Post by alan_b » 12 Jan 2014 14:40

The entire concept of moving files from partitions G:\*\ to C:\*\ is flawed,
with great potential for error that may be concealed until you try to use them.

Move G:\*\ to C:\*\ will copy,
and then delete the original with no second chance if the first attempt suffers data corruption.

You can of course copy and then validate the duplicate before deleting the original,
BUT YOU AIN'T validating the original copy - you are validating what the Windows cache remembers when the source was read,
and there could have been a one-time-only read error that results in a permanent error in the "duplicate"

When I copy a file from a USB2 Flash drive to an internal HDD,
I can then use "HashMyFiles" from Nirsoft to compute the checksum of the source and then the destination.
That always appears successful - until I realise that it APPEARS to read and compute the usb2 Flash a bit faster than it does the internal HDD.
Then I "safely remove" the flash drive, which flushes that part of Windows cache,
and then I reconnect the flash drive and read and compute is SO MUCH SLOWER.

To flush the contents of G:\*\* from the cache is more tedious than safe removal - you need a reboot.

Moving from one folder to another folder on the same partition avoids all the above problems.
The actual file is not modified so therefore it cannot become corrupted,
and it is so much faster.
I think all that Windows does is a small tweak to the MFT on NTFS partitions, or a tweak to the FAT tables on FAT32 partitions.

Regards
Alan

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Move files with condition

#11 Post by foxidrive » 12 Jan 2014 22:39

See if this works for you:

Code: Select all

@echo off
setlocal enabledelayedexpansion
:loop
for /r "g:\collection" %%a in (*.img) do (
if exist "%%~dpa\*.xml" if exist "%%~dpa\*.img.done" if exist "%%~dpa\*.xml.done" (
   set d=!date:/=-!
   set t=!time::=.!
   echo !d! @ !t! moving "%%~dpa" files
      for %%b in ("%%~dpa\.") do (
         for %%c in ("%%~dpa\*") do (
            >>"c:\logs\%%~nxb - !d! @ !t!.log" echo !d! @ !t! moved file to "C:\Generated" - "%%c"
         )
      )
   move "%%~dpa\*.img"      "C:\Generated" >nul
   move "%%~dpa\*.xml"      "C:\Generated" >nul
   move "%%~dpa\*.img.done" "C:\Generated" >nul
   move "%%~dpa\*.xml.done" "C:\Generated" >nul
)
)
timeout /t 60 /nobreak
goto :loop

zagix
Posts: 68
Joined: 16 Oct 2013 23:19

Re: Move files with condition

#12 Post by zagix » 13 Jan 2014 03:08

Hi,

Thank you Foxidrive, the files are moving to destination accordingly, but sorry to inform that still the log is not created.
Please have a look.

Thanking you.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Move files with condition

#13 Post by foxidrive » 13 Jan 2014 03:27

I edited the code - see how that goes.

I forgot that the time and date use illegal filename characters and had to change them.

zagix
Posts: 68
Joined: 16 Oct 2013 23:19

Re: Move files with condition

#14 Post by zagix » 13 Jan 2014 06:02

foxidrive wrote:I edited the code - see how that goes.

I forgot that the time and date use illegal filename characters and had to change them.


Hi Foxidrive, Still it does not create the log file.
The error: "The filename, directory name, or volume label syntax is incorrect."
Please have a look.
Thanks.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Move files with condition

#15 Post by foxidrive » 13 Jan 2014 06:25

Try it now.

Post Reply