Adding strings at certain places in a text file - batch-file

I have a text file with the following contents:
-849.4471 1272.173 22.8698 0 0 -1 7.54979E-008 Fire_Esc_6 385 792 24 -1
-837.0507 1270.862 28.1249 0 0 -1 7.54979E-008 Fire_Esc_6b 385 792 24 -1
-837.0654 1270.879 24.09248 0 0 -1 7.54979E-008 Fire_Esc_6 385 792 24 -1
For each of the lines, I need to
add setAttr "sth"; to the beginning of the line
add sth between the first and second numbers
delete everything on the line from 385 through the end of the line
I'm a total beginner in batch and have no idea where to start. Any help you can give you be greatly appreciated.
I've done until here by somethingDark's help :0
FOR /F "tokens=8* delims= " %%G IN (C:\Users\Sherlock\Documents\3DReaperDX\Frames\1.txt) DO ECHO set %%G >12.txt

Since you've made an attempt to solve this yourself, I feel better about showing you what I was imagining for your script.
#echo off
:: If 12.txt exists, delete it. This way, the entire file will be recreated when the script is re-run.
:: (If you don't want this to happen and you just want new data added to the end of the file every
:: time the script is run, just delete this part.)
if exist 12.txt del 12.txt
:: An example line looks like this:
:: -849.4471 1272.173 22.8698 0 0 -1 7.54979E-008 Fire_Esc_6 385 792 24 -1
:: Iterate through each line in 1.txt, storing each space-delimited string in a unique variable
:: %%A: -849.4471
:: %%B: 1272.173
:: %%C: 22.8698
:: %%D: 0
:: %%E: 0
:: %%F: -1
:: %%G: 7.54979E-008
:: %%H: Fire_Esc_6
:: Since we don't care about anything after the eighth token, we can just ignore it
:: The redirection command is at the start of the line to avoid an extra space at the end of the line
for /f "tokens=1-8" %%A in (C:\Users\Sherlock\Documents\3DReaperDX\Frames\1.txt) do >>12.txt echo setAttr "sth"; %%A sth %%B %%C %%D %%E %%F %%G %%H
Since this script is so short (it's really just three lines with a whole bunch of comments), you could even run this one-liner from the command line:
for /f "tokens=1-8" %A in (C:\Users\Sherlock\Documents\3DReaperDX\Frames\1.txt) do >>12.txt echo setAttr "sth"; %A sth %B %C %D %E %F %G %H
This will create the file 12.txt with the contents
setAttr "sth"; -849.4471 sth 1272.173 22.8698 0 0 -1 7.54979E-008 Fire_Esc_6
setAttr "sth"; -837.0507 sth 1270.862 28.1249 0 0 -1 7.54979E-008 Fire_Esc_6b
setAttr "sth"; -837.0654 sth 1270.879 24.09248 0 0 -1 7.54979E-008 Fire_Esc_6

Related

Fetch a specific column values from a .txt file with pipe delimiter and load into a new text file using batch script

I have a text file with N number of rows and columns, whereas I need to get particular columns with their values and load it into a new text file using batch script, e.g.:
input.txt
col1|col2|col3.....col71|col72
ew|ds|343.....csdk|gfdf
xc|gh|657.....sdfs|utyy
qw|zx|345.....ffds|xzcz
output.txt
col71|col3
csdk|343
sdfs|657
ffds|345
To split text into tokens by (a) certain delimiter(s), use the for /F loop. However, this can only handle up to 31 tokens, so you cannot simply state tokens=71, but you can nest multiple loops:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
> "output.txt" (
rem // Split off the first 31 tokens, pass the rest to the next loop:
for /F "usebackq delims=| eol=| tokens=3,31*" %%A in ("input.txt") do (
rem // Split off the next 31 tokens, pass the rest to the next loop:
for /F "delims=| eol=| tokens=31*" %%D in ("%%C") do (
rem /* Extract the proper token from the remaining ones (remember
rem that 31 + 31 = 62 tokens have been split off before): */
for /F "delims=| eol=| tokens=9" %%F in ("%%E") do (
echo(%%F^|%%A
)
)
)
)
endlocal
If there may be empty columns, the above approach fails, because for /F treats consecutive delimiters as one. To overcome this, you could do the following:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
> "output.txt" (
rem // Read complete lines:
for /F usebackq^ delims^=^ eol^= %%L in ("input.txt") do (
rem // Store current line string in interim variable:
set "LINE=%%L"
setlocal EnableDelayedExpansion
rem /* Split off the first 31 tokens, pass the rest to the next loop;
rem to avoid consecutive delimiters `|`, replace every single one by
rem :`"|"`, so `||` becomes `"|""|"`; then enclose the entire result
rem within `""`, thus achieving individual tokens enclosed within `""`: */
for /F "delims=| tokens=3,31*" %%A in (^""!LINE:|="^|"!"^") do (
endlocal
rem // Split off the next 31 tokens, pass the rest to the next loop:
for /F "delims=| tokens=31*" %%D in ("%%C") do (
rem /* Extract the proper token from the remaining ones (remember
rem that 31 + 31 = 62 tokens have been split off before): */
for /F "delims=| tokens=9" %%F in ("%%E") do (
rem // Remove the previously added surrounding `""` by `~`:
echo(%%~F^|%%~A
)
)
setlocal EnableDelayedExpansion
)
endlocal
)
)
endlocal
This approach will still fail if there are already quoted field values that contain | on their own.
Linux
You could use awk -F "|" '{ print $70 "|" $2 }' input.txt > output.txt.
Usually one would probably execute cut -d"|" -f2,70 input.txt > output.txt, the only problem is that cut (as far as I know) doesn't support reordering columns.
Powershell
On Windows' powershell (also available for Linux) you can use the following snippet:
Get-Content 'input.txt' | ForEach-Object {
$array = $_.split("|")
$array[70] + '|' + $array[2]
} | Out-File 'output.txt'
The following Batch file is a general-purpose program that use a series of nested FOR /F commands that allows access to up to 177 tokens, but in a very simple way:
#echo off
setlocal EnableDelayedExpansion
rem Method to use up to 177 tokens in a FOR /F command in a simple way
rem Antonio Perez Ayala
rem Create an example file with lines with 180 tokens each
(for %%a in (A B C) do (
set "line="
for /L %%i in (1,1,180) do set "line=!line! %%a%%i"
echo !line!
)) > test.txt
set "line="
rem Load the string of tokens characters from FOR-FcharsCP850.txt file
chcp 850 > NUL
if exist FOR-FcharsCP850.txt goto readChars
echo Creating FOR-F characters file, please wait...
set "options=/d compress=off /d reserveperdatablocksize=26"
type nul > t.tmp
> FOR-FcharsCP850.txt (
set /P "=0" < NUL
rem Create 87 characters in 38..124 range for 3 FOR's with "tokens=1-28*"
set "i=0"
for /L %%i in (38,1,124) do (
set /A i+=1, mod=i%%29
if !mod! neq 0 (
call :genchr %%i
type %%i.chr
del %%i.chr
)
)
rem Create 95 characters for 3 FOR's with "tokens=1-31*"
rem This is the tokens sequence used when code page = 850
set "i=0"
for %%i in (173 189 156 207 190 221 245 249 184 166 174 170 240 169 238 248
241 253 252 239 230 244 250 247 251 167 175 172 171 243 168 183
181 182 199 142 143 146 128 212 144 210 211 222 214 215 216 209
165 227 224 226 229 153 158 157 235 233 234 154 237 232 225 133
160 131 198 132 134 145 135 138 130 136 137 141 161 140 139 208
164 149 162 147 228 148 246 155 151 163 150 129 236 231 152 ) do (
set /A i+=1, mod=i%%32
if !mod! neq 0 (
call :genchr %%i
type %%i.chr
del %%i.chr
)
))
del t.tmp temp.tmp
set "options="
:readChars
set /P "char=" < FOR-FcharsCP850.txt
set "lastToken=177"
cls
echo Enter tokens definition string in the same way of FOR /F "tokens=x,y,m-n" one
echo/
echo You may define a tokens range in descending order: "tokens=10-6" = 10 9 8 7 6
echo or add an increment different than 1: "tokens=10-35+5" = 10 15 20 25 30 35
echo Combine them: "tokens=10,28-32,170-161-3" = 10 28 29 30 31 32 170 167 164 161
echo/
echo The maximum token number is 177
:nextSet
echo/
set /P "tokens=tokens="
if errorlevel 1 goto :EOF
rem Expand the given tokens string into a series of individual FOR tokens values
set "tokensValues="
for %%t in (%tokens%) do (
for /F "tokens=1-3 delims=-+" %%i in ("%%t") do (
if "%%j" equ "" (
if %%i leq %lastToken% set "tokensValues=!tokensValues! %%!char:~%%i,1!"
) else (
if "%%k" equ "" (set "k=1") else set "k=%%k"
if %%i gtr %%j set "k=-!k!"
for /L %%n in (%%i,!k!,%%j) do if %%n leq %lastToken% set "tokensValues=!tokensValues! %%!char:~%%n,1!"
)
)
)
rem First three FOR's use as tokens the ASCII chars in 38..124 (&..|) range: 28*3 = 84 tokens + 3 tokens for next FOR
rem Next three FOR's use as tokens Extended chars: 31*3 = 93 tokens + 2 tokens for next FOR
rem based on the tokens sequence used when code page = 850
rem Total: 177 tokens
for /F "eol= tokens=1-28*" %%^& in (test.txt) do ^
for /F "eol= tokens=1-28*" %%C in ("%%B") do ^
for /F "eol= tokens=1-28*" %%` in ("%%_") do ^
for /F "eol= tokens=1-31*" %%­ in ("%%|") do ^
for /F "eol= tokens=1-31*" %%µ in ("%%·") do ^
for /F "eol= tokens=1-31" %%  in ("%%…") do (
call :getTokens result=
rem Process here the "result" string:
echo !result!
)
goto nextSet
:getTokens result=
for %%# in (-) do set "%1=%tokensValues%"
exit /B
REM This code creates one single byte. Parameter: int
REM Teamwork of carlos, penpen, aGerman, dbenham
REM Tested under Win2000, XP, Win7, Win8
:genchr
if %~1 neq 26 (
makecab %options% /d reserveperfoldersize=%~1 t.tmp %~1.chr > nul
type %~1.chr | ( (for /l %%N in (1,1,38) do pause)>nul & findstr "^" > temp.tmp )
>nul copy /y temp.tmp /a %~1.chr /b
) else (
copy /y nul + nul /a 26.chr /a >nul
)
goto :eof
IMPORTANT: The series of six nested FOR /F commands use the following ASCII characters in the replaceable parameter and the character between quotes:
for /F "eol= tokens=1-28*" %%^& in (test.txt) do ^ %%^38
for /F "eol= tokens=1-28*" %%C in ("%%B") do ^ %%67 in ("66")
for /F "eol= tokens=1-28*" %%` in ("%%_") do ^ %%96 in ("95")
for /F "eol= tokens=1-31*" %%­ in ("%%|") do ^ %%173 in ("124")
for /F "eol= tokens=1-31*" %%µ in ("%%·") do ^ %%181 in ("183")
for /F "eol= tokens=1-31" %%  in ("%%…") do ( %%160 in ("133")
However, it seems that some web browser don't correctly copy-paste some extended characters. If the program don't works correctly, you should check that these characters were correctly copied and fix they if necessary. You may try to copy the lines above (in pink background) and test if they were correctly copied...
Output example:
Enter tokens definition string in the same way of FOR /F "tokens=x,y,m-n" one
You may define a tokens range in descending order: "tokens=10-6" = 10 9 8 7 6
or add an increment different than 1: "tokens=10-35+5" = 10 15 20 25 30 35
Combine them: "tokens=10,28-32,170-161-3" = 10 28 29 30 31 32 170 167 164 161
The maximum token number is 177
tokens=10-6
A10 A9 A8 A7 A6
B10 B9 B8 B7 B6
C10 C9 C8 C7 C6
tokens=10-35+5
A10 A15 A20 A25 A30 A35
B10 B15 B20 B25 B30 B35
C10 C15 C20 C25 C30 C35
tokens=10,28-32,170-161-3
A10 A28 A29 A30 A31 A32 A170 A167 A164 A161
B10 B28 B29 B30 B31 B32 B170 B167 B164 B161
C10 C28 C29 C30 C31 C32 C170 C167 C164 C161
tokens=71,3
A71 A3
B71 B3
C71 C3
If your application requires less than 177 tokens, you may modify this program and eliminate the code sections of the not required tokens; that is, with 2 FOR's you may access up to 56 tokens, with 3 up to 84, with 4 up to 115, and with 5 up to 146.
You may review a detailed explanation of this method here; you may also download (a previous version of) this program in a .zip file from this post that would allow to fix the problem of the extended characters in the six FOR /F commands in a simple way...

Format a hexadecimal sequence in a cmd.exe batch file

In a Windows cmd script (aka bat script), I have a FOR /L loop from 1 to 8, where I need to do a bit shift and somehow format a variable as a hexadecimal number (which if you ask, is a single CPU identifier bit to feed into /AFFINITY).
I can't figure out how to do the last step. This is my loop.cmd file:
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /L %%i IN (1,1,8) DO (
SET /A "J=1<<%%i"
ECHO %%i and !J!
)
which does everything but format a hex number:
1 and 2
2 and 4
3 and 8
4 and 16
5 and 32
6 and 64
7 and 128
8 and 256
expected output is:
1 and 2
2 and 4
3 and 8
4 and 10
5 and 20
6 and 40
7 and 80
8 and 100
How do you format a hexadecimal number?
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /L %%i IN (1,1,8) DO (
SET /A "J=1<<%%i"
CALL :DECTOHEX J
ECHO %%i and !J!
)
GOTO :EOF
:DECTOHEX VAR
SET "DEC=!%1!"
SET "HEX="
:NEXT
SET /A DIGIT=DEC%%16, DEC/=16
SET "HEX=%DIGIT%%HEX%"
IF %DEC% NEQ 0 GOTO NEXT
SET "%1=%HEX%"
EXIT /B
EDIT: Reply to the comment
Previous solution works correctly when the shifted value have just one bit on, as stated in the question. If the shifted value may have several bits on then a more general decimal-to-hexadecimal conversion is required, like the one below:
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
REM DEFINE THE HEXA DIGITS
SET "HEXA=0123456789ABCDEF"
FOR /L %%i IN (1,1,8) DO (
SET /A "J=3<<%%i"
CALL :DECTOHEX J
ECHO %%i and !J!
)
GOTO :EOF
:DECTOHEX VAR
SET "DEC=!%1!"
SET "HEX="
:NEXT
SET /A DIGIT=DEC%%16, DEC/=16
SET "HEX=!HEXA:~%DIGIT%,1!%HEX%"
IF %DEC% NEQ 0 GOTO NEXT
SET "%1=%HEX%"
EXIT /B
#echo off
setlocal enabledelayedexpansion
set x=2
set n=1
set /a result=n
for /l %%a in (1,1,10) do (
set /a result*=x
if "!result:~0,1!"=="1" set result=!result:16=10!
echo %%a and !result!
)
output:
1 and 2
2 and 4
3 and 8
4 and 10
5 and 20
6 and 40
7 and 80
8 and 100
9 and 200
10 and 400

Batch file - dynamically display huge list of variables?

I want to make a 100x100 grid of variables that are similar to this (name of variables):
1x1 1x2 1x3
2x1 2x2 2x3
3x1 3x2 3x3
And so on, all the way to 100x100. Is there an easy way to do this without having to type in echo for 10,000 variables, and just use some for loop?
Keep in mind I'm wanting to display a row of 100, then the next row of 100, so they're not all in one long list, unless I set the mode to 100x100? I know this is really strange, but I'm trying to see if I can make a graphing function within batch files.
Thank you.
EDIT:
How can I use the for /L loop to echo multiple variables in one line?
for /L %%a in (0, 1, 5) do echo %%a
Desired output:
0 1 2 3 4 5
Actual output:
0
1
2
3
4
5
You may concatenate several values in the same variable (that represent a row of values), and then just show it in a simple echo command:
#echo off
setlocal EnableDelayedExpansion
rem Get NxM dimensions from Batch file parameters
set /a N=%1, M=%2
rem Create the two-dimensional array with 4-digits random numbers
for /L %%i in (1,1,%N%) do (
for /L %%j in (1,1,%M%) do (
set /A "number=!random! %% 10000"
set "number= !number!"
set "a%%ix%%j=!number:~-4!"
)
)
rem Show the array line by line
for /L %%i in (1,1,%N%) do (
set "line="
for /L %%j in (1,1,%M%) do (
set "line=!line! !a%%ix%%j!"
)
echo !line!
)
Output example:
C:\> test.bat 10 15
4216 3058 9311 5626 1461 464 3926 3597 5312 5074 2797 7654 3306 5763 3359
1203 8313 8271 3591 3588 2415 6424 9730 8095 5958 8599 3062 4165 6671 6192
7140 9204 60 8649 9962 3374 1690 3500 331 6314 2579 3194 8451 6682 3202
2275 6582 877 8424 3732 2152 6741 1791 2544 2979 4763 1949 3282 5284 2578
9628 2193 4806 8505 3480 2517 6596 9029 2776 2377 6105 3007 8464 3826 2090
281 2278 2559 7318 3207 500 98 2061 8572 4653 9646 6815 5218 2067 2512
9862 8686 3945 5059 1191 947 9589 1983 8213 8246 408 5458 3286 7890 1280
1297 6154 8701 5214 769 1305 1946 3172 5201 5245 2113 2865 5866 8864 6476
1760 3050 3014 8195 1325 4029 2302 9466 5002 2622 741 8665 7090 9580 3388
5245 7004 9264 1708 4173 9041 8462 2055 9215 3809 2362 4400 1308 3411 5677
Here's how to get your desired output for your example above. Note that the quotes are not required, but recommended so that you are aware of the trailing space after %%a.
for /L %%a in (0, 1, 5) do <nul set/p "=%%a "

Merge Two text files line by line using batch script

I have 2 text files; A.txt and B.txt and I want to merge them to make C.txt using a batch script.
However (here's the tricky part) I wish to do it so each line from A.txt is appended with a space then the first line from B.txt then a new line with the first line from A and the second from B and so on until the end of B is reached and then we start with the second line of A.
I know I haven't worded this well so here's an example:
A.txt;
1
2
3
4
5
B.txt;
Z
Y
X
W
V
T
R
So C.txt would have;
1 Z
1 Y
1 X
1 W
1 V
1 T
1 R
2 Z
2 Y
etc.
#echo off
for /f "delims=" %%a in (a.txt) do (
for /f "delims=" %%b in (b.txt) do (
>>c.txt echo %%a %%b
)
)

Creating a word list using batch

I want to make a batch file that prints out to a text file a list of combination of numbers and letters.
Using {0, ..., 9, a, ..., z, A, ..., Z} as my character pool, I have 62 unique characters.
The word length starts as 1 and increases up to a predetermined value.
The script starts at length = 1 and prints out 0 to Z.
Then it proceeds to length = 2 and prints out 00 to ZZ, and so on...
Here is an iterative solution that is much faster.
No need for CALL.
Each permutation is only generated once.
I was able to generate up to length 4 with over 15 million permutations in less than 5 minutes.
#echo off
setlocal enableDelayedExpansion
set chars=0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
set maxPos=61
del output.txt 2>nul
>prior.txt echo(""
for /l %%I in (1 1 %1) do (
>new.txt (
for /f %%A in (prior.txt) do for /l %%N in (0 1 %maxPos%) do echo(%%~A!chars:~%%N,1!
)
type new.txt>>output.txt
move /y new.txt prior.txt >nul
)
del prior.txt
Perhaps this is what you want?
TEST.BAT
#echo off
set charPool=_0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
set charLen=62
(for /L %%a in (1,1,%1) do (
set permutation=
call :makePermutation %%a
)) > textfile.txt
goto :EOF
:makePermutation level
setlocal EnableDelayedExpansion
set lastPermutation=%permutation%
for /L %%i in (1,1,%charLen%) do (
set permutation=!lastPermutation!!charPool:~%%i,1!
if %1 gtr 1 (
set /A newLevel=%1-1
call :makePermutation !newLevel!
) else (
echo(!permutation!
)
)
exit /B
The batch file must be started with a number as parameter which is the unit length.
For example on using TEST.BAT 1 the text file textfile.txt contains 62 lines.
Note that TEST.BAT 2 generates 3906 combinations (strictly speaking, permutations in statistical sense) from 0 to ZZ, and TEST.BAT 3 generates 242234 combinations from 0 to ZZZ!
Calculation example for estimating the number of strings in text file (size of file):
Running TEST.BAT with 5 as parameter produces
62 ^ 5 + 62 ^ 4 + 62 ^ 3 + 62 ^ 2 + 62 ^ 1 = 931.151.402
strings in text file.

Resources