I'm tryin to make a simple command to strip everything after a word, and echo the result.
Example:
street-ct-2a21340565364563563
thiswouldbedifferent-ss-3c63456345645635634
andthiscouldbesomethingelse-we-5d23453453634563456
removing everything expect the first section
Something like:
street-ct-221340565364563563
would result in:
street
This isn't being saved in a txt file or anything, is it possible?
Edit: Assuming that the part you are trying to extract has variable length, this seems to be an even better solution:
set str=street-ct-2a21340565364563563
for /f "delims=-" %%a in ("%str%") do set part=%%a
echo.%part%
In that snippet, we split the string whenever a "-" occurs, and then assign the first part of the split string to the "part" variable. Additional examples on how to use for to split strings can be found here.
According to this reference you could do something like this:
set str=street-ct-2a21340565364563563
set substr=%str:~0,6%
echo.%substr%
Related
I have a batch variable called version with this value "2930.2323 "
Now i want to remove the last character (or all spaces). I've tried both ways but the whitespace is not removed.
// MYVAR is set at the beginning of my batch file by another source code
SET "MYVAR=%MYVAR: =%"
Your code doesn't remove "Whitespaces", but SPACEs only (ie if it's a TAB it won't be removed unless you add another line to remove TABs too).
For your given string, I suggest another approach:
for %%a in (%myvar%) do set "myvar=%%a"
echo --%myvar%--
Note: that only works for whitespaces at the beginning or end of the string. A space in the middle of the string will split the string into two (or more). Also, some special characters will be problematic. But it will work with strings like your example ("Version numbers")
I am dealing with some code that was put together by someone who has long since left the company. It reads:
REM XX.XXX YYYYMMDD Author Description
REM version=4.3 &:20170418 comment comment comment
REM version=4.4 &:20170519 comment comment comment
SET version=4.5c &:20170604 comment comment comment
SET "version=%version: =%"
After puzzling through this, we finally figured out two things: one, that the & thing works to tell DOS that a new command is coming in the same line, and then the :date just gets thrown out because DOS doesn't know what to do with it.
But then we get to this SET "version=%version: =%" nonsense.
All I've been able to deduce from it so far is that it will remove spaces, so that if I did this instead:
SET version=4.5 c
SET "version=%version: =%"
ECHO %version%
I'll get "4.5c" echoed to the screen.
I can't find any information about this ": =%" business anywhere online. Is there a good reason to be doing this?
What Is Going On?:
This looks like Variable Edit/Replace or in other terms syntax-replacement. What this allows you to do is take a string, and replace characters or words from it and either replace the existing string or create a new modified one.
Taking example SET "version=%version: =%" This will be modifying the string version and removing all spaces from the string.
Positives To This Method:
Being that some strings or code need to be modified, you can very conveniently use a pure batch option to replace words in text files, remove words from string, add commas after words, and even remove the last x characters in a string.
syntax-replacement is commonly used for issues that that cannot be solved within a for loop or strings that need to be tweaked before being used, an example will be folder paths. In for loops, when processing strings containing \ and trying to use the delims=\, you sometimes need to change it to a less conflicting character as ; - SET "String=%String:\=;". The uses are endless.
Negatives To This Method:
This is a very easy way to edit strings but can come with a negative being that you cannot edit strings with special characters without first using an ^ to escape special characters in the base string. An Example of this will be the following:
SET "version=Hello & There" - This will break the syntax-replacement code as &
is calling a new command.
SET "version=Hello ^& There" - This is the proper way to "ignore" the & symbol
for processing.
Check out Set /? in a CMD window for more information.
I am writing some batch code to simplify a process I have of downloading some files, renaming them, and then copying them to replace the old ones. I'm running into an issue where I have a FOR loop read in a list of files from a directory, then try to modify the filenames.
The filenames all have FLY in the name, and I want to remove all text after FLY. I can't use tokens because the filenames are inconsistent in length, have multiple spaces, and wouldn't have a set number of tokens. I can't use substring because there is not a set number of characters after FLY.
I've tried using the examples at SS64 and also read numerous threads on here but nothing really seems to match my situation.
Here's the code snippet, appreciate if someone can tell me where I'm going wrong:
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "TOKENS=*" %%A IN ('DIR /B ^"%~DP0VFR^"') DO (
SET FILENAME=%%A
SET REMOVETEXT=!FILENAME:*FLY=!
SET NEWFILENAME=!FILENAME:!REMOVETEXT!=!
ECHO !FILENAME! will be renamed !NEWFILENAME!
)
When I insert echos to see what's going on everything works as expected up until the last SET, where somehow the ending result is !NEWFILENAME! is blank.
Hmm. My results were different from yours.
The " in your dir do not need to be escaped.
The problem with your set statement is that it's interpreted as
SET NEWFILENAME=!FILENAME:! + REMOVETEXT + !=!
and since FILENAME: and = are not existing variables, each will be replaced by nothing yielding "REMOVETEXT", not blank as you claim.
The solution is to use a two-stage evaluation of newname
call SET NEWFILENAME=%%FILENAME:!REMOVETEXT!=%%
which is resolved as
SET NEWFILENAME=%FILENAME:current_value_of_REMOVETEXT=%
in a sub-shell.
After cold brew it occurred to me that I might be going about this all wrong and making it more complicated than it needs to be... I decided to try directly renaming the files with wildcards and that actually worked. Didn't even need the FOR loop.
REN "%~DP0VFR\*FLY*" *FLY
No idea why the first (and overly convoluted) solution I tried didn't work, but this does with a lot less code!
Is it possible in CMD to partialy extract the value of a string from the set /p var=command and assign each part to some other different variables? I mean, let's say that we used the set /p var= command to read some input from the user, and the user typed in I am Joe . So, now the variable %var%="I am Joe" . Is it possible to assign the "I am" content of %var% to another variable and the "Joe" content to another one as well?
It kind of all depends on what you can expect from the given input. You could use substrings to subtract a fixed part of the input string, like this:
:: part1 = I am
set "part1=%var:~0,4%"
:: part2 = Joe
set "part2=%var:~5,7%"
But if the length of the input string is less than 5, the variable part2 will be undefined. As if you didn't give the variable any value.
:: Like this
set part2=
Another way to get parts of the input string is by tokenizing it. Like this:
for /f "tokens=1-3" %%a in ("%var%") do (
set "part1=%%a %%b"
set "part2=%%c"
)
But there are a couple of problems with this approach that you should be aware of. First of all, since the input is provided by the user it could contain reserved shell characters in combination with doubles quotes and such that could actually break the code. Second, the number of tokens to use and how to deal with them has to be predefined.
I am making a program that automatically backs up files, stores up to a maximum of five of them, and has an option to restore any of the five files. When the files are backed up, it adds a date and time stamp in the format of YYYYMMDD_HHMMSS_filename.ext. In order to restore the file, it is necessary to first cut out the date stamp.
The method I am currently using to cut the date stamp off of the beginning of the file is as follows.
set VAR=%VAR:~16%
echo %VAR%
The problem being, if the backed up file is called "20120825_140343_file name.txt", the above method will only return "file," omitting anything after the space. The spaces in the file names need to be preserved for them to be recognized by the program using them.
tl;dr I need to cut the string 20120825_140343_file name.txt into just "file name.txt", but my method just returns "file."
If delimiters or something would help I could separate the date stamp and file name with a different character, I.E. 20120825_140343-file-name.txt
Your method, though inelegant and inflexible, should work. This tells me that you are not storing the entire filename in VAR. That is why %var:~16% only results in file, and not file name.txt. I assume that you assign VAR like this somewhere:
SET VAR=%1
You'll need to either do this:
SET VAR=%1 %2
Or insert double-quotes around the file name when you call your batch file, and then set var like this to remove the quotes:
SET VAR=%~1
That should be enough to get your batch to work.
====================================================
But, to answer the question you actually asked, I'll show you a method of extracting "file name.txt" from var that will work even if there are more or even less than 16 prefix characters.
Use the for /f statement, specify the 3rd token, with underscores as a delimiter. Here is a self-contained example. (To run from the command-line, change %%x to %x.
SET VAR=20120825_140343_file name.txt
for /f "tokens=3 delims=_" %%x in ("%VAR%") do set VAR=%%x
ECHO %VAR%
Just remember, this solution will NOT fix your problem if you do not fix your code to assure your VAR variable has the entire filename in it.
Have you tried this?
set VAR="%VAR:~16%"