1.

Solve : My Backup Script (Easy problem) [SOLVED]?

Answer»

It's not much right now, but it should be close to finnishing. I just ran into a problem where when I tried to backup a file, I only GET 1 line of text. This must be a really easy solution, I know that I know it, it's one of those tip of my tongue things. Here's the code.

Code: [Select]@echo off
title File Backup Program
:loop
Echo Backup which file? Include extension and path.
SET /p file=
if not exist %file% echo File not found! & Pause & Cls & Goto loop
set /p backup=<%file%
echo %backup%
pause
I only got this far, because I've been debugging as I go.well found one error:
Quote

Backup which file? Include extension and path.
C:\Documents and Settings\Administrator\Desktop\Just Basic Programs\sysstat.txt
'and' is not recognized as an internal or external command,
operable program or batch file.
Press any key to continue . . .

but it doesnt happen all the TIME. i tried this next:
Quote
Backup which file? Include extension and path.
C:\Temp\list.txt
3 2 0 13 0 0 0 0 0 0 0 0
Press any key to continue . . .


dont know if thats supposed to happen or just a bug.

but thats the bugs i found so far.

Hope this HELPED
,Nick(macdad-)
if the path has spaces the variable needs quotes...

Code: [Select]set /p file=
if not exist "%file%" echo File not found! & Pause & Cls & Goto loop
set backup=<"%file%"
echo %backup%

FBQuote from: macdad- on December 30, 2008, 05:11:56 PM
well found one error:
Quote
Backup which file? Include extension and path.
C:\Documents and Settings\Administrator\Desktop\Just Basic Programs\sysstat.txt
'and' is not recognized as an internal or external command,
operable program or batch file.
Press any key to continue . . .

but it doesnt happen all the time. i tried this next:
Quote
Backup which file? Include extension and path.
C:\Temp\list.txt
3 2 0 13 0 0 0 0 0 0 0 0
Press any key to continue . . .


dont know if thats supposed to happen or just a bug.

but thats the bugs i found so far.

Hope this helped
,Nick(macdad-)


That was something I just left out because I was doing stuff in the same directory anyway. But if you do add quotes, it will only echo the first line. Once someone tells me how to get it to read not just the first line, but the entire file, then it should be able to backup any type of file.

Quote from: fireballs on December 30, 2008, 05:32:19 PM
if the path has spaces the variable needs quotes...

Code: [Select]set /p file=
if not exist "%file%" echo File not found! & Pause & Cls & Goto loop
set backup=<"%file%"
echo %backup%

FB
I assumed everyone knew that already...I think i know what you mean now, for DOS to 'read' a file use the type command:

Code: [Select]set /p file=
if not exist "%file%" echo File not found! & Pause & Cls & Goto loop
type "%file%"

FBQuote from: fireballs on December 31, 2008, 03:24:00 AM
I think i know what you mean now, for DOS to 'read' a file use the type command:

Code: [Select]set /p file=
if not exist "%file%" echo File not found! & Pause & Cls & Goto loop
type "%file%"
No. That will DISPLAY the contents of the file. My intentions were to
1. SET %backup% as the contents of the file
2. Echo %backup%>%file%.txt
FB
Quote
No. That will DISPLAY the contents of the file. My intentions were to
1. SET %backup% as the contents of the file
2. Echo %backup%>%file%.txt

The premise to #1 is wrong. Any reason you cannot use the copy command to copy the file?

Note: By requesting the user to enter path and extension, be aware that %file%.txt will resolve to a file name with multiple dots.

Quote from: Sidewinder on January 01, 2009, 07:50:11 AM
Quote
No. That will DISPLAY the contents of the file. My intentions were to
1. SET %backup% as the contents of the file
2. Echo %backup%>%file%.txt

The premise to #1 is wrong. Any reason you cannot use the copy command to copy the file?

Note: By requesting the user to enter path and extension, be aware that %file%.txt will resolve to a file name with multiple dots.


I mean SET %backup% to be the contents of the file. And for 2, I did that for a reason.Quote
I mean SET %backup% to be the contents of the file. And for 2, I did that for a reason.

Redirection can copy a single line from a file into a variable, not the entire contents of the file.

Concatenation will strip off the carriage returns/line feeds, resulting in an output file that is mis-aligned.

Why can't you use copy?



Note: Most Windows scripting languages have an instruction which can get the contents of a file into a variable. Have you considered VBScript as a solution?Quote from: Sidewinder on January 01, 2009, 09:43:51 AM
Quote
I mean SET %backup% to be the contents of the file. And for 2, I did that for a reason.

Redirection can copy a single line from a file into a variable, not the entire contents of the file.

Concatenation will strip off the carriage returns/line feeds, resulting in an output file that is mis-aligned.

Why can't you use copy?



Note: Most Windows scripting languages have an instruction which can get the contents of a file into a variable. Have you considered VBScript as a solution?
I guess I could copy, then rename to a different extension, but I want to put more than one file into a single file.Quote
I guess I could copy, then rename to a different extension, but I want to put more than one file into a single file.

Based on your previous logic, you can use the type command:

Code: [Select]type %file%.txt>>%file%.xyz

Use two output redirection symbols to prevent overwriting the output file.

Good luck.

Note: files concatenated this way can be problematical to extract from the output file.

Thanks. I didn't know that type could do that.


Discussion

No Comment Found