batch file to open txt replace text save and close - powershell

I understand that this community is for helping people and I'm not one hundred percent how to ask for someone to do a commissioned job or w/e but I'd like a series of batch files for changing certain numbers to a particular number per batch file.
echo off
if exist %application.yml del %application.yml
for /F "delims=" %%l in (%1) do (
set "line=%%l"
set "line=%line:specific text=another word%"
echo/%line%>> %application.yml)'
How would I set this to replace an array of numbers to the set number, I'm sorry I'm a complete idiot with this.
File itself application.yml
product:
# Cntrl alt 1 Mini, 2 m16, 3 scar, 4 m4169, 5 akm, 6 ump,7 sks
discordUsername:
discordPassword:
maxCPUsUsage: 4
shutdownKey: 0x7B
reloadConfigKey: 0x79
screenResolutionX: 1920
screenResolutionY: 1080
playerEsp: true
playerEspColor: 0xFF00FF
playerEspPointSize: 1
radarEsp: true
radarEspColor: 0x0000FF
radarEspPointSize: 2
vehicleEsp: true
vehicleEspColor: 0xff0000
vehicleEspPointSize: 1
vehicleEspHotkey: 0x2D
lootEsp: true
lootEspColor: 0x00FF00
lootAirdropEspColor: 0xffffff
lootEspPointSize: 1
lootEspHotkey: 0x24
redrawTime: 0
reloadDataTime: 2000
aimEnabled: true
aimKey: 0x39
aimFOV: 95
aimFrequency: 6
aimPrecision: 1
aimSpeed: 1
bulletSpeed: 990
aimHitbox: 0
# Key list: https://msdn.microsoft.com/en-us/en-en/library/windows/desktop/dd375731(v=vs.85).aspx
So essentially the number I wish to replace is the bullet speed and it could be multiple numbers from previous changes ect, for example
990
870
400
and if it is any of these instances I wish to replace it with
900
and so on and so forth.
So it searches for any of the possible numbers and changes it to the specified number and save the file under the same file name.
Thanks in advance for any help if I'm being completely honest in the example I dont even know what to change to make it simply replace one number to the other never mind multiple instance searches :)
Once again ty for any advice or help.

The following commented .bat script should do the job, although empty lines are not copied to output file:
#ECHO OFF
SETLOCAL EnableExtensions
rem redirect all output to a brand new file
>application.yml (
rem parse a file (supplied file name %1 must not be equal to "application.yml")
for /F "usebackq delims=" %%l in ("%~1") do (
rem parse each line
for /F "tokens=1* delims=:" %%L in ("%%~l") do (
if /I "%%~L"=="bulletSpeed" (
rem modified line
echo(%%L: 900
) else (
rem original line
echo(%%l
)
)
)
)
BTW, in the original script, you would need Delayed Expansion to make proper reference to variables (re)defined within a parenthesised code block (like a FOR loop body), e.g. as follows:
echo off
SETLOCAL EnableExtensions EnableDelayedExpansion
if exist application.yml del application.yml
for /F "delims=" %%l in (%1) do (
set "line=%%l"
set "line=!line:specific text=another word!"
echo/!line!>>application.yml
)
Resources (required reading):
(command reference) An A-Z Index of the Windows CMD command line
(helpful particularities) Windows CMD Shell Command Line Syntax
(%~L etc. special page) Command Line arguments (Parameters)
(special page) EnableDelayedExpansion
(>>, > etc. special page) Redirection

Regular Expressions are ideal to do this sort of editing.
Aside from findstr's limited capabilities batch is lacking RE support.
This wrapped powershell one liner will do the job either on cmd line or in a batch-file.
powershell -NoP -C "(gc application.yml) -Replace '(?<=bulletSpeed: )\d+','900'|sc application.yml"
It uses a lookbehind assertion to replace any number following bulletSpeed: with 900.
To limit the replacement to one of previous 990, 870, 400 use this:
powershell -NoP -C "(gc application.yml) -Replace '(?<=bulletSpeed: )(990|870|400)','900'|sc application.yml"

Related

rename multiple files in order with command prompt

I have some files with different names.
Leviathan.txt,Dragon.txt and so on
I wanted to turn it into a digit begins
1.txt,2.txt,3.txt,4.txt and so on
how to perform like other language by using For and function that can pass amount files in folder?
my code so far i know is dir and ren. and i stuck now.
ren *.txt 1.txt
Next code snippet could work for you (save with .bat extension); note that rename command is echoed merely for debugging purposes:
#echo off
SETLOCAL enableextensions enabledelayedexpansion
set /A "ii=0"
pushd "working_directory_here"
for /F "delims=" %%G in ('dir /B /ON "*.txt" 2^>NUL') do (
set /A "ii+=1"
echo ren "%%~G" "!ii!%%~nxG"
)
popd
If you insist on an one-liner (launch in proper working directory):
cmd /E:ON /V:ON /K (#echo off^&set /A "ii=0" ^>NUL^&for /F "delims=" %G in ('dir /B /ON "*.txt" 2^^^>NUL') do (set /A "ii+=1" ^>nul^&echo ren "%~G" "!ii!%~nxG"))^&exit
Resources (required reading):
(command reference) An A-Z Index of the Windows CMD command line
(additional particularities) Windows CMD Shell Command Line Syntax
(%~G etc. special page) Command Line arguments (Parameters)
(EnableDelayedExpansion) Setlocal EnableDelayedExpansion
(^>, %% etc.) Syntax : Escape Characters, Delimiters and Quotes
Assuming none of your existing files are already named something like n.txt, where n is a number, then simply CD to your folder, and run the following command from the command line:
for "tokens=1* delims=:" %A in ('dir /b *.txt^|findstr /n "^"') do #ren "%B" "%A.txt"
Double up the percents if you use the command within a batch script.
EDIT
I forgot about my JREN.BAT utility - a regular expression renaming utility. It is pure script (hybrid JScript/batch) that runs natively on any Windows machine from XP onward.
JREN has a built in ability to incorporate a number into each new file name, and as an added bonus, it can left pad the number with zeros so that a DIR command lists the files in numerical order. The default numeric width is 3 digits, so files would be like "001.txt", "002.txt', ... "010.txt", ... "100.txt", etc.
jren "^.*" "$n+'.txt'" /j /fm *.txt
The /NPAD option specifies the minimum numeric width, so NTAB 1 produces no padding, which is what the original question asked for.
jren "^.*" "$n+'.txt'" /j /fm *.txt /npad 1
Since JREN is a batch script itself, you must use CALL JREN if you put the command within another batch script.
Full documentation is available from the command prompt via jren /? | more. My console window is configured with a large buffer, so I can scroll back to see prior output, and I don't bother with piping the help to MORE.

Syntax of the "for" command: whats wrong with line 10 in following batch file?

1 echo off
2
3 echo Please enter a date
4 set /p a=
5 echo %a%
6 for /D %%d in (M:\Serienbriefauftrag\*) do (
7 if not exist %%d\Erledigt\*.bat echo %%d
8 )
9 pause
10 for /d %%b in (dir M:\Serienbriefauftrag\%%d /T:C)
11 echo %%b
12 pause
The Code works fine until line 10 (syntax error) the aim is, to get the creation date of the folder compare it with "%a%" and and if it's under the entered date "%a%", the folder should be moved. but somehow...
found a way to get the creation date
if not exist %%d\Erledigt\*.bat echo %%~td|findstr /i /l
the new task is to make the output "calculateable"
Two things:
1) In line 10 %%d is undefined (empty), since the scope of the first loop (where it is defined) is left in line 8.
You can set another variable to %%d to be able to use it after the loop ends, but be careful. In batch using set in for loops is a bit tricky. To get around the aweful use of delayed variable expansion, I would suggest to you staying inside of the loop while doing your work.
#echo off
set /p a=Please enter a date:
for /d %%d in (M:\Serienbriefauftrag\*) do (
if not exist %%d\Erledigt\*.bat (
:: Do whatever you want with %%d in here
echo "%%d"
)
)
pause
2) The syntax of your second loop is wrong. You are missing the do before the loop body and /d lets the loop iterate over all folders in a specified directory. dir M:\Serienbriefauftrag\%%d /T:C is not a directory but a command.
If you want to iterate over the output of that command, you have to use /f and put the contents of the paranthesis in single quotes, like that ('dir M:\Serienbriefauftrag\%%d /T:C').

eol unix to windows in command line .bat batch

I am trying to convert text file eol to windows format from unix on windows xp machine using command line (batch file). how do I do that? what is the command for that? thanks.
This simple script is fast and works great except it converts every TAB character into 8 spaces. The number of spaces can be modified with the MORE /T option, but there is no way to preserve the TAB characters. Pass the file name (optionally with path) as the one and only argument.
#echo off
more %1 >%1.new
move /y %1.new %1 >nul
All that is needed is to read and echo each line. The FOR /F command is perfect, except it ignores empty lines. Here I use FINDSTR to prefix each line with the line number, followed by a :, thus preserving empty lines. Then I use search and replace to remove the number prefix. I must toggle delayed expansion on and off within the loop to preserve any ! that may appear. This script preserves TABs, but is limited to ~8191 bytes per line. It is also relatively slow. It will become very slow with very large files.
#echo off
setlocal disableDelayedExpansion
>%1.new (
for /f "delims=" %%A in ('findstr /n "^" %1') do (
set "ln=%%A"
setlocal enableDelayedExpansion
echo(!ln:*:=!
endlocal
)
)
move /y %1.new %1 >nul
Finally, here is a hybrid batch/JScript solution that is very fast, and does not have any limitations that I am aware of.
#if (#X)==(#Y) #end /* Harmless hybrid line that begins a JScript comment
::************ Batch portion ***********
#echo off
<%1 cscript //E:JScript //nologo "%~f0" >%1.new
move /y %1.new %1 >nul
exit /b
************* JScript portion **********/
while (!WScript.StdIn.AtEndOfStream) {
WScript.Stdout.WriteLine(WScript.StdIn.ReadLine());
}

DOS batch command to read some info from text file

I am trying to read some info from a text file by using windows command line, and save it to a variable just like "set info =1234"
Below is the content of the txt file, actually I just need the revision number, and the location of it is always the same line 5, and from column 11 to 15. In the sample it's 1234, and I am wondering is there a way to save it to a variable in Dos command line.
Thanks a lot!
svninfo.txt:
Path: .
URL: https://www.abc.com
Repository Root: https://www.abc.com/svn
Repository UUID: 12345678-8b61-fa43-97dc-123456789
Revision: 1234
Node Kind: directory
Schedule: normal
Last Changed Author: abc
Last Changed Rev: 1234
Last Changed Date: 2010-04-01 18:19:54 -0700 (Thu, 01 Apr 2010)
Here's a one line version:
for /f "tokens=2" %%i in ('findstr Revision: input.txt') do set revision=%%i
findstr is used to filter the file. It will print "input.txt:Revision: 1234"
Then the "tokens=2" means that we are interested in the second token, "1234". By default for breaks on white space.
The following code snippet shows how to do this:
#echo off
setlocal enableextensions enabledelayedexpansion
set revision=
for /f "delims=" %%a in (input.txt) do (
set line=%%a
if "x!line:~0,10!"=="xRevision: " (
set revision=!line:~10!
)
)
echo !revision!
endlocal
Its output is 1234 as desired.
The setlocal is what I use in every script to ensure variables are treated in a known way. The for statement processes each line in the input file (the delims bit stops the line from being tokenised into separate words).
The !line:~ bits are substrings with !line:~0,10! being the first ten characters and !line:~10! being the rest.
So, basically, it checks every line to see if it starts with "Revision: " and, if so, extracts the rest of the line for later.
Use the for command to parse the file:
for /f "skip=4 tokens=2" %%l in (svninfo.txt) do (
set revision=%%l
goto gotrev
)
:gotrev
echo revision is %revision%
if you have can use GNU tools, such as gawk
#echo off
for /F %%a in ('gawk -F":" "$1~/Revision/{print $2}" file') do (
set var=%%a
)
echo %var%
Knowing how to use CMD scripting deftly is great, but using PowerShell is even better. Here's one way to do it:
$revision = (( gc .\svninfo.txt | ? { $_.StartsWith( 'Revision: ' ) } ) -split ' ')[1]
What's going on?
$revision is a variable
gc is Get-Content, aka type. Each line in the file becomes a string in a sequence (pipeline).
? is Where-Object. It filters the objects in a pipeline based on a condition.
{} delimits a script block
$_ is the current object in the pipeline.
-split invokes String.Split() in .NET, giving you an array of objects
[1] indexes in to the array

MS DOS edit a file

I am writing a batch script which I wish to open a file and then change the second line of it. I want to find the string "cat" and replace it with a value that I have SET i.e. %var% . I only want this to happen on the second line (or for the first 3 times). How would you go about doing this?
I just solve it myself. It will lookup var on line two only.
#echo OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SET filename=%1
set LINENO=0
for /F "delims=" %%l in (%filename%) do (
SET /A LINENO=!LINENO!+1
IF "!LINENO!"=="2" ( call echo %%l ) ELSE ( echo %%l )
)
But I prefer using cscript (vbscript or even jscript).
First of all, using a batch file to achieve this, is messy (IMHO). You will have to use an external tool anyway to do the string replacement. I'd use some scripting language instead.
If you really want to use a batch, this will get you started.
This would be ugly to do with native batch scripting. I would either
Do this in VBScript. If you really need this in a batch file, you can call the VBScript file from the batch script. You can even pass in %var% as an argument to the VBScript.
Use a sed script. There are windows ports of Unix commands like GnuWin32, GNU Utilities for Win32 (I use these), or Cygwin.
I would create a script that would:
scan the input file
write to a second output file
delete the input
rename the output
As far as the dos commands to parse, I did a Google Search and came up with a good starting point:
#echo off
setlocal enabledelayedexpansion
set file=c:\file.txt
set output=output.txt
set maxlines=5000
set count=0
for /F "tokens=* usebackq" %%G in ("%file%") do (
if !count!==%maxlines% goto :eof
set line=%%G
set line=!line:*000000000000=--FOUND--!
if "!line:~0,9!"=="--FOUND--" (
echo %%G>>"%output%"
set /a count+=1
)
)
(Stolen from teh Intarwebnet)