Modify file contents based on Windows' version - batch-file

I want to add few lines in a text file based on os name. A pseudocode example:
os==windows 7 then open file c:\1.txt else open file c:\2.txt
I want to add some static text to that text file and save the text file.
How do I do that in batch script? My pseudocode attempt:
FOR /F "delims=" %i IN ('ver') DO set osver=%i
echo %osver%
if %osver% == "Microsoft Windows [Version 6.1.7601]"
set filepath == C:\1.txt and open 1.txt file and add text" abc "
else
set filepath == C:\2.txt and open 2.txt file and add text " abc"

Do not over complicate it by creating variables, else you might need delayedexpansion
#echo off
for /f "tokens=2 delims=[]" %%i in ('ver ^| more +1') do (
if "%%i"=="Version 10.0.17134.228" echo abc >> "C:\1.txt"
if "%%i"=="Version 6.1.7601" echo abc >> "C:\2.txt"
)
We split away version X.X.X.X only as we want to match as little as possible. Then simply do if and echo to the relevant file.
For more on the above commands, see from cmdline for /? and if /?
You need to copy the above code into a text file and save it as a file with a .cmd or .bat extension. Please do not call it ver.bat or ver.cmd as ver is a system command. Call it something like my_version_script.cmd instead.

Related

How to populate undetermined number of variables from text file?

So I know how to save strings to a text file as a list from batch by using
set /p Myvar1="Enter your variable name/value: "
set /p Myvar2="Enter your variable name/value: "
set /p Myvar3="Enter your variable name/value: "
And then append to a text file
echo %Myvar1% %Myvar2% %Myvar3% >> Mylist.txt
So when I open the text file through the batch file, it can be displayed
as a list:
SET "variables="
ECHO =================================================
ECHO My list of stuff
ECHO =================================================
< Mylist.txt (
set /p MyVar1=
set /p MyVar2=
set /p MyVar3=
)
::set line
ECHO - [0] - %Myvar1%
ECHO - [1] - %Myvar1%
ECHO - [2] - %Myvar1%
Now the problem is that:
For each new line on the Mylist.txt text file I have to manually add lines on the batch file. On the provided example the batch is setup so it displays 3 lines of text from the text file. If the text file has 10 lines, it will only show the first 3 lines because that is what is specified. So I would like to accomplish the opposite of this script.
The batch file should be able to:
Batch file reads Mylist.txt.
For each line in Mylist.txt file the batch file creates a "numerated variable".
Each "numerated variable" can be addressable so the user can be prompted to select one of the options on the list 1, 2, 3, 4, etc.
The easiest method to save variable names and their values to a file is redirecting the output of command SET to a file.
set My >"%APPDATA%\MyVariables.txt"
This simple line saves all environment variables starting with My in name with name and value into file MyVariables.txt in application data directory of current user.
And following command line can be used in a batch file to reload those environment variables from file:
for /F "usebackq delims=" %%I in ("%APPDATA%\MyVariables.txt") do set "%%I"
Same command line for usage directly in a command prompt window:
for /F "usebackq delims=" %I in ("%APPDATA%\MyVariables.txt") do set "%I"
A little bit more secure would be checking if each line in the file MyVariables.txt really starts with My as expected and so was not manipulated by someone who wants to override for example PATH by adding manually to file MyVariables.txt a line starting with PATH=.
It might be useful to know how many variables were loaded from file. This can be achieved with a simple extension:
set "VariablesCount=0"
for /F "usebackq delims=" %%I in ("%APPDATA%\MyVariables.txt") do set "%%I" & set /A VariablesCount+=1
Of course if there is an environment variable MyVariablesCount starting with My then this variable with its value would be also saved into the file and reloaded from file which would make it unnecessary to count the number of variables set from file.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
for /?
set /?
Read also the Microsoft article about Using Command Redirection Operators.
simple thing with a for loop plus a counter:
setlocal enabledelayedexpansion
set i=0
for /f "delims=" %%a in (mylist.txt) do (
set /a i+=1
set var[!i!]=%%a
)
echo number of variables: %i%:
set var[
echo ---------
for /l %%a in (1,1,%i%) do echo variable %%a = %var[%%a]%

How to pull info from within a text file - using a batch file

I need to pull information out of a text file which is always after the third comma but before the 4th eg I need tht,1,2,THIS INFO,444
then save this into a file .txt
is this possible using a .bat
I need to pull information out of a text file from between the 3rd and 4th ,
tht,1,2,THIS INFO,444
Use the following batch file (test.cmd):
#echo off
setlocal
for /f "tokens=4 delims=," %%i in (myfile.txt) do echo %%i > newfile.txt
endlocal
Notes:
Input file is myfile.txt
Output file is newfile.txt
Example output:
F:\test>type myfile.txt
tht,1,2,THIS INFO,444
F:\test>test
F:\test>type newfile.txt
THIS INFO
F:\test>
Further Reading
An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
for /f - Loop command against the results of another command.
try
for /f "delims=, tokens=3" %i in (myfile.txt) do echo %i
Thanks for the correction, (in comments below.)

grep specific string from a file and store the result in another file using bat or vbs

I have the below contents in file1
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Applications\vlc.exe\shell\Open\command]
#="\"C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe\" --started-from-file \"%1\""
I need C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe string to be copied from file1 to file2.
As a end result file2 contents should have
C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe
How can we achieve this using a bat file or a vbs ? Please share your thoughts. Thanks!
#echo off
for /f usebackq^ tokens^=^3^ delims^=^" %%a in ("file1") do >"file2" echo %%a
Using the quote as delimiter, read file1, split the lines to get the third token in line and send to file2
#ECHO OFF
SETLOCAL
FOR /f "tokens=1*delims==" %%a IN (q21568377.txt) DO IF NOT "%%b"=="" FOR /f "tokens=1,2delims=:-" %%c IN (%%b) DO SET var1=%%c&SET var2=%%d
SET var=%var1:~-1%:%var2:~0,-3%
ECHO %var%
GOTO :EOF
I used a file named q21568377.txt for my testing.
Output simply shown on screen. Redirect to file if that's your wish.

Find and Replace from batch file not working

I am trying to find and replace values of a string within a batch file but having issues. I have the user run the batch file and it asks the user 1)what drive the file is on 2)what is the name of folder in the TEST parent folder 3)what is the name of the new server. I want the batch file to look within a file called importer.config and replace a value called server_name with whatever the input from the user is. Here is what I have:
#echo off
SET drive=
SET /P drive=Please enter the drive:
SET folder=
SET /P folder=Enter name of folder desired:
SET server=
SET /P server=Enter name of new server:
#echo off > newfile.txt
setLocal EnableDelayedExpansion
if exist newfile.txt del newfile.txt
for /f "tokens=* delims= " %%a in (%drive%\test\%folder%\importer.config) do (
set str=%%a
set str=!str:server_name=%server%!
echo !str! >> newfile.txt
)
del importer.config
rename newfile.txt importer.config
pause
Every time I run this, the cmd prompt shows:
The system cannot find the file specified c:\test\users_input_they_entered\importer.config. The issue is that file is there so trying to understand what I am missing and why it cant find the file that does exist.
It then also states "Could not find c:\windows\system32\importer.config" which not sure why that happens as well
I have searched on stackoverflow, but cannot figure this out with any assistance.
You're pushing your luck using the for loop for that.
A tool like sed would work well.
If you look at this post they have a vbscript implementation that you could use
Is there any sed like utility for cmd.exe
set input_file=importer.config
set output_file=temp.config
set new_server_name=server1984
cscript /Nologo sed.vbs s/server_name/%new_server_name%/ < %input_file% > %output_file%

Write batch file to read a number from a text file and execute the command with that number

I have a text file and a .bat file. Int the text file I have a list of workstation numbers like:
CG002681
CG002526
CG002527
CG002528
CG002529
CG002530
....
so I need to read this text file and i need to excute the command as shown below.
copy "\\cg002009\c$\Documents and Settings\All Users\Application Data\abc\LM\I4S.INI" c:\asd\mul.txt
echo cg002009 >> c:\asd\Shashi.txt
type c:\asd\mul.txt >> c:\asd\345.txt \l
I need execute this command for each workstation.
You can use the for command:
for /F %F in (test.txt) do echo %F
will print each line in file test.txt to the console.
for /F "delims= " %%i in (workstation.txt) do call handle.bat %%i
in handle.bat first param (%1) will be name of workstation there you can do all work (copy, echo and so on)

Resources