[SOLVED] Script to older version of files when new ones are copied in?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
klick
Posts: 4
Joined: 08 Feb 2020 02:31

[SOLVED] Script to older version of files when new ones are copied in?

#1 Post by klick » 08 Feb 2020 08:44

Hi ppl,

Need your help. Assuming I have a folder that should ONLY contain the latest version of any work files, can I have a script that will automatically move all older versions to a backup folder?
eg. I have a folder called "Current" and another folder called "Backup".
Let's say there are 3 files in "Current". They are:
HouseMap[1].jpg
FieldMap[3].jpg
SeaMap[1].jpg

Assuming I have a new version of HouseMap and I named it HouseMap[2].jpg
Once I copied it into the "Current" folder, I want to script to know that HouseMap[2].jpg is the latest and it automatically moves HouseMap[1].jpg into the "Backup" folder.
(Newer files should have a bigger number appended at the end of the filename)
"Backup" folder acts like a dumping ground of ALL old versions.
Is this possible?

Thank you for your time. Any info and help is greatly appreciated.

Klick.
Last edited by klick on 09 Feb 2020 00:02, edited 1 time in total.

penpen
Expert
Posts: 1997
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Script to older version of files when new ones are copied in?

#2 Post by penpen » 08 Feb 2020 15:18

You could move all existing versions of that files to the backup folder (matching "HouseMap[*].jpg" ) before you copy your new version of that file to that folder.
Assuming the following "next.bat" batch file is located in the parent directory of both of your named directories "Current" and "Backup", and
assumed your given example reflects the format of all your files, you could drag and drop new files onto that batch.

"next.bat" (untested):

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
if "%~1" == "" goto :eof

for /f "tokens=1-3 delims=[]" %%a in ("%~nx1") do move "Current\%%~a[*]%%~c" "Backup"
goto :eof
penpen

pieh-ejdsch
Posts: 239
Joined: 04 Mar 2014 11:14
Location: germany

Re: Script to older version of files when new ones are copied in?

#3 Post by pieh-ejdsch » 08 Feb 2020 15:43

There are already some useful scripts here. There is sure to be more.

File Versioning with Timestamp:viewtopic.php?t=7903#p52948

Make Backup cycle:
viewtopic.php?t=8956#p58728

Eureka!
Posts: 136
Joined: 25 Jul 2019 18:25

Re: Script to older version of files when new ones are copied in?

#4 Post by Eureka! » 08 Feb 2020 17:38

When the maximum version is 9, like FieldMap[9].jpg, you can make use of sorting filenames.
( Explorer and CMD sort files different. Explorer uses natural sort/logic sort (1,2,3,11,12), whereas CMD uses ascii sort (1,11,12,2,3) That is why the max is 9 for this)

In code:

Code: Select all

@echo off
md Backup
for /f "tokens=1,3 usebackq delims=[]" %%x in (`dir /b /o:-n *[*].jpg`) do for /f "usebackq skip=1 delims=" %%f in (`dir /b /o:-n "%%x[*]%%y" `) Do move "%%f" Backup


When there are more than 9 versions possible (like FieldMap[19].jpg), this should do it:
(Basic concept)

Code: Select all

echo off
md Backup
for /f "tokens=1,2,3 usebackq delims=[]" %%x in (`dir /b /o:-n *[*].jpg`) do ^
for /f "tokens=1,2,3 usebackq delims=[]" %%f in (`dir /b /o:-n "%%x[*]%%z"`) do ^
if %%g LSS %%y move "%%f[%%g]%%h" Backup
Or if you prefer it on 1 line:

Code: Select all

echo off
md Backup
for /f "tokens=1,2,3 usebackq delims=[]" %%x in (`dir /b /o:-n *[*].jpg`) do for /f "tokens=1,2,3 usebackq delims=[]" %%f in (`dir /b /o:-n "%%x[*]%%z"`) do if %%g LSS %%y move "%%f[%%g]%%h" Backup
(Try it first on a copy of these files)

klick
Posts: 4
Joined: 08 Feb 2020 02:31

Re: Script to older version of files when new ones are copied in?

#5 Post by klick » 08 Feb 2020 23:49

Hi ppl,

Thank you for all your replies. I will check & test them out. Will post questions if I hit anything I can't understand.
The main reason for the script is because this is constantly needed as many people will be submitting their work into this folder and changes are happening rapidly. We are looking for an easy way to introduce some sanity into this process and to ensure ppl's work don't accidentally gets overwritten and whatnot. Plus, we don't have ANY tech people in the company. *sweat drop* So erm.. hahahh.... brushing up what little I have learnt during my student days on DOS/batch script. :P

Thank you for your time.

Klick.

klick
Posts: 4
Joined: 08 Feb 2020 02:31

Re: Script to older version of files when new ones are copied in?

#6 Post by klick » 09 Feb 2020 00:01

Dear Eureka!

You are da MAN!!! Thank you sooooo much!
It was exactly what we needed! I changed the jpg to a wildcard as there are also diff types of files and they needed to ensure the filename stayed the same so they could easily reference back old versions in case of future issues.
Thank again! You are a life saver! *bounces around happily*

I want to thank others also for taking their time to give help info. :)

Klick.

klick
Posts: 4
Joined: 08 Feb 2020 02:31

Re: [SOLVED] Script to older version of files when new ones are copied in?

#7 Post by klick » 09 Feb 2020 00:07

Sorry ppl. I am noob. Posted some earlier replies to explained how the problem was solved by Eureka!'s solution and changed the topic to [SOLVED] but couldn't see the post I posted nor the post marked as Answered. *blush* any help is appreciated to teach me how to properly mark a topic as answered. Thanks again.

In case my previous replies were somehow lost. I want to thank you all for your help. You guys are great!

ShadowThief
Expert
Posts: 1163
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: [SOLVED] Script to older version of files when new ones are copied in?

#8 Post by ShadowThief » 09 Feb 2020 18:17

klick wrote:
09 Feb 2020 00:07
Sorry ppl. I am noob. Posted some earlier replies to explained how the problem was solved by Eureka!'s solution and changed the topic to [SOLVED] but couldn't see the post I posted nor the post marked as Answered. *blush* any help is appreciated to teach me how to properly mark a topic as answered. Thanks again.

In case my previous replies were somehow lost. I want to thank you all for your help. You guys are great!
We're just a bunch of batch enthusiasts; this isn't really a Q&A forum so there's nothing like marking things as "solved."

Post Reply