batch command that will loop through files ending in “.txt” and display file contents - batch-file

I'm pretty sure I am just missing something small, but I am trying to figure out how to loop through the contents of a directory, identify those with a .txt extension, and display the contents of the .txt files to the display.
I searched for an answer to this but couldn't find it specifically. I found this one that shows how to find all the .txt and use echo to display the file name.
I also found this one that shows how to use Type to display the file contents, and this one that combines them sort of. But I keep getting errors when I try different variations on using type in a for loop.
I've tried:
for %%i in (*.txt) do type %%i
and
for %%i in (*.txt) do (
type %%i
echo.
)
But both of those are giving errors. I'm sure it's something simple I am missing.
Update:
The problem was spaces in the filenames. Adding quotes around the %%i following Type fixed it.

No need for any FOR loop. type *.txt will do the trick. Each file name will be printed to stderr, followed by the content on stdout. By default, both stderr and stdout appear on the console. There will be one or two line gaps between each file name and file content.
If you don't want to see the file names, then type *.txt 2>nul. But then there is no gap between each file.

Related

Unable to check if a string contains a specific string in a for loop of a text file

I have a batch script that reads line by line a text file (ProcessedFiles.txt). The text has many lines that represent filenames of a directory. They can be either JSON or HTML files. I would like to check if the string "json" exists in that line. I have already searched and tried a few answers like
if not x%%a:json=%==x%%a% echo It contains JSON
or even extracting the last 4 characters from the %%a
SET var=%%a:~-4%
but I must be doing something really wrong here because I cannot get it to work.
I apologize for my very limited knowledge on batch script.
Basically I want to know if each line of the text file I am reading contains the string "json". This is my for loop code i am trying to correct
for /F "tokens=*" %%a in (%jsonDir%ProcessedFiles.txt) do (
echo Processing file: %%a
SET var=%%a:~-4%
)
The echo Processing file: %%a displays the line correctly and this is what I am aiming to search if it contains the string "json".
For example, if text file ProcessedFiles.txt contains the lines
ABCD.json
BCDF.json
ABDR.html
CVBF.json etc.
when the loop reads the line ABCD.json or BCDF.json or CVBF.json then I need to know that it has encountered a JSON file, but for line ABDR.html it is not a JSON file.
Thank you in advance

Output from FINDSTR into a string in batch command

I would be really grateful for anyone that could save me some time on this.
I have a Windows batch file that I am using to match two different file types with different extensions and write the results out to a file.
It reads through a table of filenames (called matched_filenames.txt) and, for each line in it, tries to locate the filename in two other tables.
I have this code (which works to an extent).
for /f "tokens=*" %%z in (%Ourhome%\matched_filenames.txt) do (
rem -------
rem ------- now (for each line), find the photo name in both the list of raw and list of jpg files
rem -------
findstr /C:"%%z" raw_photos_directory.txt >>located_matches.txt
findstr /C:"%%z" jpg_photos_directory.txt >>located_matches.txt
)
But it writes separate lines out to the located_matches.txt file.
I would really like to assign the results of the findstr's to variables so that I can then concatenate them and then write the concatenated line as a single line to the file.
Can anyone recommend a more elegant way of doing this?

I want to copy files, listed in a .txt file, into a new folder

I have roughly 2000 documents in one folder and I want to seperate them into different folders.
I have created a "documents.txt", which lists every file I´m interested in.
The .txt file reads as:
C:\Users\NIE\Desktop\test2\one.pdf
C:\Users\NIE\Desktop\test2\two.pdf
C:\Users\NIE\Desktop\test2\three.pdf
So now I went on creating a .bat file:
#echo off
FOR /F "delims=" %a IN (C:\Users\...\Desktop\test2\documents.txt) DO COPY "%%~a" "C:\Users\...\Desktop\test2\kopieren\%%~nxa"
The folder structure is like:
In "...\Desktop\test2", all documents are located. In a subfolder ("...\Desktop\test2\copy"), the specific documents (as listed in the documents.txt) should be copied.
While running my code, I´m getting the statement:
%C:\Users\...\Desktop\test2\one.pdf
The syntax for the filename, path is wrong
0 files were copied.
So I guess the "%" seems to be the bad guy here. I tried different styles for the .txt file, like
one.pdf
userprofile%\Desktop\test2\one.pdf (thought I could use the first % for completing the "%userprofile%" stuff
Every solution I could find via google did not worked either, the formatting of the .txt file seems to be a problem in my case.
Really looking forward for you answers :)
Looks like you have done a fatal mistake: %a
Fixed:
FOR /F "delims=" %%a IN (C:\Users\...\Desktop\test2\documents.txt) DO COPY "%%~a" "C:\Users\...\Desktop\test2\kopieren\%%~nxa"

Using a batch file, how do I delete files that have a certain string of characters inside?

I want to make a .bat files that can allow me to delete the file if a certain string of characters appears inside. For example, if I want to delete files that have the string harp inside of it and the files in the folder are:
harp1.txt
mellowharp.txt
highharplow.txt
I want to be able to delete all of these files. I figured out how to do it if it starts or ends with the string, by merely writing
del harp*.*
del *harp.*
But I can't figure out how to do it if harp is found in the middle of the string.
I've tried things like putting an asterisk on each side of harp for a catch all case, but obviously that doesn't work.
This is literally my first day and experience with .bat files, and I would really appreciate any help I get on this, but please by gentle and explain EVERYTHING because I'm coming into this with no knowledge whatsoever.
Well, since using del *harp*.* didn't work for you, you could iterate through all the files in the specified directory then delete when the substring "harp" is found in the file name.
This should do it:
#echo off
for /f %%F in ('dir /b * ^| find /i "harp"') do del %%F
echo Files Successfully Deleted...
pause>nul
exit
Make sure this .bat file is in the same directory as your other files.
Explanation:
The for command loops through the files in the directory and assigns them to the variable %%F and searches for the substring "harp" in the file name. Then, when found, the do del %%F tells the program to delete the file (%%F). Read more about for statements here.
The echo command simply prints a line of text to the console window. To read more about echo click here.
The pause>nul pauses the program and waits for the user to press a key, but without any text being displayed on screen. (The normal text that would appear would be press any key to continue...). More about pause here.
The exit command stops the program. More on exit here.

How do I remove only certain words (not lines containing that word) from a text file with a Batch?

I'm writing a batch file that uses an output from a dir command to perform other tasks, I also want to use this same output (stored in dir_output.txt) for another use, but I don't want the file extensions at the end. right now the file looks like this:
barrier_1_post.p3d
barrier_1_section.p3d
but I want it to look like this
barrier_1_post
barrier_1_section
minus the file extensions, but I have no idea how to do this via the batch, I've looked through SO exhaustively but either I'm not finding the solution or I can't see the wood for the trees.
Any help would be amazing, I'm fairly new to batches.
for /f "delims=" %%a in (yourfilename.txt) do echo %%~na
should remove those extensions quite happily.
%~na delivers the name part only of the assumed filename in %a
(see for /? from the prompt for documentation)

Resources