Need help writing a batch file for copying files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
domulation
Posts: 9
Joined: 25 Aug 2016 23:52

Need help writing a batch file for copying files

#1 Post by domulation » 25 Aug 2016 23:59

Hello all,

I'm fairly new at writing batch files so please bare with me here...
I need to write a batch file that does the following things:

1. Copy Oldest Dated File to destination(eg. 7/21)
2. Wait, When destination is emtpy, send next dated file(eg. 7/22, 7/23, 7/24, etc.)
3. Repeat step 2 until there are no more files to send

The reason I need the destination to be empty is because when a file is placed in there it gets "consumed" and then the file disappears after a period of time.
The source and destination will be within sub directories.

I have the basic batch file made to copy a file one at a time, but am unsure how to write the rest of the script to do the rest.
Btw this was just testing the script, obviously the source and destination will change.
Also, the files being copied will be sent across a network.

Thank you for any and all help. It is much appreciated.

@echo off
setlocal

set source=C:\testC
set dest=D:\testD
set exts=*.txt

pushd "%source%"

for /f "delims=" %%G in ('dir /b /o:d %exts%') do (
move /-y "%%~G" "%dest%"
goto :break
)
:break

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

Re: Need help writing a batch file for copying files

#2 Post by foxidrive » 26 Aug 2016 00:10

This is untested:

It gets the oldest filename from the source folder.
If a file doesn't exist in the source folder it will wait for 60 seconds and try again.

It moves the oldest to the destination folder and waits for 60 seconds
If the file is not longer there then it will loop to get the next file, otherwise it will wait for another 60 seconds.


Code: Select all

@echo off
setlocal
set "source=C:\testC"
set "dest=D:\testD"
set "exts=*.txt"
pushd "%source%"
:nextfile
set "file=
for /f "delims=" %%G in ('dir /b /o:-d %exts%') do set "file=%%G"
if not defined file timeout 60 /nobreak & goto :nextfile
move /-y "%file%" "%dest%"
:check
if exist "%dest%\%file%" timeout 60 /nobreak & goto :check
goto :nextfile

domulation
Posts: 9
Joined: 25 Aug 2016 23:52

Re: Need help writing a batch file for copying files

#3 Post by domulation » 26 Aug 2016 09:12

Thank you foxidrive!

Your code worked perfectly.
In addition, is there anyway to display the file name that has been transferred?

Again, thank you!
Much appreciated.

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

Re: Need help writing a batch file for copying files

#4 Post by foxidrive » 26 Aug 2016 09:47

domulation wrote:In addition, is there anyway to display the file name that has been transferred?

This change after the move line echos it to the screen and also places it in the windows titlebar.

Code: Select all

move /-y "%file%" "%dest%"
echo "%file%"
title "%file%"

domulation
Posts: 9
Joined: 25 Aug 2016 23:52

Re: Need help writing a batch file for copying files

#5 Post by domulation » 26 Aug 2016 11:05

Perfect! Thank you.

Is this possible to do with copy instead of move?
I changed the move to copy and after the destination file disappears it selects the same file it did before.
After the destination file disappears, I need it to select the next dated file from the source folder.
Example: If I have 2 files with 2 different dates, I want it to first copy the first file with the oldest date, then after the file disappears from the destination folder, the next file selected is the second oldest date.

Is this possible with copy, or just move?
As always, much appreciated.

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

Re: Need help writing a batch file for copying files

#6 Post by foxidrive » 26 Aug 2016 15:03

domulation wrote:Perfect! Thank you.

Is this possible to do with copy instead of move?
I changed the move to copy and after the destination file disappears it selects the same file it did before.

Yes, it does this because the code looks for the oldest file in the folder. When using copy the file hasn't been shifted and it is still the oldest file in the folder.

Any change is possible in programming, but I would like to ask why you asked to move the files to a server and have now changed the task after someone provided you with a solution? The logic I have used will have to be rewritten and it is now wasted time.

I'm only making this point because it is a common problem with people who are asking for programming help. See here: viewtopic.php?f=3&t=6108

domulation
Posts: 9
Joined: 25 Aug 2016 23:52

Re: Need help writing a batch file for copying files

#7 Post by domulation » 26 Aug 2016 17:41

To be fair on my part, in my original post, it did say copy and not move. I stated this several times.
My mistake was having the move function in my original code to begin with. I apologize for that and understand how it was misleading.

domulation wrote:Hello all,

I'm fairly new at writing batch files so please bare with me here...
I need to write a batch file that does the following things:

1. Copy Oldest Dated File to destination(eg. 7/21)
2. Wait, When destination is emtpy, send next dated file(eg. 7/22, 7/23, 7/24, etc.)
3. Repeat step 2 until there are no more files to send

The reason I need the destination to be empty is because when a file is placed in there it gets "consumed" and then the file disappears after a period of time.
The source and destination will be within sub directories.

I have the basic batch file made to copy a file one at a time, but am unsure how to write the rest of the script to do the rest.
Btw this was just testing the script, obviously the source and destination will change.
Also, the files being copied will be sent across a network.

Thank you for any and all help. It is much appreciated.

@echo off
setlocal

set source=C:\testC
set dest=D:\testD
set exts=*.txt

pushd "%source%"

for /f "delims=" %%G in ('dir /b /o:d %exts%') do (
move /-y "%%~G" "%dest%"
goto :break
)
:break
You provided me with my move function.

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

Re: Need help writing a batch file for copying files

#8 Post by foxidrive » 26 Aug 2016 18:47

domulation wrote:To be fair on my part, in my original post, it did say copy and not move. I stated this several times.
My mistake was having the move function in my original code to begin with.

That is exactly how I made my mistake.

I read your question and had taken in the main aspects of your task. I then quoted it and reused your code as I modified it, and it was your move command that changed the way I saw your task.

I apologise for making my point where it wasn't warranted.

There are different ways to approach using the copy command instead but they all depend on your system and network.

1) The files that are processed could be moved to a storage folder inside the same 'source' folder
2) A list could be maintained in a text file so the same file is not reused.
3) A list in a text file could be used in the beginning to consume each line and so process all the files.

Number 1) is the most reliable.
Number 2) depends on the filename structure of the files and if every single filename is unique, so that no filename clashes occur in the future.
Number 3) depends on the source files, if new ones are added now and then at random.

They all depend on the way the source files are archived and how they are replaced.

domulation
Posts: 9
Joined: 25 Aug 2016 23:52

Re: Need help writing a batch file for copying files

#9 Post by domulation » 27 Aug 2016 14:38

No need to apologize sir, the mistake was on my part.
I just appreciate your help.

As for option 1, Are you suggesting copying the files into a storage folder, so I have 2 copies, and then using the move script that was written?

Also, after all files have been moved or copied, how can i end the batch file but leave the prompt open showing that all files have been successfully transferred instead of it looping back to keep checking.

I have no problem using the move logic you have written as it seems easier in my situation.

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

Re: Need help writing a batch file for copying files

#10 Post by foxidrive » 28 Aug 2016 02:33

domulation wrote:As for option 1, Are you suggesting copying the files into a storage folder, so I have 2 copies, and then using the move script that was written?

No.
You have your files in C:\testC at present.

The script can create a folder in C:\testC\storage and then copy them as needed but move the original file into C:\testC\storage so it's been handled and finished with and will not be a nuisance in the script logic.
Also, after all files have been moved or copied, how can i end the batch file but leave the prompt open showing that all files have been successfully transferred instead of it looping back to keep checking.

If you are only processing a single load of files then it can finish when all files are done. In this case copying isn't a problem at all because a different for command can be used.

This kind of task often involves more files being added which is a continual process, and which I was catering for.

Would you prefer a log file of the filenames processed, maybe left on the desktop.

domulation
Posts: 9
Joined: 25 Aug 2016 23:52

Re: Need help writing a batch file for copying files

#11 Post by domulation » 29 Aug 2016 22:12

Hmmm alright. I can't say I fully understand what your saying but, the original script you have written works perfect.

I was trying to edit the code a little to have it pause once all the files are gone.
Do I edit this line? I have tried different variations of the Pause command, but can't seem to get it to work.
Do you have any advice on this?

if not defined file timeout 5 /nobreak & goto :nextfile

As always, any input is greatly appreciated.

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

Re: Need help writing a batch file for copying files

#12 Post by foxidrive » 30 Aug 2016 05:49

domulation wrote:Are you suggesting copying the files into a storage folder, so I have 2 copies, and then using the move script that was written?

The asnwer is Yes. I didn't follow your meaning at that time.

But because you are only doing a batch of files you can use this.

Code: Select all

@echo off
set "source=C:\testC"
set "dest=D:\testD"
set "exts=*.txt"
pushd "%source%"
for /f "delims=" %%a in ('dir /b /o:d %exts%') do (
  echo "%%a" & title "%%a"
  copy /b "%%a" "%dest%" >nul
  for /L %%b in (1,1,1000) do if exist "%dest%\%%a" timeout 60 /nobreak
)
pause

domulation
Posts: 9
Joined: 25 Aug 2016 23:52

Re: Need help writing a batch file for copying files

#13 Post by domulation » 30 Aug 2016 20:59

Thank you foxidrive,

The script works perfect and exactly what I needed.

Thank you for all your help and sharing your knowledge with me.

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

Re: Need help writing a batch file for copying files

#14 Post by foxidrive » 31 Aug 2016 14:10

domulation wrote:The script works perfect and exactly what I needed.

That's great, thanks for your feedback.

I made a typo in your code that makes no difference to the operation but I had intended to clean up the screen output.

In the code above I changed this "2>nul" to this ">nul" and it just hides some irrelevant screen information.


With the "2>nul" syntax it will hide any error messages instead of the trivial text.
The 2 refers to "standard error" to the console screen instead of "standard out".

Post Reply