Sign in to follow this  
Followers 0
oneway

Writing text into a Batch File

17 posts in this topic

Hi all.

I am new at autoit. I used to use WinBatch a long time ago but never was advanced.

What I am trying to do: I am controlling the execution of a batch file with autoit (simply because I could not get filecopy to work). In the batch file the path to a user exists.

For example:

C:\Documents and Settings\some-username\etc\etc...

Because of what this program is meant to do I cannot use an environment variable like "%username%" - that will not work.

What: What the program does is sync with some user data on a network (home lan).

For example:

When I am logged into my profile, and the other computer on my home lan is on,I want to sync with certain user data from that profile. However, perhaps my son is on that 'other' computer logged into 'his' profile.

The program needs to be able to do the following

Copy S:\Documents and Settings\Dad\etc\ C:\Documents and Settings\Dad\etc\ Now the user data I wanted to pull from the one computer to the this one I am on is accomplished (syncing my data) even though my son is logged in on his profile.

The only way I can think of making this work is for a drop down menu or a field that comes up when you run the program...you would type your username in the field (or select it if already saved) and it would write in the correct username where the username would go like thus:

Copy S:\Documents and Settings\Dad\etc\ C:\Documents and Settings\Dad\etc\

or

Copy S:\Documents and Settings\David\etc\ C:\Documents and Settings\David\etc\

I want Autoit to take user input from a field or drop-down and write that username into the batch file into the path where username goes...like you see in bold above.

Can someone help me do this please?

Thanks

Oneway

Share this post


Link to post
Share on other sites



First - welcome to Autoit Forums.

About your post:

- get your batch file written and the username needing replacement in the format you want (e.g #username# -> Copy S:\Documents and Settings\#username#\etc\ C:\Documents and Settings\#username#\etc\ )

- your AutoIt script will take the username from an inputbox and replace "#username#" with the username in the *.bat file

Advices:

- design your GUI with Koda (from SciTE/Tools)

- use GuiCtrlRead to get the value from your inbox

- use _ReplaceStringInFile to replace "#username#" with the value from inputbox in your *bat file

Use the help file and will help alot. Post your attempts here and you'll get more "precise" help.


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

Ok. I will lookup those commands and also look into Koda. Do I just download it from their site, install it and it works with autoit scripts?

Thanks

oneway

Share this post


Link to post
Share on other sites

First - welcome to Autoit Forums.

About your post:

- get your batch file written and the username needing replacement in the format you want (e.g #username# -> Copy S:\Documents and Settings\#username#\etc\ C:\Documents and Settings\#username#\etc\ )

- your AutoIt script will take the username from an inputbox and replace "#username#" with the username in the *.bat file

Advices:

- design your GUI with Koda (from SciTE/Tools)

- use GuiCtrlRead to get the value from your inbox

- use _ReplaceStringInFile to replace "#username#" with the value from inputbox in your *bat file

Use the help file and will help alot. Post your attempts here and you'll get more "precise" help.

Here is my GUI:

#include <ButtonConstants.au3>

#include <EditConstants.au3>

#include <GUIConstantsEx.au3>

#include <StaticConstants.au3>

#include <WindowsConstants.au3>

#Region ### START Koda GUI section ### Form=C:\home\mark\scripts\Koda\Forms\ChromeSync.kxf

$Form1 = GUICreate("", 325, 182, 287, 150)

GUISetFont(12, 400, 0, "MS Sans Serif")

$Label1 = GUICtrlCreateLabel("Enter ", 64, 24, 47, 24)

$Label2 = GUICtrlCreateLabel("USERNAME", 112, 24, 104, 24)

GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")

$Label3 = GUICtrlCreateLabel("to Sync", 216, 24, 57, 24)

$Username = GUICtrlCreateInput("Username", 64, 64, 209, 28)

$Button1 = GUICtrlCreateButton("Sync", 88, 120, 155, 25)

GUISetState(@SW_SHOW)

#EndRegion ### END Koda GUI section ###

While 1

$nMsg = GUIGetMsg()

Switch $nMsg

Case $GUI_EVENT_CLOSE

Exit

Case $Form1

Case $Username

EndSwitch

WEnd

Since I am not a programmer and this seems much more difficult than Winbatch (which I no longer own) I don't know which commands to use to make this thing work.

You will see that it is a simple GUI. A person should be able to enter their name in the box and press the button 'Sync' and the script needs to be able to replace the "#username# (in the batch file) with the 'username' that was inputed in the field.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

this writes that name to a bat file. You may want to post partial contents of your .bat if you have issues replacing the string

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=C:\home\mark\scripts\Koda\Forms\ChromeSync.kxf
$Form1 = GUICreate("", 325, 182, 287, 150)
GUISetFont(12, 400, 0, "MS Sans Serif")
$Label1 = GUICtrlCreateLabel("Enter ", 64, 24, 47, 24)
$Label2 = GUICtrlCreateLabel("USERNAME", 112, 24, 104, 24)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
$Label3 = GUICtrlCreateLabel("to Sync", 216, 24, 57, 24)
$Username = GUICtrlCreateInput("Username", 64, 64, 209, 28)
$Button1 = GUICtrlCreateButton("Sync", 88, 120, 155, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit

Case $Button1
    $file = fileopen (@Scriptdir & "\testname.bat" , 2)
    filewrite ($file , GuiCtrlRead($Username))
    fileclose($file)
EndSwitch
WEnd
Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

this writes that name to a bat file. You may want to post partial contents of your .bat if you have issues replacing the string

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=C:\home\mark\scripts\Koda\Forms\ChromeSync.kxf
$Form1 = GUICreate("", 325, 182, 287, 150)
GUISetFont(12, 400, 0, "MS Sans Serif")
$Label1 = GUICtrlCreateLabel("Enter ", 64, 24, 47, 24)
$Label2 = GUICtrlCreateLabel("USERNAME", 112, 24, 104, 24)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
$Label3 = GUICtrlCreateLabel("to Sync", 216, 24, 57, 24)
$Username = GUICtrlCreateInput("Username", 64, 64, 209, 28)
$Button1 = GUICtrlCreateButton("Sync", 88, 120, 155, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit

Case $Button1
    $file = fileopen (@Scriptdir & "\testname.bat" , 2)
    filewrite ($file , GuiCtrlRead($Username))
    fileclose($file)
EndSwitch
WEnd

Thank you. You should know I am only using the .bat file because I couldn't get 'FileCopy' to work in Autoit. lol. I am only wanting a certain folder and subsequent files to be copied from a profile on one computer to the current computer the user is on at the time. This is on a home lan. So, if a user is on any one of the computers (as long as the other one is on) the user can simply enter their username and press 'Sync' and it will copy that particular folder and files from their profile on the other computer to the one they are currently on thus updating the information on the current computer.

I hope I said that ok. IF AT ALL POSSIBLE I would like to accomplish ALL of this with Autoit instead of having to use a .bat file. There will need to be a 'Progress' bar to see while copying as well.

I wish I could do what I could and post it but since this may be a simple program for someone who knows what they are doing perhaps someone could just post what they think might do the trick and I would be much obliged and I'll just work with that suggestion and see if I can't get it to work.

Thanks!

oneway

Share this post


Link to post
Share on other sites

Thank you. You should know I am only using the .bat file because I couldn't get 'FileCopy' to work in Autoit. lol. I am only wanting a certain folder and subsequent files to be copied from a profile on one computer to the current computer the user is on at the time. This is on a home lan. So, if a user is on any one of the computers (as long as the other one is on) the user can simply enter their username and press 'Sync' and it will copy that particular folder and files from their profile on the other computer to the one they are currently on thus updating the information on the current computer.

I hope I said that ok. IF AT ALL POSSIBLE I would like to accomplish ALL of this with Autoit instead of having to use a .bat file. There will need to be a 'Progress' bar to see while copying as well.

I wish I could do what I could and post it but since this may be a simple program for someone who knows what they are doing perhaps someone could just post what they think might do the trick and I would be much obliged and I'll just work with that suggestion and see if I can't get it to work.

Thanks!

oneway

Maybe FileWriteToLine - not sure how to do it. I wish there was a better example. I don't know...geesh! lol

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

1) use the add reply button at the bottom, that way you dont get all the quoted txt.

2) filecopy works fine, as should be demonstrated with this example, click the button it creates and copies a file to a folder it creates relative to the script: should be able to filecopy without issue on button press.

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=C:\home\mark\scripts\Koda\Forms\ChromeSync.kxf
$Form1 = GUICreate("", 325, 182, 287, 150)
GUISetFont(12, 400, 0, "MS Sans Serif")
$Label1 = GUICtrlCreateLabel("Enter ", 64, 24, 47, 24)
$Label2 = GUICtrlCreateLabel("USERNAME", 112, 24, 104, 24)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
$Label3 = GUICtrlCreateLabel("to Sync", 216, 24, 57, 24)
$Username = GUICtrlCreateInput("Username", 64, 64, 209, 28)
$Button1 = GUICtrlCreateButton("Sync", 88, 120, 155, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit

Case $Button1
    $file = fileopen (@Scriptdir & "\testDIR.txt" , 2)
    filewrite ($file , "test if this was copied")
    fileclose($file)
    filecopy (@Scriptdir & "\testDIR.txt" , @Scriptdir & "\TESTCOPY\testCOPY.txt" , 9)
    exit
EndSwitch
WEnd
Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

Thanks. That works pretty decent!

I am wondering if I am going about this the wrong way though. Let me try to explain what I am doing again.

1) There are two home computers.

2) The first computer is being used...so David sits down on the second computer.

3) David logs into his profile and clicks on a shortcut to a autoit exe that copies data from a folder on the 'first' computer to the 'second' computer.

4) Now when David opens up the application he is going to work in, he sees the 'same' information that he would have seen on the last computer he worked on because his profile on this computer (second one) has been updated to match the 'first' computer.

David needs to be able to do this from whichever computer he may be on at the time. Kind of a 'Syncing' program to update a particular folder that resides in is profile.

NOT only David but every family member that uses these computers can be sure that whichever computer they sit at they can simply 'CLICK' and the pertinent information from the OTHER profile is copied over to the computer they are currently using thus giving them the most updated information. UNLESS they sit down at the same computer the second time. This, of course, is only when they have to use a different computer than they used the previous time.

I hope that makes since.

Share this post


Link to post
Share on other sites

From what I said above you will see there will still need to be a box that comes up and asks for their username in order to accomplish this.

However, if I use FileCopy to accomplish this within autoit instead of a .bat file then I won't need to use FileOpen and FileWrite. I will need a way to get autoit to take the USERNAME inputed into the field and then will use that username to copy from that user's profile on the one computer to the computer they are currently using.

It seems too complex for me but perhaps it's easier then I think it might be. I thought about ENVIROMENT VARIABLES but that won't work since they are copying from their profile that exists on another computer on the network. Carmen might be logged in on the other computer at the time but David needs the data from his profile on the other computer (the one Carmen is sitting at right now) to be copied to the current computer he is sitting at BEFORE starting his work application. Does that makes sense? I can't think of a better way of explaining it.

Any help will be greatly appreciated. Thanks!

Share this post


Link to post
Share on other sites

Any takers? :x

Share this post


Link to post
Share on other sites

You have anything besides a GUI and some walls of text?

have you tried utilizing the return from GUIctrlRead($username) in the path of a file copy for the items you want from the profile?


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

Any takers? :x

DC with roaming profile?

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

Any takers? :x

Assumng both of your machines run the same OS, this may work, but I have 2 questions.

  • Are you wanting to recurse subdirectorys?
  • What happens if a file already exists on the target? Do you want to overwrite if newer/larger/or just different?
Local $Machine1 = 'David'
Local $Machine2 = 'Carmen'
Global $Folder = @MyDocumentsDir
Local $UserName = InputBox('Work Folder Sync', 'Enter your User Name', '', ' M')
If @error = 1 Then Exit

Switch @ComputerName
    Case $Machine1
        CopyRemoteFolder($Machine2, $UserName)
    Case $Machine2
        CopyRemoteFolder($Machine1, $UserName)
    Case Else
        MsgBox(262160, 'FATAL ERROR', 'Cannot Find Remote Computer')
EndSwitch



Func CopyRemoteFolder($Computer, $User)
    Local $RemoteDrive = StringReplace($Folder, 'C:\', '\\' & $Computer & '\C$\')   ;Formats remote drive to \\Machine\C$
    Local $RemoteFolder = StringReplace($RemoteDrive, @UserName, $User) ;replaces current user name with input username
    Local $LocalFolder = StringReplace($Folder,  @UserName, $User)
    If Not FileExists($RemoteFolder) Then Exit MsgBox(262160, 'FATAL ERROR', 'Cannot Find ' & @LF & $RemoteFolder)
    FileCopy($RemoteFolder & '\*.*', $LocalFolder & '\', 9)
    MsgBox(262208, '', 'Remote Folder = ' & $RemoteFolder & @LF & 'Local Folder = ' & $LocalFolder)
    Exit
EndFunc

EDIT: Just to be sure, you are not trying to copy the entire profile over, are you?

Edited by Varian

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

No Varian,

I am not trying to copy the entire profile over just a specific directory and files.

Let's say David was computer1. Since computer1 is occupied, Carmen sits down on computer2. She runs the program enters her username and clicks sync. The particular directory I have in mind copies from s:\documents and settings\Carmen\etc\etc\ TO C:\documents and settings\Carmen\etc\etc\ Now she has the same thing on the computer2 as she had when she was last at computer1.

So..Yes. I want it to overwrite anything that is "different". Any changes at all (i.e. deletions/adds)

Edited by oneway

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

In that case change the $Folder variable to match the correct folder. If it is in the user's My Documents folder then just make it

$Folder = @MyDocuments & '\etc\etc'
All the files will be overwritten, but this script does not recurse directories...if you need that, let me know.

If the folder that you are trying to copy does not reside in the user's profile folder, a few changes in the function will be necessary.

Edited by Varian

Share this post


Link to post
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
Sign in to follow this  
Followers 0