We use cookies to provide you with a better experience. If you continue to use this site, we'll assume you're happy with this. Alternatively, click here to find out how to manage these cookies

hide cookie message
Contact Forum Editor

Send an email to our Forum Editor:


PLEASE NOTE: Your name is used only to let the Forum Editor know who sent the message. Both your name and email address will not be used for any other purpose.

Tech Helproom


It's free to register, to post a question or to start / join a discussion


 

Another batch file problem.


Housten

Likes # 0

Good Afternoon [ !!! ],

I asked about what I thought was something easy, but after googling various things I am, now, more confused than ever! So I have revised what I want to do, and have realized – or think I have – that the simplest way would be to enter a number, right at the start of the file. And I must emphasize that – it will always be a number. This number will then be used to create a new sub-directory/sub-folder, which will then be written to. So what I want is something like:

Echo “What is number you wish to use”: Input %number% MD “G:\Mine\Backup\%number% [ or should this be %%number%%? ] ECHO OFF Xcopy “C:\bank data” “G:\Mine\Backup\%number%\bank data” and then I would use various parameters like ‘/s /e /c /h /y /v’ which I think will cover [ or more probably, more than cover ] all the options I will ever need, but I am going for the fullest amount so that once done, everything is covered and all bases are as well. As well as doing this folder and get it running properly, I will [ later ] be able to add other folders, and be able to save them where and when I want.

It is just getting the start correct that is proving really annoying, so if someone can tell me what to write I would be a very grateful pensioner.

Many thanks in anticipation.

Like this post
Housten

Likes # 0

robinofloxley,

Thanks for taking an interest, and you make a very good point that I hadn't thought about. I think I may well adapt your idea, it would make things a lot simpler in many, many ways.

Many thanks for your suggestion!

lotvic,

I am still plugging away at writing this backup batch file. I will give you the occasional update, to keep you abreast of my (non)progress!!!

Many thanks for your suggestions, any more would be welcome!

Like this post
Eric10

Likes # 0

Sorry for coming late to the party but I don't check the forums as much as I once did.

Since you are familiar with using a command prompt you could try something like the batch file below. You would run it from a command prompt by typing the batch file name followed by a space then the number you want to allocate to the folder you are creating. eg. makedir.bat 29

The number you supply is automatically stored in the %1 argument and is used in the appropriate places within the batch file.

There are a couple of simple safeguards built in to prevent overwriting an existing folder (message2) and to warn against omitting the folder number when launching the batch file (message1). I hope that some of it might be useful.

@echo off

cls

if "%1" == "" goto message1

if exist G:\Mine\Backup\%1 goto message2

MD G:\Mine\Backup\%1

DIR G:\Mine\Backup /B

xcopy "C:\Bank Data*.*" G:\Mine\Backup\%1 /E

echo.

echo Press any key to exit

pause > nul

goto end

:message2

echo.

echo Folder already exists, see list below

DIR G:\Mine\Backup /B

echo Press any key to close command prompt and try again

pause > nul

goto end

:message1

echo.

echo SYNTAX ERROR

echo.

echo usage is: makedir number

echo.

echo Press any key to close command prompt and try again

pause > nul

:end

exit

Like this post
lotvic

Likes # 0

Eric10, It's all new stuff to me, but on third read through I started to understand :) wish I had more time to study batch files. or not, I'd probably just get myself in a tangle :p

Like this post
Housten

Likes # 0

Eric10,

I have just read your posting, and it would seem you know about batch files, and having changed my mind about what I wish to do, perhaps you can help me!

What I realised is that the '/d' command - in effect - gives the same as a diffential backup. Where I was making a mistake was that I was making a backup a the start of every month and then new backups on different days. However as the 'destination' file was new the '/d' would not have any effect. so what I thought was that if I called the backup file 'Year Month' - say '2012 08' - then the '/d' would be very useful as only those new or changed files would be recorded. Then I realised that deleting old backups was a problem and would have to be done manually, but I then realised that if instead of calling the file '2012 08' I called it '\1\2012 08' then the number would be a control and it would be possible to delete backups older than - say - 6 months by doing it when you create a new backup file which would have '1' added to it, and after the creation have something like 'If number is 6 then let number1=number-6, RD '\number1*.*', which I am hoping will delete the backup. The number '6' could be changed to any number that I feel I need, which will be different for different backups and copies.

Now my problem is how to record the number, because I wish to have several different backups, and as these will all be on different individual numbers I need a way of writing the numbers to a file, at the end of every completed running of the batch file and then getting the numbers from this file at the start of every running of the batch file. Having only realised this yesterday afternoon, I have not yet started looking at how this could be accomplished. I am certain I have seen a method somewhere, sometime but being a lazy brain dead pensioner if you know of a way for me to do this I would be very grateful. I am going to start testing what I have written this afternoon, and once I have it working correctly, I will add all my options to it and test them. This will not - I suspect, unfortuneately - be a quick process, but if you could let me have the code for this file, it could be added very easily to my file.

Sorry to have gone on for so long, but I thought you should have the full details of what I would like to achieve. Many, many thanks for your help so far and in anticipation of what is to come.

Like this post
Housten

Likes # 0

Good Afternoon [ or something!!! ]

OK, so now I am getting annoyed, because I do not know what I am doing wrong, so can someone please tell me, what - and where - I am mucking this up?

I decided to write the first part of the file and then when I got that correct, it would be just a case of copying it the right number of times, and then doing some editing, and BINGO!! it would be there. But I had a problem so I have got one itsy-bitsy teensy bit of it, on its own, and it isn't working!!. What I have is

@ECHO OFF echo %date%

SET foldermonth="%date:~6,4%-%date:~3,2%

echo %foldermonth%

set folder=W:\RON Backup\C\%foldermonth%

echo W:\RON Backup\C\%foldermonth%

md folder

XCOPY /M /D /S /E /V /C /Q /G /H /J "C:\LaCie*.*" "folder"

ECHO Elvis HAAS left the building!!!echo %date%

I print the date to see what it is, and tried to work out what I wanted – I seem to have failed at even this simple instruction!

foldermonth is the year and month, it is supposed to be 2012-07, but is coming out as "2012-07. I do not know if this is the problem or not!!

But what is happening – and this is the second folder from ‘C:’ that I have tried, and I am getting told that files have been copied. Well and good, BUT… W:\RON Backup\C\ is empty. The files – so it says – have been copied but I can not find them. Admittedly I using ‘Windows Explorer’, but it only finds one or two, not the 86 it says were copied!!!

Obviously the ‘W:\Ron Backup\C’ folder exists but %foldermonth% is not being created. I do not see why not, and I find this quite worrying, as I thought this would be one of the ‘Easy Peasy’ things to have got done!

I thought the 'xcopy' command was OK, but now I am not so sure.

The '/c' of the command means 'carry on, even if you get an error, and so what I get is a parse error, and then Elvis HAAS left the building!!! Which is just to let me know that it is finished.

So can anyone tell me where I am wrong, please?

Many thanks in anticipation.

Like this post
Eric10

Likes # 0

Housten

First off I don't want you to think that I am an actual expert but I do have a little experience with batch files.

Secondly, I'm a pensioner like yourself (72) but got my first PC 20 years ago and it ran DOS 5 and Windows 3.1 so that's where the experience has come from but some of it is a little rusty now.

I thought I could see an error in your program on the XCOPY line. You have C:\LaCie. but now I think that it's the forum that has omitted a back slash after LaCie and it is probably there in your original code.

SET foldermonth="%date:~6,4%-%date:~3,2% is outputting "2012-07 because you have included the double quotes just after SET foldermonth= and this could cause a problem since you are using it in subsequent instructions.

The space in W:\RON Backup\C\%foldermonth% is causing a problem and you would be better advised to replace any spaces in the folder names with the underscore character (_) or even a hyphen (-).

You should also be using MD %folder% rather than just MD folder but that may just be a typo when posting on the forum. And finally I would guess that you may find your files in a folder called W:\Backup\C\2012-07.

Like this post
Eric10

Likes # 0

Housten

Just a couple of points that have come to mind.

Regarding your xcopy switches: you don't need to use both /S and /E. If you need to copy subfolders whether they are empty or not then use /E but if you want to leave empty subfolders behind then use /S. You have used /D but haven't specified a date /D:m-d-y.

You mentioned files not being copied so it's worth reminding you that when using the /M switch and doing repeated tests, files that have been copied will have the archive attribute unset on the source files and will not copy again until the attribute has been reset. This can be achieved by changing the content of the source files and resaving them or by selecting them in Explorer and ticking the box in Properties, Advanced. This is a simple point but can catch you out in the confusion of testing and troubleshooting.

On the same xcopy line you should also be specifying the destination as %folder% rather than just folder.

Like this post
Housten

Likes # 0

Eric10,

Many thanks for bo0th your emails. I too came via your way, buying my first computer - a Dragon 32 - in 1983. I wrote quite a lot of batch files finding them of great use! But it has been some years since I had any dealings with them, and with my approaching dementia/alzheimers not helping, I have, and still am, desperately trying to remember what I did!!

One thing your posts infer - at least I hope that they infer - is that batch files only work in the DOS command window. It only struck me a few minutes ago, but I was assailed by a sudden doubt, but your posts I hope are confirming that what I have been doing is correct. If you think that this is wrong I would be grateful if you could let me know.

Many thanks in anticipation.

Like this post
lotvic

Likes # 0

Maybe you should try/be using Powershell. Introduction to PowerShell and How to Open Windows PowerShell in Windows 7 and guide to getting started with Windows PowerShell. Read through these pages to get familiar with Windows PowerShell, and soon you’ll be driving around like a pro. (it says)

Like this post
Eric10

Likes # 0

You can run a batch file from a shortcut and it will open its own DOS box but it then looses the flexibility of being able to pass different arguments to it and you may need to end with PAUSE so that the box doesn't close before you have the chance to read the output. You can also run it from "Start, Run" in XP or "Start, Search" in Win 7 with the same caviats for keeping the DOS box open after it has run. Your idea of having a shortcut to the command prompt in Quick Launch or on the Taskbar is a good compromise.

Like this post

Reply to this topic

This thread has been locked.



IDG UK Sites

Best Black Friday 2014 tech deals: Get bargains on smartphones, tablets, laptops and more

IDG UK Sites

Tomorrow's World today (or next year)

IDG UK Sites

See how Trunk's animated ad helped Ade Edmondson plug The Car Buying Service

IDG UK Sites

Yosemite tips: Complete Guide to OS X Yosemite