2016-02-06 6 views
6

मैं ~ के सभी उदाहरणों को {~} के साथ बैच स्क्रिप्ट में बदलने की कोशिश कर रहा हूं ताकि वीबीस्क्रिप्ट यह नहीं मानता कि मेरा मतलब है कि जब मैं इसे भेजने के लिए पास करता हूं तो प्रवेश करने का मतलब है, हालांकि, चर के लिए अक्षर को बदलना होता है इसे बदलो।सेट/प्रतिस्थापित करने में "~" कैसे बचें?

उदाहरण के लिए

,

rem Intended function; 
rem input = Hello~world. 
rem output = Hello{~}world. 

हालांकि हर बार, यह या तो कुछ नहीं करता है, या शाब्दिक स्ट्रिंग refVar:~={~} को refVar सेट।

यहां मैंने जो कोशिश की है;

set refVar=!refVar:~={~}! 

rem Also 
set refVar=%refVar:~={~}% 

rem and... 
set refVar=%refVar:^~=^{^~^}% 

rem and of course; 
set refVar=!refVar:^~=^{^~^}! 

rem and even: 

set "pre=~" 
set "post={~}" 
set refVar=%refVar:!pre!=!post!% 

rem and the same set commands with; 
set refVar=!refVar:%pre%=%post%! 

क्या मुझे इससे बचने का कोई तरीका नहीं है? मुझे पूरा यकीन है कि इसे ~ के साथ एक समान कमांड में एक स्थितित्मक चरित्र होने के साथ करना है।

मुझे समझ में आता है कि वीबीस्क्रिप्ट में इसे ठीक करने का कोई तरीका है, लेकिन इससे मुझे स्पष्ट कामकाज के बिना प्रतिबंध लगाने की परेशानी होती है।

उत्तर

2

मैं में Dennis van Gils से ब्रेसिज़ में टिल्ड द्वारा पर्यावरण चर के मूल्य स्ट्रिंग में प्रत्येक टिल्ड कैरेक्टर को बदलने के लिए शायद एक आसान समाधान प्रदान करें जो इस कार्य के लिए भी ठीक काम करता है।

@echo off 
set "TestVar=hello~world." 
echo TestVar has value: %TestVar% 
call :SpecialReplace "TestVar" 
echo TestVar has value: %TestVar% 
goto :EOF 


rem The subroutine below expects the name of an environment variable as 
rem parameter. The subroutine does nothing if called without parameter. 
rem Also nothing is done if specified environment variable is not defined. 

rem Each tilde character in value of this environment variable is replaced 
rem by {~} by this subroutine. 

rem Note: This subroutine can be also easily modified to replace other 
rem special characters like the equal sign by a different string which 
rem can be also no string in case of special character should be removed. 
rem Just modify Search and Replace variables for a different replace. But 
rem be aware of more code must be changed if search string is longer than 
rem one character. Length of replace string does not matter on code below. 

:SpecialReplace 
if "%~1" == "" exit /B 
setlocal EnableDelayedExpansion 
set "VarName=%~1" 
set "VarValue=!%VarName%!" 
if "!VarValue!" == "" endlocal & exit /B 
set "NewValue=" 
set "Search=~" 
set "Replace={~}" 

:ReplaceLoop 
if "!VarValue:~0,1!" == "!Search!" (
    set "NewValue=!NewValue!!Replace!" 
) else (
    set "NewValue=!NewValue!!VarValue:~0,1!" 
) 
set "VarValue=!VarValue:~1!" 
if not "!VarValue!" == "" goto ReplaceLoop 
endlocal & set "%VarName%=%NewValue%" & exit /B 

इस्तेमाल किया आदेशों को समझने और वे कैसे काम के लिए, एक कमांड प्रॉम्प्ट विंडो खोलने वहाँ निम्नलिखित आदेश पर अमल, और सभी मदद पेज बहुत सावधानी से प्रत्येक आदेश के लिए प्रदर्शित पूरी तरह से पढ़ें।

  • call /?
  • echo /?
  • endlocal /?
  • exit /?
  • goto /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?

और देखना यह भी समझते हैं कि कैसे दो आदेशों के बीच एक भी एम्परसेंड के साथ कई आदेशों के साथ लाइनों काम Single line with multiple commands using Windows batch file पर जवाब।


अब चर खोज का समर्थन करने और के रूप में Dennis van Gils ने सुझाव दिया दूसरे और तीसरे पैरामीटर के रूप में निर्दिष्ट स्ट्रिंग की जगह एक विस्तारित सबरूटीन SpecialReplace के साथ ऊपर का एक प्रकार।

@echo off 
set "TestVar=hello~world." 
echo TestVar has value: %TestVar% 
call :SpecialReplace "TestVar" "~" "{~}" 
echo TestVar has value: %TestVar% 
goto :EOF 


rem The subroutine below expects the name of an environment variable as 
rem first parameter. The subroutine does nothing if called without parameter. 
rem Also nothing is done if specified environment variable is not defined. 

rem The second parameter must be the search string. The subroutine does 
rem nothing if there is no second parameter which can be also just one 
rem or two double quotes enclosed in double quotes. 

rem The third parameter is an optional replace string. Without a third 
rem parameter all occurrences of search string are removed from value 
rem of the specified environment variable. 

rem With just one or two double quotes in double quotes as search string 
rem no replace string can be defined because command processor does not 
rem parse the quote strings as most would expect it. But """ or """" as 
rem search string with no replace string works and results in removing 
rem all double quotes or all occurrences of two double quotes. 

:SpecialReplace 
if "%~1" == "" exit /B 
setlocal EnableDelayedExpansion 
set "VarName=%~1" 
set "VarValue=!%VarName%!" 
if "!VarValue!" == "" endlocal & exit /B 
set "NewValue=" 
set "Search=%~2" 
if "!Search!" == "" endlocal & exit /B 
set "Replace=%~3" 
set "Length=0" 

:GetLength 
if "!Search:~%Length%,1!" == "" goto ReplaceLoop 
set /A Length+=1 
goto GetLength 

:ReplaceLoop 
if "!VarValue:~0,%Length%!" == "!Search!" (
    set "NewValue=!NewValue!!Replace!" 
    set "VarValue=!VarValue:~%Length%!" 
) else (
    set "NewValue=!NewValue!!VarValue:~0,1!" 
    set "VarValue=!VarValue:~1!" 
) 
if not "!VarValue!" == "" goto ReplaceLoop 
endlocal & set "%VarName%=%NewValue%" & exit /B 
+0

यह वास्तव में एक साफ जवाब है। आसानी से उपयोगिता के लिए खोज के लिए तर्कों का उपयोग करके स्ट्रिंग का उपयोग करना बेहतर हो सकता है। –

+0

हाहा, मुझे अधिकांश भाग के लिए बैच-फ़ाइल स्क्रिप्टिंग कमांड पता है, लेकिन मुझे लगता है कि सहायता आदेश अन्य लोगों के लिए एक ही उत्तर की तलाश में हैं। किसी भी तरह से, धन्यवाद, समाधान को जानकर खुशी हुई कि केवल delimiters का एक निश्चित आदेश नहीं था, मैं कोशिश करने में असफल रहा। – Bloodied

+0

@Arescet हां, मैंने प्रयुक्त आदेशों की सूची और इस उत्तर को पढ़ने वाले सभी के लिए उनकी सहायता कैसे प्राप्त की है क्योंकि मैं लगभग हमेशा अपने बैच फ़ाइल से संबंधित उत्तरों पर करता हूं। यह आपके प्रश्न से स्पष्ट रूप से मेरे लिए था कि आप उन सभी आदेशों को बहुत अच्छी तरह से जानते हैं। – Mofi

4

तो मैंने कुछ खुदाई की और ऐसा लगता है कि यह वास्तव में वास्तव में कठिन है। हालांकि, मैं मिला this है, जो समस्या का हल, हमें देने के लिए इस कोड के रूप में:

@echo off 

set "input=Hello~world." 
echo input: %input% 

call :wavereplacer "%input%" {tilde} output 
set output=%output:{tilde}={~}% 
echo output: %output% 
pause 

::your own code should be above this. 
goto :eof 

:wavereplacer String Replacer [RtnVar] 
setlocal 
rem the result of the operation will be stored here 
set "result=#%~1#" 
set "replacer=%~2" 
call :strlen0.3 result wl 
call :strlen0.3 replacer rl 

:start 

    set "part1=" 
    set "part2=" 

    rem splitting the string on two parts 
    for /f "tokens=1* delims=~" %%w in ("%result%") do (
    set "part1=%%w" 
    set "part2=%%x" 
) 

    rem calculating the count replace strings we should use 
    call :strlen0.3 part1 p1l 
    call :strlen0.3 part2 p2l 
    set /a iteration_end=wl-p1l-p2l 

    rem creating a sequence with replaced strings 
    setlocal enableDelayedExpansion 
    set "sequence=" 
    for /l %%i in (1,1,%iteration_end%) do (
    set sequence=!sequence!%replacer% 
) 
    endlocal & set "sequence=%sequence%" 

    rem adjust the string length 
    set /a wl=wl+iteration_end*(rl-1) 

    rem replacing for the current iteration 
    set result=%part1%%sequence%%part2% 
    rem if the second part is empty the task is over 
    if "%part2%" equ "" (
    set result=%result:~1,-1% 
    goto :endloop 
) 


    goto :start 

:endloop 
endlocal & if "%~3" neq "" (set %~3=%result%) else echo %result% 
exit /b 

:strlen0.3 StrVar [RtnVar] 
    setlocal EnableDelayedExpansion 
    set "s=#!%~1!" 
    set "len=0" 
    for %%A in (2187 729 243 81 27 9 3 1) do (
    set /A mod=2*%%A 
    for %%Z in (!mod!) do (
     if "!s:~%%Z,1!" neq "" (
     set /a "len+=%%Z" 
     set "s=!s:~%%Z!" 

    ) else (
     if "!s:~%%A,1!" neq "" (
      set /a "len+=%%A" 
      set "s=!s:~%%A!" 
     ) 
    ) 
    ) 
) 
    endlocal & if "%~2" neq "" (set %~2=%len%) else echo %len% 
exit /b 

यह तुरंत ~{~} को बदल नहीं सकते, तो आप एक अस्थायी प्रतिस्थापन उपयोग करने की आवश्यकता इस मामले {tilde}

1
@ECHO Off 
SETLOCAL ENABLEDELAYEDEXPANSION 
SET "input=hello~world" 

SET "xhead=!input:%input:*~=%=!" 
SET "output=!input:%xhead%=%xhead:~0,-1%{~}!" 

ECHO %input% --^> %output% 

GOTO :EOF 

यह वहाँ ठीक एक ~ है और यह प्रारंभिक या टर्मिनल चरित्र नहीं है कि प्रदान की काम करने के लिए प्रकट होता है।

संबंधित मुद्दे