I am trying to list all the files in a directory excluding the extension. I would like to output this into a txt file. I have managed to list all the files successfully.
#ECHO OFF
for %%a in ("c:\wamp64\www\blessd\final\*") do ECHO %%~na
This works fine and all the files in the directory are printed to the screen, however when I try output this to a txt file it does not work.
for %%a in ("c:\wamp64\www\blessd\final\*") do ECHO %%~na > test1.txt
You need to redirect the output of the bat file, not of a single output. If your bat file is My.bat, try:
My.bat > this.out
Related
I am fairly new in using/making .bat files.
I'm trying to make an incredible simple one that combines .txt files into a .tmp file, and then renames the .tmp file in to a .txt file.
This is the code:
#echo off
title Combine Text Files
for %f in (*.txt) do type %f >> Combined.tmp & echo. >> Combined.tmp
rename Combined.tmp Combined.txt
pause
The problem I come up with is that when I try to run the .bat file, nothing happens. CMD closes itself immediately.
If I try to run the script on the third line on CMD, it works fine and it creates the .tmp file.
Similarly, if I run the .bat file sans the script on the third line, I am able to see the "Press any key to continue.." just fine.
Am I doing wrong with the for loop when in .bat files?
You need to double the % when using for command from batch file because this is how the for parser works:
#echo off
title Combine Text Files
for %%f in (*.txt) do (
type "%%~ff">>"Combined.tmp"
(echo()>>"Combined.tmp"
)
rename "Combined.tmp" "Combined.txt"
pause
The errors in the FOR syntax are one of the very few occasions that will terminate a bat file execuction.
As type command supports wild cards you can just try with:
type "*.txt">"Combined.tmp"
I have an exe for comparing 2 .csv files. And it works when I open cmd window, drag exe, type space, drag 1st csv file, type space, and drag 2nd file.
Now I want to automate it, and the problem is it is never on the same place, nor uses the same files for comparing...
What I got so far is following in batch:
%~dp0\Komparator.exe BC101.csv BC102.csv
pause
those 2 csv files are going to be in same folder, next to .exe and .bat file... but that "same folder" is not always the same, today its one location, another day is another folder
but I dont know how to automate those 2 arguments for file names, I just want it to recognize two .csv files near .exe and .bat
Thanks in advance!
You can read csv filenames and store them into batch variables using for cycle.
Here's an example, assuming your csv files are in the same directory as the batch file.
compare.cmd
#echo off
set FILE1=
set FILE2=
for /f "delims=" %%a in ('dir /b %~dp0*.csv 2^>nul ^| sort') do (
if defined FILE1 set "FILE2=%%~a" & goto :exit_loop
set "FILE1=%%~a"
)
:exit_loop
if not defined FILE1 echo first csv file not found!& goto :eof
if not defined FILE2 echo second csv file not found!& goto :eof
%~dp0\Komparator.exe "%~dp0%FILE1%" "%~dp0%FILE2%"
So basically, let's say I have the following folders in a directory:
test_folder_1
test_folder_2
And I also have a text file with the list of all the folders in that directory.
How can I create a script that will create a text file, test.txt, in all the folders in the directory? (test_folder_1 and test_folder_2)?
I tried modifying some code I found online and got this:
for /F "tokens=1 delims=," %%v IN (folderlist.txt) DO echo test>"C:\Users\myname\My Documents\test\"%%v""
However, running this, I get "Access Denied."
Any idea what went wrong, or alternative ways to do this? Thanks.
The command
echo test>"C:\Users\myname\My Documents\test\"%%v""
despite the wrong double quotes around %%v redirects the text test directly to the directory instead of a file test.txt in the directory. Or in other words: The attempt to create a file with name of an already existing directory is denied by Windows and the result is the access denied error message.
The solution for creating a file test.txt with the line test in each directory specified in text file folderlist.txt is:
for /F "tokens=1 delims=," %%v in (folderlist.txt) do echo test>"%USERPROFILE%\My Documents\test\%%v\test.txt"
It is also possible to create a file test.txt with a file size of 0 bytes in all subfolders of the test folder with following command line in the batch file.
for /D %%v in ("%USERPROFILE%\My Documents\test\*") do echo. >nul 2>"%%v\test.txt"
The command FOR returns in this case the subfolder name with full path always without surrounding double quotes even if the folder path contains a space character as it is the case here.
See How to create empty text file from a batch file? and read the Microsoft article about Using command redirection operators for more details.
echo. >nul 2>test.txt results in writing just a new line to STDOUT redirected to device NUL to avoid printing lots of blank lines on batch execution to screen. And to the file text.txt nothing is redirected from STDERR as there is no error message printed by command ECHO resulting in creating an empty test.txt file.
Run in a command prompt window for /? for help on command FOR.
I am looking to create a batch file to search one of my workstations for a specific file by name. If the file is found I want to output the report into a txt or docx.
Below is what I have so far but it is only searching the root directory:
#echo off
C:
if exist C:\test.* (echo FILE_EXIST) else (echo FILE DOES NOT EXIST)
pause
Can someone please provide me with a sample of the batch that I need to make this work? Thank you
That's simple i suppose using dir
#echo off
C:
dir /s/b test.*
pause
It will show file not found if there is no file with the name and will show the complete path of the file where the file(s) with that name is/are.
If you want the reports to be stored in a file use dir /s/b test.* >reports.txt instead.
The reports then will be stored in the file "reports.txt" in the very directory.
I want to write a batch file in DOS which reads all the files from the input directory and display the list of files on the screen using FOR loop.
dir for the files in the directory
dir /s if you want to include the files in the sub-directories.
microsoft command line reference
edit:
FOR %%i IN (*.*) DO echo %%i
This will print out the names of all the files in the directory you run the batch file in.