I want to remove all spaces and tabs from a txt file. Below is my code with which I am trying to do this:
for /F "delims=" %%a in (CODE_CHECK.txt) do (
set one=%%a
set one=%one:=%
) >> CODE_CHECK_2.txt
Example file lines:
ONE VIEW
TWO PACKEGE BODY
After the code it should be:
ONEVIEW
TWOPACKEGEBODY
Given the file holds less than 64K lines with a length less than 8K bytes/characters each, empty lines do not need to be preserved, and the file is ASCII/ANSI-encoded with DOS/Windows-style line-breaks, you could to the following:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Write output to another file:
> "CODE_CHECK_2.txt" (
rem /* Read file by `more` which replaces TABs by SPACEs;
rem then parse the output by `for /F` (skipping empty lines): */
for /F delims^=^ eol^= %%L in ('more "CODE_CHECK.txt"') do (
rem // Store current (non-empty) line:
set "LINE=%%L"
rem /* Toggle delayed expansion to be able to write and read
rem a variable in the same block of code and to preserve `!`: */
setlocal EnableDelayedExpansion
rem // Replace every SPACE by nothing, hence remove it:
echo(!LINE: =!
endlocal
)
)
endlocal
exit /B
Here is an approach that preserves empty lines (the remaining restrictions still apply though):
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Write output to another file:
> "CODE_CHECK_2.txt" (
rem /* Read file by `more` which replaces TABs by SPACEs;
rem precede every line by line number plus `:` to not appear empty;
rem then parse the output by `for /F` (which would skip empty lines): */
for /F "delims=" %%L in ('more "CODE_CHECK.txt" ^| findstr /N "^"') do (
rem // Store current line:
set "LINE=%%L"
rem /* Toggle delayed expansion to be able to write and read
rem a variable in the same block of code and to preserve `!`: */
setlocal EnableDelayedExpansion
rem // Replace every SPACE by nothing, hence remove it:
set "LINE=!LINE: =!"
rem // Remove line number prefix and return result:
echo(!LINE:*:=!
endlocal
)
)
endlocal
exit /B
Assuming you're on a Windows platform, due to your provided code and included tagsā¦
The folowing batch-file example will remove spaces and tabs and also remove any blank lines:
#If Exist "CODE_CHECK.txt" (For /F Delims^=^ EOL^= %%A In ('More /T1 "CODE_CHECK.txt"')Do #Set "$=%%A"&Call Echo(%%$: =%%)>"CODE_CHECK_2.txt"
To maintain any blank lines, using a batch-file, you'd need something more like this:
#If Exist "CODE_CHECK.txt" (For /F "Tokens=1*Delims=]" %%A In ('More "CODE_CHECK.txt"^|Find /V /N ""')Do #Set "$= %%B"&Call Echo(%%$: =%%)>"CODE_CHECK_2.txt"
In this example I've removed the /T1 option for More, I'm unsure if its inclusion is more or less efficient
You could also use powershell for this, (if needed the input and output file can be the same):
(GC 'CODE_CHECK.txt') -Replace '\s',''|SC 'CODE_CHECK_2.txt'
You can also run the powershell version from a batch-file:
#PowerShell -NoP "(GC 'CODE_CHECK.txt') -Replace ' |\t',''|SC 'CODE_CHECK_2.txt'"
In this version, I've used ' |\t', as a possible alternative to '\s'.
Related
I'm trying to extract every value for FileRef from a string I've already extracted out of a file. Unfortunately, the string is one line which makes it more difficult to use for /f "tokens=*".
The string is:
"<Cim:TrnTable_list><Cim:TrnTable Id="Root"><Cim:TrnElem Ref="3" FileRef="A1-FS.elt"/><Cim:TrnElem Ref="4" FileRef="A1-MS.elt"/><Cim:TrnElem Ref="9" FileRef="Product\Product-v1\Product-v1-MD.elt"/><Cim:TrnElem Ref="11" FileRef="Product\Product-v2\Product-v2-MD.elt"/><Cim:TrnElem Ref="12" FileRef="RunnerPart_Assembly#1.elt"/></Cim:TrnTable></Cim:TrnTable_list>"
How to get every value for FileRef inserted into a variable in the following format?:
A1-FS.elt?A1-MS.elt?Product\Product-v1\Product-v1-MD.elt?Product\Product-v2\Product-v2-MD.elt?RunnerPart_Assembly#1.elt
I mean, then I could loop trough them using for /f "delims=?" right?
Or is there a way to convert each ? in the above example to a 'new line' within one string, or maybe even better ideas to loop trough each FileRef-value?
Many thanks!
Squashman is right in his comment, use a language that is capable of handling XML data natively.
Anyway, if you insist on using pure Windows batch scripting, you could assemble a new string with ? symbols as separator like in the following script:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_FILE=%~dpn0.txt" & rem // (path to file containing the line of text)
(set ^"_LF=^
%= empty line =%
^") & rem // (this constitutes a new-line character)
rem // Initialise collection variable:
set "COLL=?"
rem // Read line from file:
for /F "usebackq delims=" %%L in ("%_FILE%") do (
set "LINE=%%~L"
setlocal EnableDelayedExpansion
rem // Replace `><` by `>` + line-break + `<`:
set ^"LINE=!LINE:^>^<=^>^%_LF%%_LF%^<!^"
rem // Read one tag enclosed within `<` and `>`:
for /F "delims=" %%I in ("!LINE!") do (
endlocal
set "ITEM=%%I"
rem // Extract string between ` FileRef` and `/>`:
setlocal EnableDelayedExpansion
set "ITEM=!ITEM:* FileRef=!"
set "ITEM=!ITEM:/>=!"
rem // Check for `=`-sign after `FileRef`:
if "!ITEM:~,1!"=="=" (
rem // Remove leading `=` and surrounding `""`:
for /F "delims=| eol=|" %%F in ("!ITEM:~1!") do (
endlocal
set "NAME=%%~F"
rem // Assemble return string using `?` as separator:
setlocal EnableDelayedExpansion
for /F "delims=| eol=|" %%J in ("!COLL!!NAME!?") do (
endlocal
set "COLL=%%J"
setlocal EnableDelayedExpansion
)
)
)
)
endlocal
)
rem // Return collection variable:
setlocal EnableDelayedExpansion
echo(!COLL:~1,-1!
endlocal
endlocal
exit /B
Toggling delayed expansion is done in order not to have trouble with ! symbols.
Better than collecting all values in a single variable is to just loop through them in my opinion.
This is one other way to brute force this. This code will put each FileRef into its own variable and sequence the variable name up.
#echo off
FOR /F "delims=" %%G IN (line.txt) do set "line=%%G"
set i=0
:loop
set /a i+=1
set "line=%line:*FileRef=%"
FOR /F "tokens=1* delims==/" %%G IN ("%line%") DO (
set "var%i%=%%~G"
set "line=%%H"
)
echo "%line%"|find /I "fileref" >nul 2>&1 &&GOTO loop
set var
pause
When executed it will output this.
C:\BatchFiles\SO\XML>bruteforce.bat
var1=A1-FS.elt
var2=A1-MS.elt
var3=Product\Product-v1\Product-v1-MD.elt
var4=Product\Product-v2\Product-v2-MD.elt
var5=RunnerPart_Assembly#1.elt
Press any key to continue . . .
If you don't want the data assigned into their own individual variables you can just use the %%G meta-variable directly inside the FOR command.
I know similar questions had been asked many time, but I cannot workout something fitting my needs...
So I have a file, containing a secret key, the line is starting like :
SECRET_KEY =
The secret key is a non-fixed length.
I want to find this line, to save it somewhere, to replace the all file containing the secret key by another file (very similar, but with a different secret key which I don't want), and to reintroduce the original secret key in the new file!
So for the moment I have this code:
echo Saving the actual SECRET_KEY...
findstr "SECRET_KEY" somefile.py > SECRET_KEY.txt
echo Copying source code files...
xcopy %~dp0\somepath %~dp0\someotherpath /EY
This part is working, as I have stored the original secret key in a text file, and replaced the file by another (together with other files, directories structures, etc...)
So now how can I find again the line starting by SECRET_KEY and replace the all line by the content of SECRET_KEY.txt?
EDIT:
I made some progress I think:
echo Saving the SECRET_KEY...
findstr "SECRET_KEY" inputfile.py > SECRET_KEY.txt
echo Copying source code files...
xcopy %~dp0\somepath %~dp0\someotherpath /EY
echo Restoring the SECRET_KEY...
setlocal EnableDelayedExpansion
for /F "delims=" %%a in ('findstr /I "SECRET_KEY" SECRET_KEY.txt') do set "secret_key=%%a"
echo !secret_key!
#echo off
set file=inputfile.py
set newline=!secret_key!
set insertline=23
set output=outputfile.py
(for /f "tokens=1* delims=[]" %%a in ('find /n /v "##" ^< "%file%"') do (
if "%%~a"=="%insertline%" (
echo !newline!
) ELSE (
echo.%%b
)
)) > %output%
I can then delete SECRET_KEY.TXT, inputfile.py, and rename outputfile.py into inputfile.py
Most likely not the most efficent way, but it works...
EXCEPT for one thing! I have a lot a special characters in this inputfile, that's why I used !secret_key! for example...
But I also have a lot of:
[some stuff]
and in the outputfile, it appears as:
[some stuff
So I guess I'm close, but I can't find a delimiter which will allow every single character to be outputed!
The following script reads the line containing SECRET_KEY from file otherfile.py at first; then it gets the number of the line of the input file inputfile.py where SECRET_KEY is placed; afterwards it reads the file inputfile.py line by line, check each line number whether it matches the previously found one; if a match is found, the previously gathered key from file otherfile.py is returned, so the line is replaced; otherwise, the line returned unedited; the output data is written to file outputfile.py:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "INFILE=inputfile.py"
set "OUTFILE=outputfile.py"
set "KEYFILE=otherfile.py"
set "KEYWORD=SECRET_KEY"
rem // Retrieve key from a file:
set "KEY="
setlocal EnableDelayedExpansion
for /F delims^=^ eol^= %%K in ('findstr /L /C:"!KEYWORD!" "%KEYFILE%"') do (
endlocal
set "KEY=%%K"
rem // Toggle delayed expansion inside of the loop to not lose `!`:
setlocal EnableDelayedExpansion
rem // `goto` ensures to take *first* key; remove it to take *last* one;
goto :CONT_KEY
)
:CONT_KEY
endlocal
rem // Retrieve line number where key is found:
set "LRPL="
setlocal EnableDelayedExpansion
for /F "delims=:" %%M in ('findstr /N /L /C:"!KEYWORD!" "%INFILE%"') do (
endlocal
set "LRPL=%%M"
rem // Toggle delayed expansion inside of the loop to not lose `!`:
setlocal EnableDelayedExpansion
rem // `goto` ensures to take *first* key; remove it to take *last* one;
goto :CONT_NUM
)
:CONT_NUM
endlocal
rem // Move line by line from one file to another and replace key:
> "%OUTFILE%" (
rem /* Use `findstr` to precede every line with its line number plus `:`,
rem in order no line to appear empty to `for /F`, as this ignores such;
rem the prefix (up to and including the first `:`) is removed later: */
for /F "delims=" %%L in ('findstr /N /R "^" "%INFILE%"') do (
set "LINE=%%L"
rem // Extract the prefixed number of the current line:
for /F "delims=:" %%N in ("%%L") do set "LNUM=%%N"
rem // Toggle delayed expansion inside of the loop to not lose `!`:
setlocal EnableDelayedExpansion
rem /* Check current line number against previously retrieved one
rem and replace line in case of a match: *//
if !LNUM! EQU !LRPL! (echo(!KEY!) else (echo(!LINE:*:=!)
endlocal
)
)
endlocal
exit /B
Note that the KEYFILE and INFILE variables (defined at the top of the script) may be set to the same file path. However, OUTFILE must be different from INFILE; otherwise the referred file will be emptied.
This covers only the tasks of storing the SECRET_KEY line of a file to a variable and of exchanging the line containing SECRET_KEY in a file by the previously stored one and write the result to another file.
how can I remove the duplicate entry from the text file using batch script. All i want to remove the duplicates before "=" sign and "%%" is exist in every single text file. Text file look likes below
%%B05AIPS_CDDOWNLOAD_IBDE_UNC=\\%%B05AIPS_UPLOAD_NODE.\F$\DATA\IPSL\CDFILES\B05_NAG\CD\INCOMING
%%B05AIPS_CDDOWNLOAD_FTS_UNC=\\%%B05AIPS_UPLOAD_NODE.\B05_NAG\FTS\To_Clearpath\%%DATE_CCYYMMDD.
%%B05AIPS_CDDOWNLOAD_FTS_UNC=%%B05AIPS_CDDOWNLOAD_FTS_UNC.
I got about 30 plus different text files which contains above kind of entries and want to remove the duplicate line and want to keep the first occurrence. Remember duplicate line should be identified before "=" sign only and removal required for the entire line.Each of the different text files have got "%%" sign. Please guide me if there is way to do through batch script or vbscript? Thanks
Here is a simple batch-file solution; let us call the script rem-dups.bat. Supposing your input file is test.txt and your output file is result.txt, you need to provide these files as command line arguments, so you need to call it by: rem-dups.bat "test.txt" "results.txt". Here is the script:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
set "INFILE=%~1"
set "OUTFILE=%~2"
if not defined INFILE exit /B 1
if not defined OUTFILE set "OUTFILE=con"
for /F "usebackq tokens=1,* delims==" %%K in ("%INFILE%") do (
set "LEFT=%%K"
set "RIGHT=%%L"
set "LEFT=!LEFT:*%%%%=__!"
rem Remove `if` query to keep last occurrence:
if not defined !LEFT! set "!LEFT!=!RIGHT!"
)
> "%OUTFILE%" (
for /F "delims=" %%F in ('set __') do (
set "LINE=%%F"
echo(!LINE:*__=%%%%!
)
)
endlocal
exit /B
The script is based on the fact that there cannot occur duplicate environment variables, that are such with equal names.
This code only works if the following conditions are fulfilled:
the file content is treated in a case-insensitive manner;
the order of lines in the output file does not matter;
the partial strings before the first = sign start with %% and contain at least one more character other than %;
the partial strings before the first = contain only characters which may occur within environment variable names, besides the leading %%;
the partial strings after the first = must not be empty;
the partial strings after the first = must not start with = on their own;
no exclamation marks ! are allowed within the file, because they may get lost or lead to other unexpected results;
Here is an alternative method using a temporary file:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "INFILE=%~1"
set "OUTFILE=%~2"
if not defined INFILE exit /B 1
if not defined OUTFILE set "OUTFILE=con"
set "TEMPFILE=%TEMP%\%~n0_%RANDOM%.tmp"
> "%TEMPFILE%" break
> "%OUTFILE%" (
for /F usebackq^ delims^=^ eol^= %%L in ("%INFILE%") do (
for /F tokens^=1^,*^ delims^=^=^ eol^= %%E in ("%%L") do (
> nul 2>&1 findstr /I /X /L /C:"%%E" "%TEMPFILE%" || (
echo(%%L
>> "%TEMPFILE%" echo(%%E
)
)
)
)
> nul 2>&1 del "%TEMPFILE%"
endlocal
exit /B
Every unique (non-empty) token left to the first = sign is written to a temporary file, which is searched after having read each line from the input file. If the token is already available in the temporary file, the line is skipped; if not, it is written to the output file.
The file content is treated in a case-insensitive manner, unless you remove the /I switch from the findstr command.
Update: Improved Scripts
Here are two scripts which are improved so that no special character can bring them to fail. They do not use temporary files. Both scripts remove lines with duplicate keywords (such is the partial string before the first = sign).
This script keeps the first line in case of duplicate keywords have been encountered:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "INFILE=%~1"
set "OUTFILE=%~2"
if not defined INFILE exit /B 1
if not defined OUTFILE exit /B 1
> "%OUTFILE%" break
for /F usebackq^ delims^=^ eol^= %%L in ("%INFILE%") do (
for /F tokens^=1^ delims^=^=^ eol^= %%E in ("%%L") do (
set "LINE=%%L"
set "KEY=%%E"
setlocal EnableDelayedExpansion
if not "!LINE:~,1!"=="=" (
set "KEY=!KEY: = !"
set "KEY=!KEY:\=\\!" & set "KEY=!KEY:"=\"!"
more /T1 "%OUTFILE%" | > nul 2>&1 findstr /I /M /B /L /C:"!KEY!=" || (
>> "%OUTFILE%" echo(!LINE!
)
)
endlocal
)
)
endlocal
exit /B
This script keeps the last line in case of duplicate keywords have been encountered:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "INFILE=%~1"
set "OUTFILE=%~2"
if not defined INFILE exit /B 1
if not defined OUTFILE exit /B 1
> "%OUTFILE%" (
for /F delims^=^ eol^= %%L in ('findstr /N /R "^" "%INFILE%"') do (
set "LINE=%%L"
for /F "delims=:" %%N in ("%%L") do set "LNUM=%%N"
setlocal EnableDelayedExpansion
set "LINE=!LINE:*:=!"
if defined LINE if not "!LINE:~,1!"=="=" (
for /F tokens^=1^ delims^=^=^ eol^= %%E in ("!LINE!") do (
setlocal DisableDelayedExpansion
set "KEY=%%E"
setlocal EnableDelayedExpansion
set "KEY=!KEY: = !"
set "KEY=!KEY:\=\\!" & set "KEY=!KEY:"=\"!"
more /T1 +!LNUM! "%INFILE%" | > nul 2>&1 findstr /I /M /B /L /C:"!KEY!=" || (
echo(!LINE!
)
endlocal
endlocal
)
)
endlocal
)
)
endlocal
exit /B
For both scripts, the following rules apply:
the order of lines with non-duplicate keywords is maintained;
empty lines are ignored and therefore removed;
empty keywords, meaning lines starting with =, are ignored and therefore removed;
non-empty lines that do not contain an = at all are treated as they would be ended with an = for the check for duplicates, hence the entire line is used as the keyword;
for the check for duplicates, each TAB character is replaced by a single SPACE;
every line that is transferred to the returned file is copied from the original file without changes (hence the aforementioned attachment of = or replacement of TAB is not reflected there);
the check for duplicates is done in a case-insensitive manner, unless you remove the /I switch from the findstr command;
Amendment: Processing Multiple Files
All of the above scripts are designed for processing a single file only. However, if you need to process multiple files, you could simply write a wrapper that contains a for loop enumerating all the input files and calls one of the scripts above (called rem-dups.bat) for every item -- like this:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Define constants here:
set "INPATH=D:\Data\source" & rem (location of input files)
set "OUTPATH=D:\Data\target" & rem (location of output files)
set INFILES="source.txt" "test*.txt" & rem (one or more input files)
set "OUTSUFF=_no-dups" & rem (optional suffix for output file names)
set "SUBBAT=%~dp0rem-dups.bat"
pushd "%INPATH%" || exit /B 1
for %%I in (%INFILES%) do if exist "%%~fI" (
call "%SUBBAT%" "%%~fI" "%OUTPATH%\%%~nI%OUTSUFF%%%~xI"
)
popd
endlocal
exit /B
You must not specify the same locations for the input and output files. If you want to overwrite the original input files, you need to write the modified output files to another location first, then you can move them back to the source location -- supposing you have set OUTSUFF in the wrapper script to an empty string (set "OUTSUFF=" instead of set "OUTSUFF=_no-dups"). The command line to overwrite the original input files would be: move /Y "D:\Data\target\*.*" "D:\Data\source".
You could read the file into Excel without splitting it into multiple columns. Use Excel functionality to eliminate duplicates and save it back. You could do all this in VBScript.
Create an Excel Object
Loop
Load text file
Remove duplicates
Save text file
Until there are no more files
Dispose of the Excel Object
Code for the individual pieces should be easily available on the web. Do ask for any additional, specific, pointers you might need.
I'm writing a batch file and I wanted to display a splash in the main screen. I wanted the batch file to pick a random line from a text file and ECHO it. The file name would be splashes.txt.
And in it would be like:
More addicting the lemons
Apples.
This is a splash
Test
The batch file would pick a random quote based on its line.
Like if the patch file picked line 2 it would ECHO "Apples."
Note that I'm using Windows 8.
Any ideas?
While I agree with others that you should attempt this on your own, this is a relatively straight-forward script which should serve as a good working example for learning:
#ECHO OFF
SETLOCAL EnableDelayedExpansion EnableExtensions
REM Source file.
REM The first line on this file should be blank as it will never be selected.
REM Additionally, this file should have no empty lines on the end.
SET TextFile=text.txt
REM Determine the number of lines.
SET NumLines=0
FOR /F "usebackq tokens=* delims=" %%A IN (`TYPE %TextFile%`) DO SET /A NumLines=!NumLines!+1
REM Pick a random line.
SET /A RandomLine=(%RANDOM% %% %NumLines) + 1
REM Prevent skipping all the lines.
IF "%RandomLine%"=="%NumLines%" SET RandomLine=1
REM Print the random line.
FOR /F "usebackq tokens=* skip=%RandomLine% delims=" %%A IN (`TYPE %TextFile%`) DO (
ECHO %%A
REM We are done. Stop the script.
GOTO Finish
)
:Finish
ENDLOCAL
So close - but not quite. SKIP will always be at least 1 (since SKIP=0 is invalid) hence the first line in the file can never be selected.
This is a file I derived from the above with a few tickles. I've also changed the filename because of the way I work. I'm using q27829742.txt containing the lines posted.
#ECHO OFF
SETLOCAL
SETLOCAL EnableDelayedExpansion EnableExtensions
REM Source file.
REM The first line on this file should be blank as it will never be selected.
REM Additionally, this file should have no empty lines on the end.
SET "TextFile=q27829742.txt"
REM Determine the number of lines.
FOR /f %%a IN ('type "%textfile%"^|find /c /v ""') DO SET /a numlines=%%a
REM Pick a random line.
SET /A RandomLine=(%RANDOM% %% %NumLines%)
REM Prevent skipping all the lines.
IF "%RandomLine%"=="0" (SET "RandomLine=") ELSE (SET "RandomLine=skip=%randomline%")
REM Print the random line.
FOR /F "usebackq tokens=* %RandomLine% delims=" %%A IN (`TYPE %TextFile%`) DO (
ECHO %%A
REM We are done. Stop the script.
GOTO Finish
)
:Finish
ENDLOCAL
The find /v /c method counts lines in the file (find files lines that don't match (/v) "" (so that means all lines) and count them (/c) - simply more efficient.
Pick-random-number : removing the + 1 produces a result 0..(numlines-1) which is the actual number of lines to skip.
Problem there is that skip=0 is invalid, so construct a string which is eother empty (for 0) or "skip=..." (otherwise) - all ready to be included in the for /f command-options.
The syntax SET "var=value" (where value may be empty) is used to ensure that any stray spaces at the end of a line are NOT included in the value assigned. set /a can safely be used "quoteless".
If you know the number of lines to be, let's say 25.
You could have a vector holding each line, and then compute a random index and print out the corresponding line with the following example.
setlocal EnableDelayedExpansion
SET line[0]=Apples.
SET line[1]=This is a splash
SET line[2]=Test
SET line[3]=...
SET line[24]=many lines later...
SET /A index=%RANDOM% % 25
ECHO !line[index]!
A practical application of Jason Falkner's (and Magoo's) answer would be to their script and place it into a .cmd file that would reside in System32 (say randomline.cmd). This will integrate it into the command line utility, making it easily re-usable via a simple call line, avoiding you to have rewrite those lines over and over and over again in all your scripts. For example:
if you call the Magoo/Falkner script randomline.cmd and place it in System32
randomline.cmd
#ECHO OFF
SETLOCAL
SETLOCAL EnableDelayedExpansion EnableExtensions
REM Source file.
REM The first line on this file should be blank as it will never be selected.
REM Additionally, this file should have no empty lines on the end.
SET "TextFile=%1"
REM Determine the number of lines.
FOR /f %%a IN ('type "%textfile%"^|find /c /v ""') DO SET /a numlines=%%a
REM Pick a random line.
SET /A RandomLine=(%RANDOM% %% %NumLines%)
REM Prevent skipping all the lines.
IF "%RandomLine%"=="0" (SET "RandomLine=") ELSE (SET "RandomLine=skip=%randomline%")
REM Print the random line.
FOR /F "usebackq tokens=* %RandomLine% delims=" %%A IN (`TYPE %TextFile%`) DO (
ECHO %%A
REM We are done. Stop the script.
GOTO Finish
)
:Finish
ENDLOCAL
you'll be able to use it anypoint in the command prompt by entering
randomline any_old_text_file.txt
or if you want to use it in a cmd/bat script
call randomline any_old_text_file.txt
And if you want to set a value for a variable in your script from a random line in a file, you can do:
call radomline file_containing_values_on_each_line.txt>%temp%\randomlinevalue.txt
set /p any_old_variable=<%temp%\randomlinevalue.txt
del %temp%\randomlinevalue.txt
I am creating a code that strips through different MAC addresses randomly, but cannot figure out how to do this. My thought on how to approach this is to randomize or rearrange the order of the MAC address in the text file with this script, but I cannot figure out how to do this with a batch file. How this will work is that it will read "maclist.txt", then create a new temp file with the random order "maclist_temp.txt", that will be the rearranged file. Then, it will pull this randomized file in order.
I have tried Google and searching the web, but I haven't found anything too useful. I'm still actively looking, but any advice would be extremely useful.
Something as simple as extracting and deleting a random line and then adding to the bottom might work. Randomization would be better though, but I want to keep the original list. Something like:
Make a temp copy of maclist.txt called maclist_temp.txt
Take one random MAC address, remove it from maclist_temp.txt
Readd it to the bottom
That is all I want, but any suggestions are welcome.
You may try this batch file to help you to shuffle your maclist.txt. The usage of the batch code is
C:\> type list.txt | shuffle.bat > maclist_temp.txt
Here are the contents of shuffle.bat:
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET TmpFile=tmp%RANDOM%%RANDOM%.tmp
TYPE NUL >%Tmpfile%
FOR /F "tokens=*" %%i IN ('MORE') DO SET Key=!RANDOM!!RANDOM!!RANDOM!000000000000& ECHO !Key:~0,15!%%i>> %TmpFile%
FOR /F "tokens=*" %%i IN ('TYPE %TmpFile% ^| SORT') DO SET Line=%%i&ECHO.!Line:~15!
::DEL %TmpFile%
ENDLOCAL
After issuing the above command, maclist_temp.txt will contain a randomized list of MAC addresses.
Hope this helps.
Here is a simpler method to randomize/randomise a file, no temp files needed. You can even reuse the same input filename.
Limitations are: blank lines and line starting with ; will be skipped, and lines starting with = will have all leading = signs stripped and ^ characters are doubled.
#echo off
setlocal
for /f "delims=" %%a in (maclist.txt) do call set "$$%%random%%=%%a"
(for /f "tokens=1,* delims==" %%a in ('set $$') do echo(%%b)>newmaclist.txt
endlocal
I really like foxidrive's approach. Nevertheless I want to provide a solution with all the listed limitations eliminated (although cmd-related restrictions like file sizes < 2 GiB and line lengths < ~ 8 KiB remain).
The key is delayed expansion which needs to be toggled to not lose explamation marks. This solves all the potential problems with special characters like ^, &, %, !, (, ), <, >, | and ".
The counter index has been implemented in order not to lose a single line of the original text file, which could happen without, because random may return duplicate values; with index appended, the resulting variable names $$!random!.!index! are unique.
The findstr /N /R "^" command precedes every line of the original file with a line number followed by a colon. So no line appears empty to the for /F loop which would ignore such. The line number also implicitly solves the issue with leading semicolons, the default eol character of for /F.
Finally, everything up to and including the first colon (remember the said prefix added by findstr) is removed from every line before being output, hence no more leading equal-to signs are dismissed.
So here is the code:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set /A "index=0"
for /f "delims=" %%a in ('findstr /N /R "^" "%~dpn0.lst"') do (
setlocal EnableDelayedExpansion
for /F %%b in ("$$!random!.!index!") do (
endlocal
set "%%b=%%a"
)
set /A "index+=1"
)
> "%~dpn0.new" (
for /f "delims=" %%a in ('set $$') do (
set "item=%%a"
setlocal EnableDelayedExpansion
echo(!item:*:=!
endlocal
)
)
endlocal
exit /B
This seems to work. Feed it a command line parameter of the file to randomize.
#echo off
setlocal EnableDelayedExpansion
rem read the number of lines in the file
rem the find prepends the line number so we catch blank lines
for /f "delims=" %%n in ('find /c /v "" %1') do set "len=%%n"
set len=!len:*: =!
rem echo %1 has %len% lines
rem Relocate as many lines as there are lines in the file
for /l %%j in (1 1 !len!) do (
rem echo starting round %%j
rem geta random number between 1 and the number of lines in the file
set /a var=!random! %% !len! + 1
rem echo relocating line !var!
rem make sure there is no temp file
if exist %1.temp del %1.temp
rem read each line of the file, write any that don't match and then write the one that does
<%1 (
for /l %%i in (1 1 !len!) do (
rem if it is the target line then save it
if %%i == !var! (
set /p found=
rem echo saving !found!
)
rem if it is the target line then write it
if not %%i == !var! (
set /p other=
rem echo writing !other!
echo !other!>> %1.temp
)
)
rem now write the target line at the end
rem echo appending !found!
echo !found!>> %1.temp
)
rem replace the original with the temp version
move %1.temp %1>nul
)
rem print the result
type %1
Place in cmd file
for /f "tokens=2 delims=/" %%m in ('cmd /e:on /v:on /c "for /f %%f in (maclist.txt) do #echo !random!/%%f" ^| sort') do echo %%m
It spawns a cmd which reads the mac list in the inner for, prefixes a random value and a slash to the mac and sorts the list. Then this list is splitted in the outter for using the slash as delimiter and printing the mac address.