Cara menggunakan python split string twice

If you are passing a parameter to a batch file that contains any of these delimiter characters, it will split the parameter into two parameters unless you surround the whole thing with double quotes: "this is;one=param,"

Notice that although / and - are commonly used to separate command options, they are absent from the list above. This is because batch file parameters are passed to CMD.exe which can accept it's own parameters (which are invoked using / and - )

The default token delimiters in a FOR IN() clause are the same as on the command line but the delims= option is available to specify something different. When FOR /F is used to process a string, the default delimters are Space and TAB.

When using the TAB character as a delimiter be aware that many text editors will insert a TAB as a series of SPACEs.

If you use %* to refer to all parameters, the value returned will include the delimiters.

^ Escape character.

Adding the escape character before a command symbol allows it to be treated as ordinary text. These characters which normally have a special meaning can be escaped and then treated like regular characters  : & \ < > ^ |

e.g. Echo THIS ^& THAT Echo Heading1 ^| heading2 ^| heading3 Echo The Escape character looks like this ^^

Escape and the pipeline

When piping or redirecting text, applying an escape character gets a little more complex, a pipe will start two new cmd.exe instances, each of these cmd processes are passed one side of the pipe and will parse their part once again (with the cmd-line parser, not with the batch-line-parser).

Knowing this you might try:
Echo THIS ^^& THAT | subroutine
So the new cmd instance would be passed THIS ^& THAT and everything would work? Not so fast, with two carets together, the first will escape the second, meaning that the & is no longer escaped at all.

Echo This ^^^& That | subroutine
In this version, the first caret escapes the second caret '^', the third caret escapes the '&' so now the new cmd instance inherits ^&
(Many thanks to Jeb who explained this over in the forum better than I could.)

Escaping CR/LF line endings.

The ^ escape character can be used to make long commands more readable by splitting them into multiple lines and escaping the Carriage Return + Line Feed (CR/LF) at the end of a line:

ROBOCOPY \\FileServ1\e$\users ^ \\FileServ2\e$\BackupUsers ^ /COPYALL /B /SEC /MIR ^ /R:0 /W:0 /LOG:MyLogfile.txt /NFL /NDL

Mark Yocom [MSFT] has more on this technique here.

A couple of things to be aware of:

  • A stray space at the end of a line (after the ^) will break the command, this can be hard to spot unless you have a text editor that displays spaces and tab characters.
  • If you want comment something out with REM, then EVERY line needs to be prefixed with REM.
    Alternatively if you use a double colon :: as a REM comment, that will still parse the caret at the end of a line, so in the example above changing the first line to :: ROBOCOPY… will comment out the whole multi-line command.

Some commands (e.g. REG and FINDSTR) use the standard escape character of \ (as used by C, Python, SQL, bash and many other languages.)
The \ escape can cause problems with quoted directory paths that contain a trailing backslash because the closing quote " at the end of the line will be escaped \".

To save a directory path with a trailing backslash (\) requires adding a second backslash to 'escape the escape'
so for example instead of "C:\My Docs\" use "C:\My Docs\\"

To be sure that a path includes a trailing backslash, you can test for it:

Set _prog=C:\Program Files\SS64 App
IF %_prog:~-1% NEQ \ (Set _prog=%_prog%\)
Echo "%_prog%"

Escaping Percents

The % character has a special meaning for command line parameters and FOR parameters.
To treat a percent as a regular character, double it:

%%

Many characters such as \ = ( ) do not need to be escaped when they are used within a "quoted string" typically these are characters you might find in a filename/path. The percent character is one exception to this rule, even though under NTFS % is a valid filename character.

Escaping Exclamation marks

When the shell is running in EnableDelayedExpansion mode the ! character is used to denote a variable and so must be escaped (twice) if you wish to treat it as a regular character:

^^!

Escape the Escape character

The escape character can be used to escape itself ^^ (so the first ^ will escape the second):

The characters in bold get escaped: ^& => &
^^ => ^
"^^" => "^^" ^^^& => ^&
^^^^ => ^^ ^^^^^& => ^^&

This changes slightly if you are running with DelayedExpansion of variables: if any part of the command line includes an '!' then CMD will escape a second time, so ^^^^ will become ^ [] Quoted carets passed to CALL are a special case.

Special Cases

A small number of commands follow slightly different rules, FINDSTR, REG and RUNAS all use \ as an escape character instead of ^

Using "Double Quotes"

If a single parameter contains spaces, you can pass it as one item by surrounding in "quotes" - this works well for long filenames.

In many places, e.g. the FIND comand, a " quote can be escaped by doubling it to ""

If a parameter is used to supply a filename like this:

MyBatch.cmd "C:\Program Files\My Data File.txt"

The parameters will be:

%0 =MyBatch
%1 ="C:\Program Files\My Data File.txt"

To launch a batch script with spaces in the script Path and other parameters, all requiring quotes:

CMD /k ""c:\batch files\test.cmd" "Parameter 1 with space" "Parameter2 with space""

Double quotes in a command line with a backslash escape:

powershell.exe -command "&{$m = \"hello world\";write-host $m}"

This can also be written with 3 quotes:

powershell.exe -command "&{$m = """hello world""";write-host $m}"

or more simply with :

powershell.exe -command "&{$m = 'hello world';write-host $m}"

or for SQL:

sqlplus.exe user/\""password with spaces"\"@service

Removing Quotes

Several methods for removing quotes are listed on the dequote page.

Working without Quotes

Without surrounding quotes, a long filename will be passed as %1 %2 %3...

MyBatch C:\Program Files\My Data File.txt

To refer to the pathname above use %* rather than %1 %2 %3 - the %* will cover all parameters - even if there are more than %9

You can apply Extended Filename syntax to %* by passing the value to a subroutine:

@ECHO OFF SET _params=%* :: pass params to a subroutine CALL :sub "%_params%" GOTO :eof :sub :: Now display just the filename (not path) ECHO %~n1

“All the best stories in the world are but one story in reality - the story of escape. It is the only thing which interests us all and at all times, how to escape” ~ A. C. Benson

Postingan terbaru

LIHAT SEMUA