You are not logged in.
Pages: 1
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
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
Offline
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
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
"
Offline
You can see the cmd image
Offline
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
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
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
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
Offline
Pages: 1