Sign in to follow this  
Followers 0
Fossil Rock

Passing variables to DOS

24 posts in this topic

#1 ·  Posted (edited)

I need to pass variables to a DOS command, anyone know how to do this?

Edited by Fossil Rock

Agreement is not necessary - thinking for one's self is!

My-Colors.jpg

cuniform2.gif

Share this post


Link to post
Share on other sites



I need to pass variables to a DOS command, anyone know how to do this?

Do you mean something like this

Run(@ComSpec & " /c " & 'dir /p', "")


Get Beta versions Here Get latest SciTE editor Here AutoIt 1-2-3 by Valuater - A great starting point.

Time you enjoyed wasting is not wasted time ......T.S. Elliot
Suspense is worse than disappointment................Robert Burns
God help the man who won't help himself, because no-one else will...........My Grandmother

Share this post


Link to post
Share on other sites

Do you mean AutoIt variable values or environment variables (or neither)?

; Set up some AutoIt variables
  $beast = "deerhound"
  $vice = "cheese"
 ; Export a value to the FLAVOR DOS environment variable for the current
 ; AutoIt script's enviromnent, which will be inherited by child processes
  EnvSet("FLAVOR", "ripe")
 ; Run a DOS command and leave the command window open ( /k ) .
  Run(@Comspec & " /k echo The " & $beast & " craves %FLAVOR% " & $vice & ".")

Yes yes yes, there it was. Youth must go, ah yes. But youth is only being in a way like it might be an animal. No, it is not just being an animal so much as being like one of these malenky toys you viddy being sold in the streets, like little chellovecks made out of tin and with a spring inside and then a winding handle on the outside and you wind it up grrr grrr grrr and off it itties, like walking, O my brothers. But it itties in a straight line and bangs straight into things bang bang and it cannot help what it is doing. Being young is like being like one of these malenky machines.

Share this post


Link to post
Share on other sites

Do you mean something like this

Run(@ComSpec & " /c " & 'dir /p', "")

What I'm trying to do is run the DOS command TYPE on a file and redirect it to another file using variable from my script.

Run(@ComSpec & "Type " &  $Path & $File1 " > " & $Path & $File2)

Agreement is not necessary - thinking for one's self is!

My-Colors.jpg

cuniform2.gif

Share this post


Link to post
Share on other sites

Fossil Rock I don't know what are you trying to do? but I will used FileCopy() instead.

Anyway this is what you want...

$Path = @TempDir
$File1 = '\Test.txt'
$File2 = '\Test2.txt'

Run(@ComSpec & ' /c Type "' & $Path & $File1 & '" > "' & $Path & $File2 & '"', @WindowsDir, @SW_HIDE)

The double quots are include for just in case the path has spaces.


AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line

Share this post


Link to post
Share on other sites

Fossil Rock I don't know what are you trying to do? but I will used FileCopy() instead.

Anyway this is what you want...

$Path = @TempDir
$File1 = '\Test.txt'
$File2 = '\Test2.txt'

Run(@ComSpec & ' /c Type "' & $Path & $File1 & '" > "' & $Path & $File2 & '"', @WindowsDir, @SW_HIDE)

The double quots are include for just in case the path has spaces.

Thanks, that works great! As to what it's for... look at this thread UNICODE to ANSI Conversion


Agreement is not necessary - thinking for one's self is!

My-Colors.jpg

cuniform2.gif

Share this post


Link to post
Share on other sites

How would I be able to run 2 commands in the same line?


Agreement is not necessary - thinking for one's self is!

My-Colors.jpg

cuniform2.gif

Share this post


Link to post
Share on other sites

How would I be able to run 2 commands in the same line?

RunWait(@ComSpec & ' /k echo "This is Command 1" & echo "And this is Command 2"')

& = Use to separate multiple commands on one command line. Cmd.exe runs the first command, and then the second command.

&& = Use to run the command following && only if the command preceding the symbol is successful. Cmd.exe runs the first command, and then runs the second command only if the first command completed successfully.

|| = Use to run the command following || only if the command preceding || fails. Cmd.exe runs the first command, and then runs the second command only if the first command did not complete successfully (receives an error code greater than zero).

If you are running on XP, then have a look in Help and Support Center for Command shell overview for more information.

Share this post


Link to post
Share on other sites

The single / double qoutes thing is really messing me up. This command works fine for me.

RunWait(@ComSpec & ' /c Type "' & $UNICODE & '" > "' & $ANSI & '"', "", @SW_HIDE)

Now I'm trying to add chcp 1252 as the first command.

RunWait(@ComSpec & ' /c chcp 1252" & 'Type "' & $UNICODE & '" > "' & $ANSI & '"', "", @SW_HIDE)

But it doesn't work. What am I doing wrong?


Agreement is not necessary - thinking for one's self is!

My-Colors.jpg

cuniform2.gif

Share this post


Link to post
Share on other sites

If you use single quotes by default, then you will find adding double quotes where needed much easier.

RunWait(@ComSpec & ' /c chcp 1252 & Type "' & $UNICODE & '" > "' & $ANSI & '"', '', @SW_HIDE)

Share this post


Link to post
Share on other sites

How would I be able to run 2 commands in the same line?

An alternative method would be to write the lines you need to a batch file (.cmd or .bat), and then run that. Syntactically, it can be a lot cleaner.

You may also wish to investigate using StringFormat() or the RunDOS() UDF to build command lines.


Reading the help file before you post... Not only will it make you look smarter, it will make you smarter.

Share this post


Link to post
Share on other sites

& =

&&

||

@MHz (or other knowledgable party)

I no longer have access to Win9X / NT4 machines :o

Do you happen to know if these symbols are valid on those OS' ?


Reading the help file before you post... Not only will it make you look smarter, it will make you smarter.

Share this post


Link to post
Share on other sites

@flyingboz

Currently, I am not sure. The conditional processing symbols are listed in @WindowsDir & \Help\ntcmds.chm. Browsing Micro$oft mentions nil as to whether they are supported on 9x or NT4. Perhaps someone with a 9x and/or a NT4 OS may want to test the symbols for a definite answer.

Share this post


Link to post
Share on other sites

@flyingboz

Currently, I am not sure.

Thx for the reply... I have some cases where I am currently writing batch files out, and would potentially rewrite to take advantage of them, (the more I use StringFormat() the more I love it! ) but I don't want to have to case around OS issues.

Reading the help file before you post... Not only will it make you look smarter, it will make you smarter.

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

If you use single quotes by default, then you will find adding double quotes where needed much easier.

RunWait(@ComSpec & ' /c chcp 1252 & Type "' & $UNICODE & '" > "' & $ANSI & '"', '', @SW_HIDE)

It works, but it doesn't work correctly. It seems to go to fast. When I do it manually it works fine, automatically it doesn't.

Edited by Fossil Rock

Agreement is not necessary - thinking for one's self is!

My-Colors.jpg

cuniform2.gif

Share this post


Link to post
Share on other sites

How can I add a pause in between the commands?


Agreement is not necessary - thinking for one's self is!

My-Colors.jpg

cuniform2.gif

Share this post


Link to post
Share on other sites

I really need a good understanding of @ComSpec.

When I send a command via @ComSpec it works fine. When I send another command (in the same line) it doesn't take advantage of what the first command did.

I'm trying to set the code page and then use the TYPE command. It does the code page change and then the TYPE command but it doesn't utilize the new code page.

I'm using the code page inconjunction with the TYPE command to convert UNICODE text to ASCII text.

When I use the /k switch to leave the window open and then type the command at the cursor it works fine. With the @ComSpec command it does it all before the cursor appears. It seems to me that the first command doesn't go active until the cursor appears.

Any ideas / suggestions ?

P.S. I've opted not to use the batch file idea because I don't want a file available to be altered or removed.


Agreement is not necessary - thinking for one's self is!

My-Colors.jpg

cuniform2.gif

Share this post


Link to post
Share on other sites

Try using && to separate the chcp command and the type command instead of only &. You can use the /k with @Comspec and use the exit command at the end of the linked commands, if that makes a difference.

If the command doesn't want to work with @comspec directly, then you could use AutoIt to write a DOS batch file, execute it, and then delete it.

Share this post


Link to post
Share on other sites

Try using && to separate the chcp command and the type command instead of only &. You can use the /k with @Comspec and use the exit command at the end of the linked commands, if that makes a difference.

If the command doesn't want to work with @comspec directly, then you could use AutoIt to write a DOS batch file, execute it, and then delete it.

The && works the same and would only make a difference if the first command didn't work. I'm using the /k switch to see what's happening, but it doesn't really show anything. It does the commands before the prompt appears. The problem (I guess) is it's doing the second command and the chcp hasn't registered yet.

As for the batch file, I've thought of that and guess I'll have to do it that way if there is no solution otherwise. Another thought would be to send keystrokes to the open window, but haven't started on that yet.

I also saw a post with something like this in it:

RunWait(@ComSpec & '/k [command] & (@ComSpec '/k [command] ......

It wasn't clear enough so I don't know if nested ComSpecs would work or not.

Thanks for the input.


Agreement is not necessary - thinking for one's self is!

My-Colors.jpg

cuniform2.gif

Share this post


Link to post
Share on other sites

#20 ·  Posted (edited)

If I run this, then I get unicode to ansi file

$UNICODE = 'D:\Documents and Settings\Desktop\test.txt'
$ANSI = 'D:\Documents and Settings\Desktop\test_ansi.txt'
RunWait(@ComSpec & ' /c chcp 1252' & _
        '& Type "' & $UNICODE & '" > "' & $ANSI & '"' & _
        '& exit', '', @SW_HIDE)

Or with a ping added, also unicode to ansi file

$UNICODE = 'D:\Documents and Settings\Desktop\test.txt'
$ANSI = 'D:\Documents and Settings\Desktop\test_ansi.txt'
RunWait(@ComSpec & ' /c chcp 1252' & _
        '& ping -n 1 127.0.0.1 > nul' & _
        '& Type "' & $UNICODE & '" > "' & $ANSI & '"' & _
        '& exit', '', @SW_HIDE)

This is tested with a blank text file.

Edit: Just realized that I tested with /c with @comspec, rather then the /k.

You could also try the copy command to copy the file to ansi. Not sure if it will change the unicode chars to ansi.

RunWait(@ComSpec & ' /c copy /a "' & $UNICODE & '" "' & $ANSI & '"', '', @SW_HIDE)
Edited by MHz

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