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 two batch file problems


Housten

Likes # 0

Good afternoon,

[ Please note that I have followed Eric10's advice and added extra line endings so that the file looks something like the way it should. The forum changes the layout, so you must be very careful when entering programmes such as this!! ]

Now it is obvious I am doing something stupid, but for the life of me I can not figure it out.

I would, therefore, be very, very pleased if someone could tell this brain dead pensioner exactly why he is mucking things up. What I am trying to do is to write a batch file to make – well I was calling it ‘backups’, but I have been thinking about this and I realise they are only copies – a copy of what I want. Now I have got some of my file written out and working, but NOT these two bits, and they are quite important!!

The first problem is that I want a choice of what I do – I know, a stupid bigheaded thing – but what happens with the choice is that it first of all prints the folder it is in, then ‘echo’ and then the choice, and then, on the next line just prints the choice, which is all I want. So I would like to know what instruction to give to avoid the first of these lines completely, and just have the choice listed.

The second line is almost as confusing. I thought that ‘xcopy’ could have as many parameters as one would wish to use with it. Perhaps I am being too clever, in that I am trying to cover all the bases, but it keeps repeating ‘Invalid number of parameters’ and so copies nothing. Does anyone know what are the wrong/incorrect parameters that I am using?

The programme I am using is:-

“:MENU

echo Pick one of the following options:

echo

echo

cls

echo This is for daily backup/copy of some files Press 1 and then 'Enter'

echo This is for the main monthly backup/copy Press 2 and then 'Enter'

echo This is for weekly backup/copy Press 3 and then 'Enter'

echo To EXIT/Finish Press 4 and then 'Enter'

echo off

:choice

Echo Hit a number [1-4] and press enter.

echo off

set /P CH=[1-4]

if "%CH%"==1 goto alpha

if "%CH%"=="2" goto beta

if "%CH%"=="3" goto gamma

if "%CH%"=="4" goto detta

:alpha

echo you chose 1

echo off

xcopy /D /C /H /I /K /Y /M /S /E /V /Q “C:\quicken data*.*” “W:\%FolderMonth%\quicken data”

goto choice

:beta

echo you chose 2

echo off

goto choice

:gamma

echo you chose 3

echo off

goto choice

:detta

echo you chose 4

echo off”

Many thanks for any and all comments/information/advice that is given.

Like this post
Eric10

Likes # 0

Hi Housten

For the 'Invalid number of parameters' problem. It would appear that your code has been copied into a word processor at some stage and the double quotes enclosing the source and destination have been changed to 'smart quotes' which xcopy is treating as separate parameters rather than just quotes. The easiest way to correct this is to open your batch file in Notepad, delete the double quotes and retype them, and then save the file again. The backslash between 'quicken data' and '*.*' is missing but I'm sure that is down to the forum and it is there in your original code.

I've tidied up your file so that the menu doesn't keep repeating down the command box and have provided a trap so that incorrect numbers are rejected. Because of this it is important to keep the 'IF' tests in the order that I have put them so that '1' is accepted as the final choice. If you prefer to keep cycling round the menu after a valid choice then just replace 'goto EOF' with 'goto choice' in alpha, beta, and gamma. EOF is just 'End Of File'. 'pause& goto' gives a pause prompt before executing the goto.

@echo off

REM MENU.bat

set FolderMonth=%date:~6,4%-%date:~3,2%

:choice

set CH=

cls

echo Pick one of the following options:

echo.

echo This is for daily backup/copy of some files Press 1 and then 'Enter'

echo This is for the main monthly backup/copy Press 2 and then 'Enter'

echo This is for weekly backup/copy Press 3 and then 'Enter'

echo To EXIT/Finish Press 4 and then 'Enter'

echo.

echo Hit a number [1-4] and press Enter.

set /P CH=[1-4]

if "%CH%"=="4" goto detta

if "%CH%"=="3" goto gamma

if "%CH%"=="2" goto beta

if not "%CH%"=="1" if not "%CH%"=="" goto choice

:alpha

echo you chose 1

xcopy /D /C /H /I /K /Y /M /S /E /V /Q "C:\quicken data\*.*" "W:\%FolderMonth%\quicken data"

pause& goto EOF

:beta

echo you chose 2

pause& goto EOF

:gamma

echo you chose 3

pause& goto EOF

:detta

echo you chose 4

pause& EOF

Like this post
Eric10

Likes # 0

I think I may have misunderstood part of your post.

I think this is want you want for your prompt to input a number:

set /P CH="Hit a number and press enter [1-4]"

Like this post
Housten

Likes # 0

Eric10,

Good Afternoon,

Well first I must apologise to you for having taken up, and wasted, so much of your time. I am giving up what is turning out to be a totally unequal struggle! I just can not get ‘xcopy’ to work and so I have been beaten by either my computer, or Microsoft or a combination!!!! I have stripped a batch file down to the minimum – see following – and it will not work. I have got the ‘yearmonth’ working and I have the ‘menu/choice’ option working very nicely – unfortunately, not yours Eric – but I can not get ‘xcopy’ to work at all. I can get the programme to make ‘w:\%foldermonth%’ perfectly OK! Including a sub-folder, but no files are copied over. I do not understand – and I have tried many, many, many options, I assure you – but all to no avail. I must admit that I got one to work, but not again. I list the file below, with some of my options. What I would be the best way was trying to get the individual parts working, and then concatenating them. Brilliant idea, absolutely rotten execution!! Well I think it was a ‘wonderful and brilliant’ idea I had, except it hasn’t worked out! The file is:-

ECHO OFF

REM set foldermonth=%date:~6,4%-%date:~3,2%

REM if not exist W:\%foldermonth% mkdir W:\%foldermonth%

xcopy /S /D /Y /M /E /V /C /Q /G /H /J "C:\QUICKEN DATA*.*" W:\TEMP1

REM xcopy "C:\QUICKEN DATA*.*" W:\%foldermonth%\ /M /D /S /E /V /C /Q /G /H /J

I had the two lines [ Lines 1 and 4 ] working at one point, with the switches at the back. So I added Lines 2 and 3, which I knew were working elsewhere, and tried it that way. No go. I tried adding line 5. No go. So I tried switching the switches to the front. No go. I have tried many options with the same result. What I have found most debilitating is that I type the file’s name into the DOS window, there is then a 1, 2, 3, 4 or 5 second delay and ‘0 files copied’ then appears. Sometimes with ‘2012-07\Quicken Data’ AND a sub-folder ‘Backup’, sometimes just ‘2012-07\Backup’. But always with ‘0n files copied’!! Weird or what?? As I say, I give up!!

I have even found a programme called ‘RoboCopy’ during my internet travels. This is a programme – which is now built into Windows – which was written by a Microsoft employee to improve ‘Xcopy’. I can’t even get that to work! Even when using so-called ‘tried and tested’ programmes.

I have said elsewhere that I couldn’t – and still can’t – get ‘Control Panel/Backup and Restore’ to work. I have since found some other little irritating things not working, and with the problems with ‘Xcopy’ I decided to try and see what ‘SFC/SCANNOW’ made of my disk, whatever. It only took it about 20 minutes – although I wasn’t actually timing it!! – for it to run through. It found nothing wrong. So, never mind anything else, ‘Backup and Restore’ doesn’t work but that’s OK. Blo**y Microsoft!! [ If I say what I really think the FE will delete this post!! ]

So what else can I do, but give up, I have spent many, many hours trying to get everything sorted – hair pulling out I think will be next - so I started with the wrong part as I should have started with the copying, but when you are as brain dead as I am 20/20 hindsight becomes a truly wonderful piece of something! Quite what I wouldn’t like to say, but it definitely becomes a part of something!! You can always start with stupidity and work your way down from there!!!

So what do I do now? Well the reason I started on this was because I was fed up with the ‘Backup’ programmes that are available, because I wanted more control over what I was ‘copying/backing up’, so I suppose I will have to go back to them, as much as this pains me, but after my debacle I see no alternative.

So may I once again thank you Eric10 for all you patience and help, and also to everyone else who have contributed to these questions/posts!!

Like this post
Eric10

Likes # 0

Hi Housten

Just because there are many switches listed in xcopy's help it doesn't mean that you have to use them all. You should be using the very minimum that gets the job done. /D and /M do very similar jobs with slight differences so pick one or the other but NOT BOTH. You are also using other spurious switches that I don't think you really need like /G, /C, and /J. You also say that the files copied once but then reported "0 file copied" on subsequent tests. Excuse me, but that is the batch file doing exactly what you have asked of it. Once you have copied the files using xcopy's /M or /D switch (or in your case, both) then the files will not copy again until the data in the source files has changed.

As a last attempt to help I'm giving you a fully working backup batch program that you can copy and paste into a NEW batch file. After pasting the code delete the long line at the end that the forum insists on putting there then work up through the code and delete all blank lines which are only there for the benefit of the forum. Save the file and then make a copy so it isn't lost if you mess it up at a later stage.

I've reduced the number of options available because if you are backing up daily into the same destination folder then a weekly backup into the same folder is completely irrelevant. I've also changed the options to better describe what the program is going to do and have included provision for handling the accidental inputting of numbers that are outside the range of available options.

The full monthly backup will create a dated folder the first time it is used in any particular month and then will copy ALL files and folders into it. Any subsequent runs of this Full backup will again copy ALL files.

The Incremental backup will only copy files that have changed since the last Incremental backup was run. If the SOURCE DATA hasn't changed then no files will be copied on a second run. Once any source data files have been changed then just the changed files will be will be copied.

The batch file will be listed in my next post which will follow immediately.

Like this post
Eric10

Likes # 0

Batch File as promised

@echo off

set FolderMonth=%date:~6,4%-%date:~3,2%

:choice

set CH=

cls

echo.

echo XCOPY BACKUP MENU

echo.

echo Pick one of the following options then press 'Enter':

echo.

echo Option 1 - MONTHLY FULL BACKUP

echo (Creates a new month folder, if needed,

echo and copies ALL FILES and FOLDERS)

echo.

echo Option 2 - INCREMENTAL BACKUP

echo (Backs up only those Files/Folders that have

echo changed since the last Incremental Backup)

echo.

echo Option 3 - EXIT without backing up

echo (No Files/Folders will be copied)

echo.

echo.

set /P CH="Choose an option number [1,2,3] "

if "%CH%"=="3" goto End

if "%CH%"=="2" goto incremental

if "%CH%"=="" goto error_1

if not "%CH%"=="1" goto error_2

:full

echo Full Monthly Backup

xcopy "C:\quicken data\*.*" "W:\%FolderMonth%\quicken data" /S /E /H /I /Y /V /Q

echo Press any key to Exit

pause > nul

goto End

:incremental

echo Incremental Backup

xcopy "C:\quicken data\*.*" "W:\%FolderMonth%\quicken data" /M /S /E /H /I /Y /V /Q

echo Press any key to Exit

pause > nul

goto End

:error_1

echo You didn't enter a number

echo Press any key to try again

pause > nul

goto choice

:error_2

echo You entered %CH% which is not a valid option

echo Press any key to try again

pause > nul

goto choice

:End

Like this post
lotvic

Likes # 0

Eric10, that's outstanding, with my limited knowledge of batch I couldn't have done it. (usually just copy some ready made off net - oops, I've just done it again ;))

Like this post
Eric10

Likes # 0

lotvic, You're welcome.

Eric

Like this post
Housten

Likes # 0

Good Afternoon, One and all,

Executive Decision!! Yup, I have now given up trying to do my own backup programme and have made the above.

What I am doing is using a sync programme so that on a daily basis - or whenever I want - some individual folders are copird over to a pendrive. With that I can take it and my wife's laptop on holiday with us, use/create/amend programmes that I use on the daily basis, and I have setup another set of files to copy these all back whenever I want to. So as ALL the files in the folders are being copied I have no worries of what is being copied which way.

With regard to my Weekly/Monthly backups I will use a reasonable free programme that I have, and start a new full back up on the first of every month, and differentials on a weekly basis.

Not perfect I accept, but at least I am quite close to what I want!!!!

Like this post

Reply to this topic

This thread has been locked.



IDG UK Sites

Best Christmas 2014 UK tech deals, Boxing Day 2014 UK tech deals & January sales 2015 UK tech...

IDG UK Sites

Chromebooks: ready for the prime time (but not for everybody)

IDG UK Sites

Hands-on with Sony's latest smartglasses

IDG UK Sites

The 13 most inspirational Tim Cook quotes