Jump to content

Batch File Creation


ame1011
 Share

Recommended Posts

Hi, I have a program that accepts input and creates a batch file from that input. I know that these functions can be accomplished through Autoit, but the batch file creation was requested and must be done. I have a function that creates the file:

Func GenerateBatch()
    $content = ""
    $loc = FileSaveDialog("Batch File Location", @MyDocumentsDir, "Batch File (*.bat)", 16, "backup.bat")
    
    $content &= "@echo off" & @CRLF
    $content &= 'echo.' & @CRLF
    $content &= 'echo.' & @CRLF
    $content &= 'echo ==========================================' & @CRLF
    $content &= 'echo | XXXXXXXX XXXXXXXXXXXXXXXXXXX Automatic Backup Utility |' & @CRLF
    $content &= 'echo ==========================================' & @CRLF
    $content &= 'echo.' & @CRLF
    $content &= 'echo.' & @CRLF
    $content &= 'echo ---==[ Beginning the Backup Process ]==---' & @CRLF
    $content &= 'echo.' & @CRLF
    $content &= 'echo.' & @CRLF
    
    for $x = 0 to UBound($Pairs)-1
        if ($Pairs[$x][0] <> "" And $Pairs[$x][1] <> "") Then
            $content &= 'echo.' & @CRLF & @CRLF
            if StringRight($Pairs[$x][0], 1) = "\" Then $Pairs[$x][0] &= '*.*'
            $content &= 'xcopy "'&$Pairs[$x][0]&'" "'&$Pairs[$x][1]&'*.*'&'" /d /y /e' & @CRLF
            $content &= 'echo.' & @CRLF & @CRLF
        EndIf
    Next
    
    $content &= 'pause' & @CRLF
    
    $batch_file = FileOpen($loc, 2)
    FileWrite($batch_file, $content)
    FileClose($batch_file)
    
EndFunc

$Pairs is an array that contains source + desc pairs (the source path / file being $Pairs[$x][0] and the destination path being $Pairs[$x][1]. The resulting batch file looks something like this:

CODE
@echo off

echo.

echo.

echo ==========================================

echo | XXXXXXXXXXXX XXXXXXXXXXXXXX Automatic Backup Utility |

echo ==========================================

echo.

echo.

echo ---==[ Beginning the Backup Process ]==---

echo.

echo.

echo.

xcopy "C:\Documents and Settings\Owner\Local Settings\Application Data\Microsoft\Outlook\test1.pst" "C:\Documents and Settings\Owner\Desktop\Ads\*.*" /d /y /e

echo.

echo.

xcopy "C:\Documents and Settings\Owner\Local Settings\Application Data\Microsoft\Outlook\test2.pst" "C:\Documents and Settings\Owner\Desktop\Ads\*.*" /d /y /e

echo.

echo.

xcopy "C:\Documents and Settings\Owner\Local Settings\Application Data\Identities\{16DD2E5D-B56C-4EF6-8CDC-224213F9A9F6}\Microsoft\Outlook Express\Folders.dbx" "C:\Documents and Settings\Owner\Desktop\Ads\*.*" /d /y /e

echo.

echo.

xcopy "C:\Documents and Settings\Owner\Local Settings\Application Data\Identities\{16DD2E5D-B56C-4EF6-8CDC-224213F9A9F6}\Microsoft\Outlook Express\Inbox.dbx" "C:\Documents and Settings\Owner\Desktop\Ads\*.*" /d /y /e

echo.

echo.

xcopy "C:\Documents and Settings\Owner\Local Settings\Application Data\Identities\{16DD2E5D-B56C-4EF6-8CDC-224213F9A9F6}\Microsoft\Outlook Express\Offline.dbx" "C:\Documents and Settings\Owner\Desktop\Ads\*.*" /d /y /e

echo.

echo.

xcopy "C:\Documents and Settings\Owner\Local Settings\Application Data\Identities\{16DD2E5D-B56C-4EF6-8CDC-224213F9A9F6}\Microsoft\Outlook Express\Outbox.dbx" "C:\Documents and Settings\Owner\Desktop\Ads\*.*" /d /y /e

echo.

echo.

xcopy "C:\Documents and Settings\Owner\Local Settings\Application Data\Microsoft\Address Book\test1.wab" "C:\Documents and Settings\Owner\Desktop\Ads\*.*" /d /y /e

echo.

echo.

xcopy "C:\Documents and Settings\Owner\Local Settings\Application Data\Microsoft\Address Book\test2.wab" "C:\Documents and Settings\Owner\Desktop\Ads\*.*" /d /y /e

echo.

pause

I am almost certain that the batch code is correct, however it doesn't work. I have a feeling that the carriage returns and line feeds are causing it to error out. Anyone have any idea why when running the batch file it just quickly opens and closes?

Edited by ame1011
[font="Impact"] I always thought dogs laid eggs, and I learned something today. [/font]
Link to comment
Share on other sites

I'm not sure about the linefeeds but the xcopy syntax looks strange to me. You are copying from a filename to a wildcard, what happens when you try copying from a filename to a filename?

Nevermind.

Edited by weaponx
Link to comment
Share on other sites

Hello,

I think the pipe symbol is the problem:

"echo | XXXXXXXXXXXX XXXXXXXXXXXXXX Automatic Backup Utility |"

In win XP, you may "escape" the pipe with the caret:

"echo ^| XXXXXXXXXXXX XXXXXXXXXXXXXX Automatic Backup Utility ^|"

Link to comment
Share on other sites

Hello,

I think the pipe symbol is the problem:

"echo | XXXXXXXXXXXX XXXXXXXXXXXXXX Automatic Backup Utility |"

In win XP, you may "escape" the pipe with the caret:

"echo ^| XXXXXXXXXXXX XXXXXXXXXXXXXX Automatic Backup Utility ^|"

It was the pipe symbol, thanks alot! :P

[font="Impact"] I always thought dogs laid eggs, and I learned something today. [/font]
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...