Batch files - why does a funny ASCII character appear when redirecting output of tree command to a file? - batch-file

The following code:
if "%DispMode%"=="tree" (
tree >temp
findstr /b /v /c:"F" temp >>temp2
del temp >nul
findstr /b /v /c:"V" temp2 >>temp
del temp2 >nul
findstr /b /v /c:"A" temp >>temp2
del temp >nul
type temp2
del temp2 >nul
)
...for some reason outputs a funny ASCII character at the beginning of the line of each non-root folder and in other places too. The character looks like a superscript, underlined a. I'm hoping someone can explain why this is happening?
I was able to produce this problem even by just typing tree >tempfile at the Win11 command prompt and then type tempfile, so I don't think this has anything to do with the block of code, the findstr commands, or whatever.

The extended ascii-characters used by tree are causing your weird characters.
Different character sets show different characters for these extended characters.
Use Tree /A to circumvent this problem.

Related

Delete string in multiple files in directory with cmd

:-)
I'm very new to cmd-commands and .bat-stuff, so would be grateful if any of you could help me with a problem.
So...I have a folder with 116 .txt files.
All these .txt files contain lines starting with a "#" and some other lines starting with "---".
I figured that findstr /v /L /C:"#" test.txt > test2.txt works in creating a new file without any lines that contain "#"
Now my question, is it possible to write something like findstr /v /L /C:"#" & "---" *.txt > *.txt ?
The goal is that each existing file is overwritten and all lines containing either "#" or "---" are removed.
Glad for any help!
Cheers,
Dyz
#ECHO OFF
SETLOCAL
rem The following setting for the source directoryis a name
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files\t w o"
FOR /f "delims=" %%w IN ('dir /b /a-d "%sourcedir%\*.txt"') DO FINDSTR /v /L /c:"#" /c:"---" "%sourcedir%\%%w">"%sourcedir%\tempfile"&MOVE /y "%sourcedir%\tempfile" "%sourcedir%\%%w" >nul
GOTO :EOF
The meat of the matter is the for/f line, obviously.
Run dir /b to obtain a list of the filenames. /a-d excludes directorynames. This list is built in memory first, as the .txt files are being replaced - this ensures that the files are only "listed" once.
The for /f "delims=" reads that list and assigns each line-contents (filename+extension only) to %%w. The delims= ensures that filenames containing spaces are properly processed.
Then execute the findstr, which can take multiple /c: arguments to or the strings.
[I haven't tried it, but FINDSTR /v /L "# ---" "%sourcedir%\%%w" should produce identical processing]
The output of the findstr is directed to a temporary filename (I chose to put it in the source directory, but it could be anywhere with any name) and then the temporary file is moved over the original with the 1 file moved message suppressed by a >nul.
As always, test first on a copied sample.

Delete 2 Lines From hosts

I need to remove 2 lines from my hosts file in a Batch File:
www.domain1.com
www.domain2.com
So I wrote this code, but have some problems:
#Echo Off
Set hosts=%WinDir%\System32\Drivers\etc\hosts
Attrib -R %hosts%
FindStr /V /I /C:"www.domain1.com" "%hosts%" > "d:\1.txt"
FindStr /V /I /C:"www.domain2.com" "d:\1.txt" > "d:\2.txt"
Well this work and d:\2.txt is made the way I want, so now I must delete 1.txt and replace/move 2.txt to hosts
I have some questions to update and make my code simpler:
Can I merge the above 2 FindStr lines into 1 line without making a 3rd file?
I don't know why for example this one does not work, while it should work?
FindStr /V /I /C:"www.domain1.com" "%hosts%" > "%hosts%"
This makes the whole file empty!
Please advise, not using Find :)
The complete command line to search case-insensitive and literally in all lines in hosts file for either www.r2rdownload.com or www.elephantafiles.com and output all lines not containing one of those two strings is:
%SystemRoot%\System32\findstr.exe /I /L /V "www.r2rdownload.com www.elephantafiles.com" "%SystemRoot%\System32\drivers\etc\hosts"
The option /L is important as otherwise the two strings separated by a space are interpreted as regular expression strings with . being interpreted as placeholder for any character except newline character.
Another possibility for exactly the same find result is:
%SystemRoot%\System32\findstr.exe /I /V /C:"www.r2rdownload.com" /C:"www.elephantafiles.com" "%SystemRoot%\System32\drivers\etc\hosts"
The command line to find additionally empty lines anywhere in file and so do not output those lines is:
%SystemRoot%\System32\findstr.exe /I /R /V "www\.r2rdownload\.com www\.elephantafiles\.com ^$" "%SystemRoot%\System32\drivers\etc\hosts"
All three space separated strings are regular expression strings in this case which is the reason for escaping . with a backslash character to be interpreted as literal character.
The entire batch file could be for example:
#echo off
set "HostsFile=%SystemRoot%\System32\drivers\etc\hosts"
%SystemRoot%\System32\attrib.exe -r "%HostsFile%"
%SystemRoot%\System32\findstr.exe /I /R /V "www\.r2rdownload\.com www\.elephantafiles\.com ^$" "%HostsFile%" >"%TEMP%\%~n0.tmp"
move /Y "%TEMP%\%~n0.tmp" "%HostsFile%"
if errorlevel 1 del "%TEMP%\%~n0.tmp"
set "HostsFile="
This batch file must be executed with elevated privileges of an administrator.

Why does batch file for "Send To" to get all file names of a folder with space in path not work?

I need to get the names of all files within a folder to do a compare in Excel. I have this working and set up as a Send To feature. However, if the folder I right click on contains a space, it fails. Here is what I have:
#echo off
for /f "tokens=2 delims=:." %%x in ('chcp') do set cp=%%x
chcp 1252>nul
set dirpath=%1
dir %dirpath% dir /b /a | sort > "%dirpath%\FolderContents.txt"
chcp %cp%>nul
exit
Why are the file names not written to "Selected Folder\FolderContents.txt" if folder path contains 1 or more spaces?
There are several mistakes in those few lines of batch code.
The line with command DIR contains the command twice.
As the last paragraph output after several pages on running cmd /? in a command prompt window explains, a file/folder name (and other parameter strings) must be enclosed in double quotes if there is in name or path a space or one of the following characters: &()[]{}^=;!'+,`~
Therefore first argument of batch file on execution is enclosed in quotes if folder name contains a space. set dirpath=%1 assigns the folder name with the quotes to the environment variable dirpath. This results on next line in ""C:\Folder With Space"\FolderContents.txt" which can't be processed by command processor as expected by you.
A number left to redirection operator > could easily result in getting it interpreted as handle number. Although code page numbers are large enough to be not interpreted as handle number, it is better to have a space left to last redirection operator > in your code as %cp% is replaced by a code page number.
A really working and additionally simplified code for your task is:
#echo off
for /F "tokens=2 delims=:." %%x in ('chcp') do set "cp=%%x"
chcp 1252 >nul
dir "%~f1" /A-D /B /ON >"%~f1\FolderContents.txt"
chcp %cp% >nul
exit
The command DIR outputs just
the names of files because of option /A-D
in bare format because of option /B
ordered by name because of option /ON
and without path because of not using option /S for getting a list with all subdirectories included.
%~f1 is replaced by command processor by name of folder with full path without quotes.
Well, determining active code page of this and only this command process and restoring the code page before exiting entire command process is also not really needed in my point of view and therefore second and last but one line are also not necessary.
This variant of above writes the file names with path into the text file.
#echo off
chcp 1252 >nul
del "%~f1\FolderContents.txt" 2>nul
for /F "delims=" %%I in ('dir "%~f1" /A-D /B /ON 2^>nul') do (
>>"%~f1\FolderContents.txt" echo %~f1\%%I
)
exit
The redirection operator >> is now left to command echo to avoid that a file name ending very unlikely with  1,  2, ... is interpreted as handle number. A separating space can't be used here as this space would be also written into the file as being taken by command echo which would result in a trailing space on each file name in text 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.
call /?
chcp /?
dir /?
echo /?
exit /?
for /?
set /?
See also Microsoft article about Using command redirection operators.

want to search for asterisk in findstr but character is recognised as wildcard instead

Iv got a simple findstr function (windows batch) however im trying to search for the actual asterisk character in a text document. Because the * character is also used as a wildcrad function my program becomes muddeld up.
Dose any body know how to overcome this issue?
#echo off
SET EXTENT=%~x1
SET MYPATH=%~dp1
SET FILETYPE=%PATH%*%EXTENT%
Type C:\HELLO.txt | findstr /I /V /C:"%FILETYPE%">>C:\TEMP.txt
DEL /S/Q "C:\HELLO.txt"
ren "C:\TEMP.txt" "HELLO.txt"
DEL /s/q "C:\TEMP.txt"
Solution: \*
Can be found at findstr /?

Batch Scripting - FOR and IF

Just new to Batch Scripting and having a problem here: I've been asked to list all the text files whose names are 7 letters long within the whole c:\ drive and ouput it to a file. I can't figure it out.
After endless hours of searching google I've come up with this:
for /R C:\ %i in (.\*) do if %~ni==???????.txt echo %i > file.txt
Now I understand this is probably wrong due to the fact... it's not working.
Thanks in advance. John W.
I think this will do it. You don't really need a batch file for this.
dir /s /b C:\*.txt | findstr "\\.......\.txt$" > files.txt
Update:
To make it work for 123\123.txt use
dir /s /b C:\*.txt | findstr "\\[^\\][^\\][^\\][^\\][^\\][^\\][^\\]\.txt$" > files.txt
I would put the code into a batch file, then you have to double the percent signs of the FOR-Loop.
As the FOR /R supports some nice features, like searching for a file-masks, you could use this.
But ???? will find all files with a maximum of the number of question marks not only excact matching.
Therefore I test the filename later, if it has at least 7 characters
setlocal EnableDelayedExpansion
for /R C:\temp\ %%i in (???????.txt) do (
set "filename=%%~ni"
if "!filename:~6,1!" NEQ "" (
echo has 7 characters %%i
)
)

Resources