Print all the files inside a dir through batch file - batch-file

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.

Related

How to delete a specific file in an unknown directory from RAR files?

I need to delete patata.pat files from about 2000 *.rar files, with most of them being inside of \unknown folder\patata\ or \patata\ folders in those rar files, but could be in even deeper directories.
I've tried with no luck
for %%f in (*.rar) do "%ProgramFiles%\WinRAR\RAR.exe" d "%%f" patata.pat
and
for %%f in (*.rar) do "%ProgramFiles%\WinRAR\RAR.exe" d "%%f" %~dp0\patata\patata.pat
I guessed that the last wouldn't work.
How to delete a specific file in an unknown directory from RAR files?
This task can be done with a batch file with the following single command line:
#for /F "eol=| delims=" %%I in ('dir "%~dp0*.rar" /A-D /B 2^>nul') do #"%ProgramFiles%\WinRAR\Rar.exe" d -idcd "%~dp0%%I" "patata.pat" "*\patata.pat"
This command line works even on FAT16, FAT32 and exFAT drives without processing a *.rar file more than once.
It would be also possible to use the following command line in the batch file on execution on an NTFS partition.
#for %%I in ("%~dp0*.rar") do #"%ProgramFiles%\WinRAR\Rar.exe" d -idcd "%%I" "patata.pat" "*\patata.pat"
This command line processing all *.rar files in directory of the batch file does not work correct on FAT16, FAT32 and exFAT drives because of the list of directory entries and the order of the *.rar directory entries changes on each execution of Rar.exe in case of a RAR file is modified because of deleting patata.pat from root of the archive or any directory inside the archive or the entire RAR file if not containing anything else than patata.pat. So RAR files are processed more than once and some RAR files could be skipped by mistake on using this command line on drives with FAT16, FAT32 or exFAT file system.
The difference is that the first command line results in executing by FOR in background with %ComSpec% /c with the DIR command line appended as additional arguments one more command process which executes the DIR command to output all RAR file names with just file name and file extension to STDOUT of the background command process. This output is captured by FOR and next processed line by line. So the changes on the directory entries list caused by Rar.exe do not matter because of a list of file names in memory is processed by FOR instead of the list of directory entries changing during the loop iterations.
The trick is to specify patata.pat once without any path to delete this file in root of a RAR file and once more with *\patata.pat to delete it from RAR file with any path stored in the RAR file. *\ for any path is something special explained in Rar.txt which is the manual of WinRAR console version Rar.exe.
The RAR command d to delete a directory or file from a RAR file and the switch -idcd to suppress the copyright and Done messages are documented also in text file Rar.txt.
The directory path of the batch file referenced with %~dp0 always expands to a string ending with \. For that reason %~dp0 should be never concatenated with an additional backslash with a folder or file name as this results in having finally on execution two backslashes in series and Windows kernel must remove the additional backslash before passing the full qualified file/folder name to the file system.

How to get a list of files and folders of just those folders listed in a text file into a list file?

I created a batch file to export all files, folders, and subfolders in a specified directory to a text file. What I need is to list specific files with subfolders from a text file which includes their path. Batch file will list files and subfolders only included in this text file. For instance, I need to export to a text file only files under those folders as in the input text file.
C:\Users\Username\Documents\test1
C:\Users\Username\Documents\test2
C:\Users\Username\Documents\test3
C:\Users\Username\Documents\test4
Thanks in advance.
Edited to add more details; normally, we use this command to list all files under a directory:
dir > output.txt
I want to apply another approach. I'd like to list files under particular directories only that a text file will include those specific directory paths.
This is text file:
C:\Users\Username\Documents\myfolder1
C:\Users\Username\Documents\myfolder2
C:\Users\Username\Downloads\yet another folder
Although there are many other folders inside Documents and Downloads, only files under folders whose paths listed in my input text file will be listed. Then, all subfolders and files of myfolder1, myfolder2, and yet another folder will be sent to an output text file.
Read a file containing folder names and list files in each of those folders and its subfolders:
#echo off
for /f "delims=" %%a in (file.txt) do dir /s /b "%%a\*"
Adapt the dir switches and file mask to your needs.
This very simple batch file can be used for this task:
#echo off
rem First check existence of folders list file and exit
rem batch file execution if this file does not exist.
if not exist "%UserProfile%\Documents\FoldersList.txt" goto :EOF
rem Delete output list file if already existing from a previous execution.
rem The error message output on file not existing is suppressed by
rem redirecting it from handle STDERR (standard error) to device NUL.
del "%UserProfile%\Documents\OutputList.txt" 2>nul
rem For each folder path in folders list file run the command DIR to output
rem in bare format all files and folders including hidden files and folders
rem in the folder and all its subfolders with redirecting the output written
rem to handle STDOUT (standard output) to the output list file with appending
rem the new lines at end of the list file. The list file is automatically
rem created on first line written to the output list file.
for /F "usebackq eol=| delims=" %%I in ("%UserProfile%\Documents\FoldersList.txt") do dir "%%~I\*" /A /B /S >>"%UserProfile%\Documents\OutputList.txt"
Just a list of files with no folders is produced with replacing DIR option /A by /A-D which means any attribute except attribute directory. That would be the same as using for last command line with the difference that files with hidden attribute set are not output by inner FOR:
for /F "usebackq eol=| delims=" %%I in ("%UserProfile%\Documents\FoldersList.txt") do for /R "%%~I" %%J in (*) do echo %%J>>"%UserProfile%\Documents\OutputList.txt"
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.
del /?
dir /?
echo /?
for /?
rem /?
See also the Microsoft article about Using command redirection operators.

Read File Names in directory and subdirectory

I have scenario where I need to find out all .csv file in current directory and sub directory and execute below command where .csv file name will be replaced dynamically which will convert in .txt (From Comma delimited to tab delimited file).
I have convertme.vbs file which takes from file and to file name as parameter which converts file but not sure how to achieve for all files in directory and sub directory.
Below is sample command I need to execute in batch file but need batch code which will read file names and plug into below command.
cscript convertme.vbs From_CSV_FileName.csv From_CSV_FileName.txt
convertall.bat
FOR /R %%G in (*.csv) do cscript //nologo convertme.vbs "%%~G" "%%~dpnG.txt"

how to find the subfolder files in cmd?

I have tried the below code to copy the list of files in the myfile.txt file. The result files copied the current directory only but I want to copy all subfolder files which I mentioned in the file name on the text file. Is there a way to copy the files from subfolders?
myfile.txt contains the list of file names only.
#echo off
for /f "delims=" %%L in (G:\MyFile.txt) do copy "%%L" G:\

Windows batch file output to another file

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

Resources