Removing blank lines and spaces in text file using batch - windows

I have a batch file which is checking the value of a registry key.
I have the file reading the value and deleting the blank line at the beginning of the file, but there are still spaces in the output which need removing.
The script appears not to read the output properly if it contains spaces.
Here is the code I am using:
#echo off
md c:\temp
md c:\temp\Reg_Test
reg query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v4.0.30319 /v SchUseStrongCrypto > c:\temp\Reg_test\AfterRegUpdate32.log
findstr /v /r /c:"^$" /c:"^\ *$" /c:"^\ *$" "C:\temp\Reg_Test\AfterRegUpdate32.log" >> "C:\temp\Reg_Test\AfterRegUpdate32.txt"
exit
It appears to do everything I need except for deleting the spaces. I know there is something simple I am missing here. Any help will be greatly appreciated.

Is this what you're trying to retrieve:
#Echo Off
MD "C:\temp\Reg_Test" 2>Nul
(For /F "EOL=H Tokens=2*" %%A In (
'Reg Query "HKLM\SOFTWARE\Microsoft\.NETFramework\v4.0.30319" /V "SchUseStrongCrypto" 2^>Nul'
) Do Echo %%B)>"C:\temp\Reg_Test\AfterRegUpdate32.txt"

Related

Is there a way to loop through a files contents and use it in batch?

I'm working on a project in batch-file and need to read the contents of a file and print out the contents. I have a set up a for /f loop to go through the contents of the file, but it isn't working.
The Code
cd C:\Users\(name)
for /f %%G in (List.txt) (
del /F /S %%G
echo %errorlevel%
pause
)
I have been Googling for about an hour now, and can't find anything which has worked for me. I was hoping that you guys could tell me what I'm doing wrong with my code.
Default delimiters in cmd for the for /F loop is whitespace. Your code will split and assign the first word/number/line up until the first whitespace.
You need to tell the for /f loop to not use any delimiters. Also usebackq in order for you to double quote your file as that can also be a full path to the file with whitespace, i.e: "C:\My Documents\Old Files\list.txt"
#echo off
for /f "usebackq delims=" %%i in ("List.txt") do del /F /S "%%~i"
then, del does not set the errorlevel and you will always get 0 returned. if you really want to check the result, redirect stderr to stdout and use findstr to determine if delete was successful.

Exclude all rows of a file of key strings contained in some other file

I am trying to write a Windows batch file where there are two files in concern.
The first file all.err contains logs of failed test cases like below:
`CHECK:FAIL,"It should have been DEST`"
`CHECK:FAIL,"It should have been XYZA`"
`CHECK:FAIL,"It should have been PRTS`"
`CHECK:FAIL,"It should have been ABCD`"
.....................................
We have another file exclude.txt which stores some string per line like:
XYZA
ABCD
I am trying to write a Windows batch script which can list all lines from all.err that do not contain any word in exclude.txt.
I am not able to understand how this can be implemented - any idea?
There is just one Windows command line necessary for this task as written by SomethingDark:
%SystemRoot%\System32\findstr.exe /I /L /V /G:exclude.txt all.err
The help output on running in a Windows command prompt window findstr /? explains the parameters /I (case-insensitive), /L (search strings are literal strings), /V (inverse matching output), /G (get search strings from file).
When using a for loop, you need to go through exclusions file first, get each line, then exclude these meta viariables from your search in the log file:
#echo off
for /f "delims=" %%i in (exclude.txt) do (
for /f "delims=" %%b in ('type all.log ^| findstr /V "%%i"') do echo(%%b
)
The first do code block is not needed, I simply posted it like that for readability, it simply can be:
#echo off
for /f "delims=" %%i in (exclude.txt) do for /f "delims=" %%b in ('type all.log ^| findstr /V "%%i"') do echo(%%b

Remove all blank lines from *.txt files in the directory and subfolders in Windows

Searching, trying and crying I developed a code:
For /R %%G IN (*.txt) do for /f "delims=" %%a in ('more +1 %%G ^| find /v ""') do (set line=%%~a echo !line!) > new\%%G
Do someone know why does it loop forever?
What it is expected to do, is to remove blank lines in every *.txt file it founds in all subdirectories and put the new file with the same name in "new" folder. I can provide all "new" folders needed manually.
I'm not sure this would cause it to loop forever, but you are writing to a NEW folder that is in the same hierarchy that you are reading from. So it will process files that you just wrote. You will need a filter to prevent that.
Also, I think your output file name needs %%~nxG to get just the file name and extension, without the path.
There is a much simpler way to strip out blank lines. findstr . file will strip out empty lines. findstr /rc:"[^ ] will strip out empty lines and lines that contain only spaces.
#echo off
setlocal
set "outFolder=new"
for %%F in ("%outfolder%\.") for /r %%G in (*.txt) do if "%%~dpG" neq "%%~fF\" findstr /rc:"[^ ]" "%%G" >"%%~fF\%%~nxG"
But the simplest thing to do is just make sure your output folder is distinct from your source hierarchy
for /r %%G in (*.txt) do findstr /rc:"[^ ]" "%%G" >"\distinctPath\%%~nxG"
UPDATE
One thing that could cause your script to hang is that piped MORE will hang if the file has more than 64k lines. But I don't understand why you are using MORE in the first place.

cmd Text Search & Print

I've been tasked with extracting some data from a text file and I was wondering if anyone knows a quick cmd method to my problem.
The file I need to search is quite large, consisting of repetitive records.
What I'm looking to do is search for one line of text, say:
'searchForThisLineOfText'
Then, when it's found, go to the next line down from that text, and write that line to a file.
So the contents of the file looks somting like this:
searchForThisLineOfText
563473
someOtherLine
34583933
anotherline
2342
searchForThisLineOfText
3424
someOtherLine
34583933
anotherline
2342
From that, I would like to write the numbers 563473 and 3424 to a file.
Anybody have any ideas on how to do this?
Thanks in advance.
#ECHO OFF
SETLOCAL
SET "found="
(
FOR /f "delims=" %%a IN (q25871330.txt) DO (
IF DEFINED found ECHO %%a
IF "%%a"=="searchForThisLineOfText" (SET "found=Y") ELSE (SET "found=")
)
)>newfile.txt
GOTO :EOF
I used a file named q25871330.txt containing your data for my testing.
Produces newfile.txt
This assumes that the blank lines are not actually in the text file.
#echo off
type "file.txt" | findrepl "searchForThisLineOfText" /o:1:1 >"newfile.txt"
pause
This uses a helper batch file called findrepl.bat (by aacini) - download from: https://www.dropbox.com/s/rfdldmcb6vwi9xc/findrepl.bat
Place findrepl.bat in the same folder as the batch file or on the path.
This will generate a temporary file with the number of the lines that need to be retrieved and then retrieves the required lines
#echo off
setlocal enableextensions enabledelayedexpansion
rem We will need a temporary file
set "tempFile=%temp%\%~nx0.%random%.tmp"
rem Get the number of the lines that we need to retrieve
(
for /f "delims=:" %%a in ('
findstr /n /l /c:"searchText" "data.txt"
') do ( set /a "n=%%a+1" & echo(!n!:)
)>"%tempFile%"
rem Retrieve the indicated lines to the output file
(
for /f "tokens=1,* delims=:" %%a in ('
findstr /n /r /c:"^" "data.txt"
^| findstr /b /l /g:"%tempFile%"
') do echo(%%b
)>"outputFile.txt"
rem Remove the temporary file
del "%tempFile%" > nul 2>nul

How to retrieve substring from a string in Windows batch file?

How do I retrieve the string "C:\Program Files (x86)\Stupid\MS\" from the following input?
HKEY_CURRENT_USER\Software\Stupid\MS`
installpath REG_SZ C:\Program Files (x86)\Stupid\MS\`
please use windows bat file.
My way:
#echo off
Setlocal enabledelayedexpansion
for /f "tokens=2,* delims=:" %%i in ('reg query "HKEY_CURRENT_USER\Software\Stupid\MS" /v "installpath"') do (
echo %%i
)
pause
the result is : \Program Files (x86)\Stupid\MS\
req query "HKEY_CURRENT_USER\Software\Stupid\MS" /v "installpath"
returns this on my xp (sorry I don't have anything more recent at the moment)
! REG.EXE VERSION 3.0
HKEY_CURRENT_USER\Software\Stupid\MS
installpath REG_SZ C:\Program Files (x86)\Stupid\MS
When using delim's default value (a space) what we actually need is the third and following tokens from the last line. "third and following" is tokens=3*. The third token will be available in %%i, the "and following" in %%j. This letter is automatically assigned.
Unfortunately the first line also has a third and following token, so we need to exclude the first line. We can do that by using the eol option to exclude lines starting with "!". "Setlocal enabledelayedexpansion" doesn't change anything in this litte batch file so I dropped it.
Summary: this works for me:
#echo off
for /f "eol=! tokens=3*" %%i in ('reg query "HKEY_CURRENT_USER\Software\Conceptworld\Piky\Settings" /v "installpath"') do echo %%i %%j
This should work - put a tab character in where it says TAB and leave the space too.
#echo off
for /f "tokens=2,* delims=:TAB " %%i in ('reg query "HKEY_CURRENT_USER\Software\Stupid\MS" /v "installpath"') do (
set "var=%%j"
)
echo "%var%"
pause

Resources