Move files with condition
Moderator: DosItHelp
Move files with condition
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.
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.
Re: Move files with condition
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
Regards
aGerman
Re: Move files with condition
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.
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.
Re: Move files with condition
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
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
-
- Posts: 208
- Joined: 26 Dec 2013 09:28
- Contact:
Re: Move files with condition
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
Re: Move files with condition
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.
Re: Move files with condition
Are you saying that the filenames are not the same?
Why did you say they were the same in the original post?
Why did you say they were the same in the original post?
Re: Move files with condition
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.
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.
Re: Move files with condition
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?
Re: Move files with condition
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
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
Re: Move files with condition
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
Re: Move files with condition
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.
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.
Re: Move files with condition
I edited the code - see how that goes.
I forgot that the time and date use illegal filename characters and had to change them.
I forgot that the time and date use illegal filename characters and had to change them.
Re: Move files with condition
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.
Re: Move files with condition
Try it now.