How to Read a particular digit in input arguments to batch script - input

I have a batch script in which I have set a variable- "cpu-count" and its default value is set to 1. But when I call the batch script, the cpu-count is passed as an argument and its value can vary from 1-n. For example, I have to invoke the file as ' myscript.bat cpucount-4 ' or ' myscript.bat cpucount-7 '. I have to get this value of '4' or '7' in my script. Does anyone know how to read this value from the argument passed to batch script?

You have several options to do that.
Getting from character 10 to end of string:
set value=%1
set value=%value:~9%
Removing from begining of value until dash:
set value=%1
set value=%value:*-=%
Separating value in two parts at dash, get second part:
for /F "tokens=2 delims=-" %%a in ("%1") do set value=%%a
I hope it helps...

Related

Issue with special characters in path (exclamation point !, carrot ^, etc) using batch Delayed Expansion

I have looked around and not been able to find anything to get my script working correctly with special characters (such as ! or ; or ^) in the file path or file name.
My script does work, but only if the above characters are not in any of the scanned folders or file names. If any folders or files have those characters, then the script breaks down. I need help figuring out how to make my script work with special characters (like above) within the path or file name. Here is my script:
set srcdir=%~dp0%src
set desdir=%~dp0%des
setlocal EnableDelayedExpansion
for /r "%srcdir%" %%f in ("*.txt") do (
set "subdir=%%~f"
set "subdir=!subdir:%srcdir%=%desdir%!"
echo !subdir!
pause
)
endlocal
Thanks for any and all assistance!
Exclamation marks get clobbered when delayed expansion is enabled while you set a variable. You can avoid this by waiting to delay expansion until you retrieve a variable value. Sometimes this takes some acrobatics to make it work. In this case, it's probably easier just to leave delayed expansion disabled and use call to delay expansion.
#echo off
setlocal
set "srcdir=%~dp0%src"
set "desdir=%~dp0%des"
for /r "%srcdir%" %%f in ("*.txt") do (
set "subdir=%%~f"
rem // use call to avoid delayed expansion and preserve exclamation marks
call set "subdir=%%subdir:%srcdir%=%desdir%%%"
rem // use set /p rather than echo to exploit quotation marks and preserve carets
call set /P "=%%subdir%%"<NUL & echo;
pause
)
Or if you prefer delayed expansion, one trick I like to use to toggle delayed expansion for one line is to use a for loop like this:
#echo off
setlocal
set "srcdir=%~dp0%src"
set "desdir=%~dp0%des"
for /r "%srcdir%" %%f in ("*.txt") do (
set "subdir=%%~f"
setlocal enabledelayedexpansion
for %%I in ("!subdir:%srcdir%=%desdir%!") do endlocal & set "subdir=%%~I" & echo(%%~I
pause
)
Put all paths in between "".
Always use syntax set "VAR=Value".
Toggle delayed expansion: when expanding %%~F, disable it; afterwards, enable it.
Here is the fixed code:
setlocal DisableDelayedExpansion
set "srcdir=%~dp0src"
set "desdir=%~dp0des"
for /R "%srcdir%" %%F in ("*.txt") do (
set "subdir=%%~F"
setlocal EnableDelayedExpansion
set "subdir=!subdir:%srcdir%=%desdir%!"
echo(!subdir!
pause
endlocal
)
endlocal
This only works as long as the directory where the batch file is stored does not contain exclamation marks. If it does, let me know...
Amendment
Sub-string substitution using also variables for the search and replace strings is never safe against all characters in general.
Imagine you have something like this:
echo(!VAR:%SEARCH%=%REPLACE%!
This means to replace every occurrence of %SEARCH% by %REPLACE% (in a case-insensitive manner).
But if %SEARCH% contains a =, the behaviour is changed: for instance, %SEARCH% is a=b and %REPLACE% is cd, the immediately expanded version is !VAR:a=b=cd!, so every a is going to be replaced by b=cd.
A leading * in %SEARCH% changes the behaviour: replace everything up to and including the rest of %SEARCH% by %REPLACE%. (An asterisk cannot occur within a path, of cource.)
A leading ~ in %SEARCH% changes the the behaviour from sub-string substitution to sub-string expansion, that is, expansion of a string portion given by character position an length; if the syntax is violated, the non-expanded string !VAR:~a=b! will be returned literally, supposing ~a and b are the search and replace strings, respectively.
Finally, if %SEARCH% and/or replace contain a !, this is going to be taken as the closing ! for the delayed expansion, so !VAR:a!=b! is seen as !VAR:a!, which is invalid syntax and will be kept as is.

Batch file; Replace variable's spaces with hyphens

I've started another small Batch project, however I've encountered a bit of a dilemma. I use set name= followed by set /p name=, to allow the user to give the input desired. However, I want the variable entered with spaces to then have the spaces replaced with hyphens (e.g. "hello world" becomes "hello-world").
I've searched around and found many situations in which someone asks a similar question, however they are all using the ren command, which I have no use for due to not dealing with files.
My current code is as follows:
#echo off
Cls
echo Insert a string with spaces?
set string=
set /p string=
I have also found a solution for renaming files that mentioned using ren "!file!" "!file:_= !" to change a file name's spaces to underscores (_). However changing it to set name=!name:-= ! didn't work for me.
How can I best replace spaces with hyphens in my batch file?
You're close.
#echo off
cls
echo Insert a string with spaces?
set string=
set /p string=
set string=%string: =-%
echo String is now %string%
See set /? for details on variable manipulation.

Single letter variables in cmd

Apologies for a silly and newbie question, but I have no IT background whatsoever, and I don't seem to be able to find an answer in google for that.
I am currently trying to understand (very) simple batch script which happens to be:
for /F "tokens=1,2,3 delims=," %%i in (users.csv) DO dsadd user "cn=%%j
%%i %%k,ou=2013,ou=students,dc=[domain],dc=org"
The bits that is unclear for me are %%i, %%j, %%k. I can see that they represent the columns from the csv file, respectively 1,2,3, and that the output is in the order 2,1,3.
Now, my question is - are letters i, j, k for variables fixed? I can see that this works when I replace them with a, b, c respectively, so I guess not. So is it an agreed convention?
I guess that %%i in this bit delims=," %%i in (users.csv) determines what letter should correspond to the first token, and then the following tokens are assigned alphabetically to j and k?
The following attributes apply to the for command:
The for command replaces %variable or %%variable with each text string in the specified set until the command processes all of the files.
For variable names are case-sensitive, global, and no more than 52 total can be active at any one time.
To avoid confusion with the batch parameters %0 through %9, you can use any character for variable except the numerals 0 through 9. For simple batch files, a single character such as %%f works.
You can use multiple values for variable in complex batch files to distinguish different replaceable variables.
For - http://technet.microsoft.com/en-us/library/bb490909.aspx
The first letter defined will be the first token with the remaining tokens being the next letters alphabetically.
Type the command for /? for more details.

Inserting a variable from a batch file into a text file

How would I insert a variable previously established in a batch file into a text file. I have the inserting text into a text file down, i just cant figure out the insertion of a variable.
What I am doing
SET name = "Casey"
ECHO "Hey" + name > file.txt
The result
"Hey" + name
What I want
"Hey Casey"
You should do it like this:
SET name=Casey
ECHO "Hey %name%" > file.txt
Note that there is no spaces before and after the = in
name=Casey
Too bad syntax, you need to forget other programming languages, this is Batch.
First you can't use spaces when assing values to variables, this is the way to do it:
SET "name=Casey"
Also you can do this:
SET "name= Casey"
Second Batch don't have ANY concatenate operator for strings, forget + and &, & is for concatenating commands.
So this is the correct syntax:
SET "name=Casey"
(ECHO Hey %name%)> "file.txt"
Try to use () agrupation operators when Echo a numeric ending string like "MyName 2", to avoid problems in large scripts with Batch redirections.

Adding numbers containing commas in a batch script

I'm trying to add two numbers together in a windows batch file. The numbers are coming from the output of a command and I cannot change the code to output it in a different format.
The problem is that the numbers use commas in the numbers as the thousands separator. i.e. 154022 is output as 154,022. Now when I try to add this number to another number it only adds the first part (i.e. that 154).
set A=1,000
set B=154,022
set /a TOTAL=A + B
echo %TOTAL%
produces: 155, not 155022 that I would like, or even 155,022 would do.
Is there a way to convert easily from numbers with commas to numbers without commas in a batch script?
set A=1,000
set B=154,022
set A2=%A:,=%
set B2=%B:,=%
set /a TOTAL=A2 + B2
echo %TOTAL%
You can do string manipulation like this
set result=%input:substring=replacement%
This one and other nice tips: http://www.dostips.com/DtTipsStringManipulation.php