-3

I need to create a batch file that runs a daily backup of 10 files on to our server. To provide a brief background, we use a payment system that spits these files out each day, we'd like to ensure these are uploaded on to the server daily however, the filenames for these files will change each day meaning that an overwrite might not work. I can confidently create a batch file to do the copy and overwrite however, I'm unsure how to overwrite these with a new batch of 10 files with different filenames.

Are you able to assist?

Kind regards,

Ad

Admaine
  • 43
  • 8
  • 1
    Maybe. Show us your code by editing it into your post. – Magoo Jun 21 '17 at 08:29
  • This is the basic code that I was working with however, I came to a standstill after realising that the files (with ever changing filenames) needed overwriting each evening: xcopy C:\Users\Username\Desktop\Test* R:\Test* /Y /Q /S – Admaine Jun 21 '17 at 08:46
  • 1
    @Admaine Correct me if I misunderstood, but could you not simply delete the old backup and then copy the files? If not, do the names depend on anything/are they in any way predictable? – Regejok Jun 21 '17 at 08:55
  • Hi, The filenames only include the date at the start of them, this is then followed by a series of numbers (for example 2106201723534334.bak Thanks – Admaine Jun 21 '17 at 09:28
  • You can get the current date and subtract N days from it. I just don't quite understand which files exactly are to be deleted. Are we talking about >10 backups of 'projects', and each day 10 of >10 projects are backed up again? Or are there 10 backups of the same project/thing in the backup folder at all times? – Regejok Jun 21 '17 at 12:03
  • Yes, so within this folder there are always 10 backups, the oldest is deleted at midnight and the rest are pushed back a day to make way for the most current days backup file. So using my example before, the 14th June would disappear making the 15th the oldest file and the 22nd the newest with a filename being something along the lines of 2206201712381319.bak. The newest file needs a script to copy it on to our server. Hope this helps to explain it. – Admaine Jun 21 '17 at 13:55
  • Please include your code into the question, properly [formatted](https://stackoverflow.com/help/formatting), rather than in comments where it's hardly readable! – aschipfl Jun 21 '17 at 16:09

1 Answers1

0
:: this deletes 10 files named "file1.txt" to "file10.txt"
for /L %%a in (1,1,10) do del R:\Test\file%%a.txt
:: this takes ALL of the files matching the mask and copies them to
:: "file*.txt" at the destination
for /f "tokens=1*delims=: " %%a in (
 'dir /b /a-d "C:\Users\Username\Desktop\Test*" ^|findstr /n /r "." ') do (
 echo xcopy "C:\Users\Username\Desktop\%%b" "R:\test\file%%a.txt"
)

The first command should be obvious.

The second simply lists the source directory, names only, no directorynames of files matching "test*" and directs that output to findstr which /n numbers each line matching the /r regular expression "." (which will be all.)

The findstr output will be of the form

1: test one.xyz

for will tokenise the result, %%a receiving the part before the delimiter-sequence and %%b the part after. The delimiters are set to : and Space; findstr counts each as a delimiter, and a string of delimiters as one.

The result is echoed to ensure that the proposed action is reported, not executed so that any error may be detected before any damage occurs.

The activate the copy, simply remove the echo keyword.

Note that the files remain in the source as you've not given any clues about how they'll be further processed. If the files are left in place, they'll be copied on the next run as well - the for loop will just keep counting...


for /f "tokens=1*delims=: " %%a in (
 'dir /b /a-d /o-d "C:\Users\Username\Desktop\Test*" ^|findstr /n /r "." '
) do if %%s leq 10 (
 echo copy "C:\Users\Username\Desktop\%%b" "R:\test\"
)

will [echo the commands to] copy the latest 10 files from the source directory to the destination.

It should be obvious how to change 10 to some other value.

Manipulating the actual directory names and maintaining the source and destination directories - your affair.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Hi, Sorry I should have been clearer. So the backup files automatically deletes the oldest at the end of each day, so to make it easy to understand if the backups spat out a file from the 14th to the 21st of June then at the end of the day the 14th would be removed. In theory I just need the most recent file backed up however I was unsure how to do so with the filenames never being the same. I hope this helps :) – Admaine Jun 21 '17 at 09:27
  • @Admaine If you only need the most recent files, you might want to take a look at `forfiles`. Example how to delete all files older than 10 days: `forfiles -p "C:\backups" /D -10 /C "cmd /c del @path"` – Regejok Jun 21 '17 at 11:57
  • If you simply want the most recent `n` files, then add `/o-d` to the `dir` command which will then provide the list in reverse-date order and if you want to restrict to `n` files to be transferred, add the condition `if %%a leq n`before the `copy` so that the copy only takes place if the sequence number is less than or equal to the nominated quantity. You then probably don't need the alteration to the filename, so you can simply remove the `filename%%a.txt` from the destination filename, leaving just the directoryname. – Magoo Jun 21 '17 at 13:40
  • Hi, this might have some legs in it as it's not referencing the filename. In the example you've provided are we simply saying delete any file in C:\backups that's older than 10 days? Do I then need to add another line to copy the newest to the server? – Admaine Jun 21 '17 at 13:58
  • Hi Magoo, apologies for coming across a bit dense, are you able to structure that for me so I can try to understand it a bit better? I'm a bit of a novice when it comes to scripting. Thanks in advance. – Admaine Jun 21 '17 at 14:33