Computing.Net > Forums > Windows 95/98 > Creating a batch file for backup

Creating a batch file for backup

Reply to Message Icon

Original Message
Name: machalel
Date: February 7, 2006 at 21:54:48 Pacific
Subject: Creating a batch file for backup
OS: Windows 98
CPU/Ram: Pentium
Comment:

Hi,

I want to create a small Batch file that can move some files from one directory to another.

I have a directory full of many different data files with the extensions *.001, *.002, *.003 etc, and I want to be able to move all files *except* the last 3 with the highest extensions somewhere else.

For example, I might have files foo.001, foo.002, foo.003, foo.004, baa.001, baa.002, baa.003, baa.004, baa.005, baa.006

In this case I would move foo.001, baa.001, baa.002, baa.003 to a backup directory and leave the others where they are.

Is this do-able using Dos Batch commands? or should I forget about it and move onto scripting it in C etc?

Thanks in advance


Report Offensive Message For Removal

Response Number 1
Name: Mechanix2Go
Date: February 7, 2006 at 22:52:57 Pacific
Subject: Creating a batch file for backup
Reply: (edit)

Hi,

Your criteria seem to contradict each other; no offense intended.

I THINK it's do-able in DOS, but will get somewhat convoluted.

Please clarify.


If at first you don't succeed, you're about average.

M2Go


Report Offensive Follow Up For Removal

Response Number 2
Name: Derek
Date: February 8, 2006 at 10:14:57 Pacific
Subject: Creating a batch file for backup
Reply: (edit)

Supposing your files are in c:\fred and you want them in c:\joe

@echo off
copy c:\fred\*.001 c:\joe
copy c:\fred\*.002 c:\joe
copy c:\fred\*.003 c:\joe
del c:\fred\*.*
cls

I think this does what you meant

DerekW


Report Offensive Follow Up For Removal

Response Number 3
Name: Derek
Date: February 8, 2006 at 10:32:55 Pacific
Subject: Creating a batch file for backup
Reply: (edit)

NO NO NO. I misread your question. Try this:

Suppose your files are in c:\fred and you want to keep .004 and above in this location (moving the
lower numbers to c:\backup).

@echo off
copy c:\fred\*.001 c:\backup
copy c:\fred\*.002 c:\backup
copy c:\fred\*.003 c:\backup
del c:\fred\*.001
del c:\fred\*.002
del c:\fred\*.003
cls

Hope I understood correctly this time. I think this could probably be simplified but I have to rush off right now. Back in a few hours.

DerekW



Report Offensive Follow Up For Removal

Response Number 4
Name: Mechanix2Go
Date: February 8, 2006 at 11:30:42 Pacific
Subject: Creating a batch file for backup
Reply: (edit)

Hi Derek,

I'm still waiting for more info.

BTW, a COPY & DEL is for most purposes, a MOVE.

So you could:
move c:\fred\*.001 c:\backup
...
and so on, avoiding the individual DELs.

Or, more simply:

for %%B in (001 002 003) do move *.%%B c:\backup


If at first you don't succeed, you're about average.

M2


Report Offensive Follow Up For Removal

Response Number 5
Name: Derek
Date: February 8, 2006 at 14:08:01 Pacific
Subject: Creating a batch file for backup
Reply: (edit)

Yep, Mechanix2Go, that "move" was in the back of my mind as the main simplification when I was dragged off, although I doubt I'm clever enough to have come up with the one-liner.

Back again now but, as you say, we need to be absolutely sure that the question has been understood before going any further.

DerekW (average LOL)



Report Offensive Follow Up For Removal


Response Number 6
Name: machalel
Date: February 8, 2006 at 15:43:49 Pacific
Subject: Creating a batch file for backup
Reply: (edit)

Hi, thanks for the replies! I'm sorry if I wasnt clear.

That is kind of what I want to do, except I need to do it for all of the files in the directory (there are a lot, so I can't hard code them all in), and secondly, each of the files will have a different number of extensions.

For example the "foo" files might have extensions 001 to 004, whereas "baa" has extensions 001 to 006.


Report Offensive Follow Up For Removal

Response Number 7
Name: Derek
Date: February 8, 2006 at 16:29:26 Pacific
Subject: Creating a batch file for backup
Reply: (edit)

That shouldn't matter.

The examples given will, as they stand, move ALL files with .001 .002 & .003 extensions into the backup folder, leaving everything else in it's original folder. No further coding is necessary because *. takes them all.

So, simplifying my #3, this is all you need to write:

@echo off
move c:\fred\*.001 c:\backup
move c:\fred\*.002 c:\backup
move c:\fred\*.003 c:\backup
cls

Or you can use the one-liner Mechanix2Go gave at the end of his post #4.

DerekW


Report Offensive Follow Up For Removal

Response Number 8
Name: Mechanix2Go
Date: February 8, 2006 at 16:36:17 Pacific
Subject: Creating a batch file for backup
Reply: (edit)

Hi,

Does it boil down to "move the three newest" or "move the three lowest numbered"?

And before we really get bogged down, what EXACT version of DOS?


If at first you don't succeed, you're about average.

M2


Report Offensive Follow Up For Removal

Response Number 9
Name: Derek
Date: February 8, 2006 at 16:37:05 Pacific
Subject: Creating a batch file for backup
Reply: (edit)

Heck, no, I get it now (I must take more water with it). I seem to have had word blindness regarding this bit:

"move all files *except* the last 3 with the highest extensions"

So, yeah, you might want to move out more than just .001 .002 & .003

I'll think about it. Maybe Mechanix2Go or a.n.other will see a way to do this.

DerekW


Report Offensive Follow Up For Removal

Response Number 10
Name: machalel
Date: February 8, 2006 at 16:43:23 Pacific
Subject: Creating a batch file for backup
Reply: (edit)

yeah, I need to move all *except* the three highest. So it may be .001 .002 & .003 for one file, but it could be more for another file.

The computer is currently running Windows 98, although maybe XP in the future (will that make a difference?)


Report Offensive Follow Up For Removal

Response Number 11
Name: Derek
Date: February 8, 2006 at 17:04:58 Pacific
Subject: Creating a batch file for backup
Reply: (edit)

Sorry, I got stuck in a loop trying to solve some simpler problem and your original question now seems quite clear. It would presumably need a file count routine. There might well be a batch file solution but it's beyond my knowledge.

Maybe someone will be able to sort this out on here, otherwise you could try the Programming forum.

DerekW


Report Offensive Follow Up For Removal

Response Number 12
Name: Mechanix2Go
Date: February 8, 2006 at 17:42:58 Pacific
Subject: Creating a batch file for backup
Reply: (edit)

There is a big difference between w98 and w2k/XP batch capability. [w2k is less clutterd and less a resource hog than XP]

Is there much consistency to the file names, like a dozen foo with different numbers in extensions; a bunch of baa.* and blah.*

Or are there bunches of different names?

Maybe you can paste in, lets say, 12 lines of a DIR so we can see what we've got to work with.



If at first you don't succeed, you're about average.

M2


Report Offensive Follow Up For Removal

Response Number 13
Name: machalel
Date: February 8, 2006 at 17:53:52 Pacific
Subject: Creating a batch file for backup
Reply: (edit)

names are like:
4LA-CMP.*
068APV.*
NDSSPD.*
PROFRD.*
087BBF.*
10QIK512.*

so as you can see there is not really much consistancy, and there are around 10,000 files in total :P


Report Offensive Follow Up For Removal

Response Number 14
Name: Mechanix2Go
Date: February 8, 2006 at 18:05:12 Pacific
Subject: Creating a batch file for backup
Reply: (edit)

I'll work on it.

Looks like an uphill battle.


If at first you don't succeed, you're about average.

M2


Report Offensive Follow Up For Removal

Response Number 15
Name: jackfrost5556
Date: February 10, 2006 at 11:44:22 Pacific
Subject: Creating a batch file for backup
Reply: (edit)

I think your going at this backwords. If you only need to save 3 files in the original directory.. This copy, with appropriate substitutions will work fine in a batch file.
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
echo off
move C:\windows\desktop\old\*.* C:\windows\desktop\new
move C:\windows\desktop\new\able.txt C:\windows\desktop\old
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Need a second batch file to re-organize..

Just trying


Report Offensive Follow Up For Removal

Response Number 16
Name: Derek
Date: February 10, 2006 at 15:39:59 Pacific
Subject: Creating a batch file for backup
Reply: (edit)

jackfrost5556

Me again - watching.

See what the poster says but I don't think he will be in a position to predict the complete names of the files to move back.

As the number of files will vary and he only wants the three with the highest extension numbers, he could be presented with wanting to retain a set like this (and many other possibilities):

tom.004
tom.005
tom.006

dick.007
dick.008
dick.009

harry.002
harry.003
harry.004

Wish I knew the answer - or maybe I've lost the plot again.

DerekW


Report Offensive Follow Up For Removal

Response Number 17
Name: jackfrost5556
Date: February 10, 2006 at 17:47:51 Pacific
Subject: Creating a batch file for backup
Reply: (edit)

Hi Derek,
Like I said, he'll need a second batch file, to recover to his original configeration.
(Second batch file)
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
echo off
move C:\windows\desktop\new\*.* C:\windows\desktop\old
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I'm using my own path, and only listed one file in my first post. He would have to copy all three files.
As I remember the directory(folder) name cannot contain spaces and be 8 characters or less. If need be, he could rename his folders as needed.

Just trying


Report Offensive Follow Up For Removal

Response Number 18
Name: Mechanix2Go
Date: February 11, 2006 at 04:38:27 Pacific
Subject: Creating a batch file for backup
Reply: (edit)

Time to make coffee.


If at first you don't succeed, you're about average.

M2


Report Offensive Follow Up For Removal

Response Number 19
Name: Derek
Date: February 11, 2006 at 14:51:04 Pacific
Subject: Creating a batch file for backup
Reply: (edit)

jackfrost5556

Maybe I'll never get this...

In #15 you moved everything in the "old" folder to the "new" folder.

You then selected able.txt to move back to the "old" folder. I accept that you only used one file in this example.

In #17 you moved what remained in the "new" folder back into the "old" folder.

Doesn't this mean you have to make a "manual selection" (able.txt in your example) in order to decide which files are put back into the "old" folder? Each time I run through this lot I end up with everything back in the "old" folder again.

Sure as heck I'm missing something quite basic here. Tried coffee, didn't help.

DerekW


Report Offensive Follow Up For Removal

Response Number 20
Name: jackfrost5556
Date: February 12, 2006 at 12:45:25 Pacific
Subject: Creating a batch file for backup
Reply: (edit)

Hi Derek,
I assumed he would want to move all of his files to a new folder, except for the last three. As well, I assumed he would, at some time in the future want to reassemble all of the files (modified or not) back into the original folder.
Let's assume there are 100 files. Using a batch file, I moved all 100 files to the new folder, then in the second part of the batch file I returned the 3 he wanted to remain in the original folder.
So the end result is 3 files in the original folder, with the balance in the new folder. That's what he wanted.
To return the configeration to original, you would need a second batch file.
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
echo off
move C:\windows\desktop\new\*.* C:\windows\desktop\original
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
That would return the 97 files back to the original folder. adding them to the 3 already there. That would make him whole again.
As I said, I'm using my own path, and file names. They would have to be changed as needed.
Jack Hall

Just trying


Report Offensive Follow Up For Removal

Response Number 21
Name: Derek
Date: February 12, 2006 at 13:31:52 Pacific
Subject: Creating a batch file for backup
Reply: (edit)

jackfrost5556

Fine but perhaps the confusion is due to my assumption that machalel would want a completely automatic process and never have to change the contents of any batch file(s) once written.

As those returned would be different each time then presumably they would have to be chosen, then manually inserted into the batch file. If this is the case then machalel could just as easily use cut and paste and not bother with batch files at all.

DerekW


Report Offensive Follow Up For Removal

Response Number 22
Name: machalel
Date: February 12, 2006 at 15:20:58 Pacific
Subject: Creating a batch file for backup
Reply: (edit)

Hi, thanks for the help guys, its much appreciated! :)

I'm really looking at totaly automating the program without having to enter any definite filenames. This would be very tedious and time consuming, as there are around 15,000 files!

I'm not sure if the files are to be "merged" again at a later date (i'm just writing the code, not using it! ^_^) but most probably they'll either be purged in a year or so, or combined with the "recent" files and put on a CD as a physical backup later on. Either way, it's just a matter of "drag-and-drop" using windows.

Derek's post about the variety of files(post 16) is correct, I can't really predict the names of the files, and out of the 15,000 files there are probably around 2,000 that will stay in the current directory, and 13,000 will be moved to the backup directory (This is a guess).

I'm beginning to think this will be easier to do if I write a small C program! >_<


Report Offensive Follow Up For Removal

Response Number 23
Name: Derek
Date: February 12, 2006 at 15:35:40 Pacific
Subject: Creating a batch file for backup
Reply: (edit)

Yeah, although there might be some obscure way of doing this with a batch file (maybe using loops to count)
I think you are right to consider a small C program.

DerekW


Report Offensive Follow Up For Removal

Response Number 24
Name: Mechanix2Go
Date: February 13, 2006 at 03:55:39 Pacific
Subject: Creating a batch file for backup
Reply: (edit)

Yes, yes & yes.

If it can be done in bat, I think it will come down to:

[1] Finding and grouping files with identical names of any extension.

[2] Acting on each "group".

This will be tortured enough if there are no LFNs involved.


If at first you don't succeed, you're about average.

M2


Report Offensive Follow Up For Removal

Response Number 25
Name: jackfrost5556
Date: February 16, 2006 at 11:00:18 Pacific
Subject: Creating a batch file for backup
Reply: (edit)

Did a little playing with this thing. I'm not totally sure I understand the original question, but if the intent is to retain 3 known file extensions, in the original folder, and send the rest to a backup folder, this will work.
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

echo off
move C:\windows\desktop\old\*.* C:\windows\desktop\new
move C:\windows\desktop\new\*.*sxw C:\windows\desktop\old
move C:\windows\desktop\new\*.*sxc C:\windows\desktop\old
move C:\windows\desktop\new\*.*sxi C:\windows\desktop\old
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
In this case, all the .txt, .doc, .exe and so on would be transferred to the backup folder. All of the open office files using the extensions shown would be returned to the original folder.
I'm done.
Jack Hall

Just trying


Report Offensive Follow Up For Removal

Response Number 26
Name: Derek
Date: February 16, 2006 at 12:22:19 Pacific
Subject: Creating a batch file for backup
Reply: (edit)

jackfrost5556

You need a medal for trying. Earlier on I had problems understanding the question too. I think it goes like this.

Different file names will appear in a folder but all of them will have extensions that look like numbers (.001 .002 .003 etc).

The poster might get confronted with something like this:

tom.001
tom.002
tom.003
tom.004
tom.005
tom.006

dick.001
dick.002
dick.003
dick.004
dick.005
dick.006
dick.007
dick.008
dick.009

harry.001
harry.002
harry.003
harry.004

The requirement is to retain the files with the highest three extension numbers, that is:

tom.004
tom.005
tom.006

dick.007
dick.008
dick.009

harry.002
harry.003
harry.004

All the rest should go to the backup folder. Somehow I don't think we are out of the wood yet.

I guess I'm done too.

DerekW



Report Offensive Follow Up For Removal

Response Number 27
Name: jackfrost5556
Date: February 16, 2006 at 18:39:58 Pacific
Subject: Creating a batch file for backup
Reply: (edit)

Cool Derek. Made my day. Let's just hope he posts the script for the "C" program he's compiling.

Just trying


Report Offensive Follow Up For Removal

Response Number 28
Name: Derek
Date: February 16, 2006 at 19:18:55 Pacific
Subject: Creating a batch file for backup
Reply: (edit)

Well, I do hope he lets us know the outcome.

Post is OK for at least 2 more weeks (athough that
might not be enough).

CU

DerekW



Report Offensive Follow Up For Removal

Response Number 29
Name: machalel
Date: March 1, 2006 at 22:10:35 Pacific
Subject: Creating a batch file for backup
Reply: (edit)

Hi everyone,

I started programing this is C, when I found a language called "AutoHotKey" which contains a few functions that make life easier.

It works perfectly when testing it on my computer, but I'm having issues when I actually try to use it for real and backup all the required files. I think that this is because it has been compiled on Windows XP and is trying to be run on Windows 98. In the future, when I have time, I'm going to try compiling it on the same machine it is to be used on.

Thanks for all the help! :D

(http://www.autohotkey.com/forum/viewtopic.php?p=47592)


Report Offensive Follow Up For Removal

Response Number 30
Name: machalel
Date: March 1, 2006 at 22:11:53 Pacific
Subject: Creating a batch file for backup
Reply: (edit)

its not C code, but i'll post it anyway :)

[code]

MsgBox, 4, , Do you want to start the backup process?
IfMsgBox, No
return
; Otherwise, the user picked yes.

moveDir = c:\tempmove
moveCounter = 0
Loop,*.*,0,0
{
thisFile = %A_LoopFileName%
StringTrimRight,thisFile,thisFile,4
thisExt = %A_LoopFileExt%
nextExt = %thisExt%
nextExt ++
just = 00%nextExt%
StringRight,nextExt,just,3
nextFile = %thisFile%.%nextExt%
Loop
{
IfExist,%nextFile%
{
nextExt ++
just = 00%nextExt%
StringRight,nextExt,just,3
nextFile = %thisFile%.%nextExt%
}
IfNotExist,%nextFile%
{
break
}
}

tempExt = %thisExt%
tempExt += 3
just = 00%tempExt%
StringRight,tempExt,just,3
if (nextExt > tempExt)
{
moveExt = %nextExt%
moveExt -= 4
Loop
{
just = 00%moveExt%
StringRight,moveExt,just,3
moveFile = %thisFile%.%moveExt%
FileMove,%moveFile%,%moveDir%
moveCounter ++
if (moveExt = thisExt)
{
break
} else {
moveExt --
}
}
}
}
MsgBox, %moveCounter% files were transfered to %moveDir%
return

[/code]


Report Offensive Follow Up For Removal

Response Number 31
Name: machalel
Date: March 1, 2006 at 22:13:57 Pacific
Subject: Creating a batch file for backup
Reply: (edit)

hmm... sorry it's not very readable, obviously I can use indenting here. If you follow the link I posted before it is easier to read :)


Report Offensive Follow Up For Removal

Response Number 32
Name: Mechanix2Go
Date: March 2, 2006 at 00:11:43 Pacific
Subject: Creating a batch file for backup
Reply: (edit)

It's not c; what is it?


If at first you don't succeed, you're about average.

M2


Report Offensive Follow Up For Removal

Response Number 33
Name: Derek
Date: March 2, 2006 at 03:24:06 Pacific
Subject: Creating a batch file for backup
Reply: (edit)

Thx for taking the trouble to give us the feedback - should be useful.

DerekW


Report Offensive Follow Up For Removal

Response Number 34
Name: machalel
Date: March 8, 2006 at 20:38:41 Pacific
Subject: Creating a batch file for backup
Reply: (edit)

no it's not C, its a language called AutoHotKey

Its used to automate keyboard and mouse inputs, but it can be used for other things obviously.

I wrote it in this language, mainly for the easy file iteration loops and move commands

:)


Report Offensive Follow Up For Removal

Response Number 35
Name: machalel
Date: March 8, 2006 at 20:39:13 Pacific
Subject: Creating a batch file for backup
Reply: (edit)

www.autohotkey.com


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Creating a batch file for backup

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software