#1 24 Nov 2017 15:30

vasilis
Member
Registered: 24 Nov 2017
Posts: 5

One line text replacements in CDM

Hi!
I came across this forum as I was looking for a way to make text replacements through command line (cmd). I am new to programming and I need some help

First, I found this …
SET _test=12345abcabc
SET _result=%_test:ab=xy%
ECHO %_result%

…but it’s not exactly what I am looking for!

I need to be able to make more replacements IN ONE LINE. I got a reason for asking this!
For example, to be able to replace “ab” with “xy”, “1” with “this”, “2” with “that” etc etc. Again, IN ONE LINE. Is that possible (with this or with another piece of code) without using a command utility software? If yes, it is compatible in all Windows versions (7, 8, 8.1, and 10 etc)?

Thank you

Offline

#2 24 Nov 2017 16:13

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,040

Re: One line text replacements in CDM

The native text substitution routine is primitive.  If you have multiple replacements to make, you'll need multiple statements.

SET _test=12345abcabc
SET _result=%_test:ab=xy%
SET _result=%_result:1=2%

And so forth.

Otherwise you're looking at something like a port of GNU sed -- but that has its own complexities.

Last edited by bluesxman (24 Nov 2017 16:15)


cmd | *sh | Ruby | Chef | Puppet | PowerShell | AutoIT3

Offline

#3 24 Nov 2017 19:07

Aacini
Member
Registered: 05 Dec 2012
Posts: 126

Re: One line text replacements in CDM

Well, technically, the replacements in this code are made IN ONE LINE:

@echo off
setlocal EnableDelayedExpansion

SET _test=12345abcabc
for %%a in ("ab=xy" "1=this" "2=that") do set _test=!_test:%%~a!
echo %_test%

Antonio

Offline

#4 25 Nov 2017 08:48

vasilis
Member
Registered: 24 Nov 2017
Posts: 5

Re: One line text replacements in CDM

Hi! Thank you both for replying!

Antonio! What do I do wrong? It seems that CMD doesnot accept the replacements! I  am getting an error (in Greek) saying something like "it wasn't expected at this moment

"mXESYR

Offline

#5 25 Nov 2017 08:51

vasilis
Member
Registered: 24 Nov 2017
Posts: 5

Re: One line text replacements in CDM

You can see the cmd image https://ibb.co/mXESYR

Offline

#6 25 Nov 2017 12:32

vasilis
Member
Registered: 24 Nov 2017
Posts: 5

Re: One line text replacements in CDM

bluesxman wrote:

The native text substitution routine is primitive.  If you have multiple replacements to make, you'll need multiple statements.

SET _test=12345abcabc
SET _result=%_test:ab=xy%
SET _result=%_result:1=2%

And so forth.

Otherwise you're looking at something like a port of GNU sed -- but that has its own complexities.

Bluesxman!
Your code seems to work. However, I have to make 130 different replacements. All of them include symbols (e.g. a=[!], b= [@] etc).

I am developing a simple bot (with WinAutomation software) that will run in very slow PCs. I am currently using a loop to do the replacements. Loop is executed very fast and causes CPU and memory overload. From the other hand, If I place a waiting action at the end of the loop (1 sec minimum), the replacements will finish in 130 seconds. This is time wasting! I need a ONE LINE code in order to make the replacements at once.

I have uploaded an image from WinAutomation’s Run Command Window. You can view it here https://imgur.com/3hbcCZE.
Thank you

Offline

#7 25 Nov 2017 22:18

Aacini
Member
Registered: 05 Dec 2012
Posts: 126

Re: One line text replacements in CDM

It seems there is a fundamental confusion here! I assumed that you want to create a Batch file, that is, a text file with .BAT extension that you will execute via its name. My solution works correctly if you create a Batch file with it: open Notepad, enter the lines, select "Save as" and give any name. After that, change the extension from .txt to .bat. To execute such Batch file, enter its name...

However, if you want to enter several commands one-by-one at the command prompt, then my method don't work. Please, be very clear about the method you want to use to perform the replacements...

Antonio

Offline

#8 25 Nov 2017 23:19

vasilis
Member
Registered: 24 Nov 2017
Posts: 5

Re: One line text replacements in CDM

Aacini wrote:

It seems there is a fundamental confusion here! I assumed that you want to create a Batch file, that is, a text file with .BAT extension that you will execute via its name. My solution works correctly if you create a Batch file with it: open Notepad, enter the lines, select "Save as" and give any name. After that, change the extension from .txt to .bat. To execute such Batch file, enter its name...

However, if you want to enter several commands one-by-one at the command prompt, then my method don't work. Please, be very clear about the method you want to use to perform the replacements...

Antonio

Antonio!
Sorry for not being clear! I followed your instructions about the batch file and … yes!!! It worked!!! Thank you!!!!

It is possible to run a similar one-line code directly to the cmd without a batch file?
I need the replacements to encode/decode a text in a simple application I am making. For this reason, the replacements must not be visible to user who might be able to access the batch file before my application deletes it and removes it from recycle bin.

The application will run in a very slow Windows PC. The replacements are 130 in total. Writing as less code lines as possible will make the application run faster. Furthermore, as you saw in image https://imgur.com/3hbcCZE, WinAutomation allows me to use only a single line of code in the DOS command field.

Thank you

Offline

#9 28 Nov 2017 10:14

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,040

Re: One line text replacements in CDM

You are limited by what you can do in a single line without use of a batch script.

For instance the "setlocal EnableDelayedExpansion" and use of "!" expansion -- which are fundamental to Aacini's method -- do not work outside of a batch script.

However, you should be able to adapt it like so (with a performance penalty -- "call" is expensive in bulk), to work at the command line:

for %a in ("ab=xy" "1=this" "2=that") do call set _test=%_test:%~a%

NB -- Because this is a slight misuse of "for" (it's really for processing files) you may find strange behaviour occurs if your replacements contain wildcards (*, ?) -- asterisk is a special character in substring replacement as well.  You might find that escaping such with "^" will help (though it may not, I've not forced the issue to confirm).

Last edited by bluesxman (28 Nov 2017 10:16)


cmd | *sh | Ruby | Chef | Puppet | PowerShell | AutoIT3

Offline

Board footer

Powered by FluxBB