batch code to give paths to files in folders and subfolders - batch-file

I want to get the list of all files in 1 folder (including subfolders). So let's say I have folder A, Subfolers A1,A2 and files B.txt,C.csv,D.json
C:\A\B.txt
C:\A\A1\C.csv
C:\A\A1\D.json
This is just a simple example. The stuff I am using has multiple folders and files in it. So for each file that there is I want output to be
C:\A\B.txt
C:\A\A1\C.csv
C:\A\A1\D.json
in some file named paths.txt
How can this be done?

dir /b /s /a:-d *.txt *.csv *.json>paths.txt

for /F %%A in ('dir /b /s /a-d "C:\A\A1"') do echo %%~dpnxA>>paths.txt
This will grab all files in C:\A\A1 and its subdirectories without including the subdirectories themselves in that list.

Related

Creating files in all subfolders of a file with a bat file

I have a folder with lots of subfolders and I want to create a folder in each of these subfolders. As There is a lot of these folders I was wondering if there was a way of automatically doing this with a bat file.
This should work for your problem
FOR /d %A IN (d:\softwares\files*) DO mkdir %A\source
Run this in the folder in question: it works on all subfolders in the tree.
If you only want to process the immediate set of folders then remove the /s
#echo off
for /f "delims=" %%a in ('dir /b /s /ad ') do md "%%a\folder" 2>nul
echo done
pause

batch file to create a txt file showing a list of folders inside each subdirectory of a parent folder

I need the correct way to get a simple process to repeat till it reaches end of folder. Inside my Main Folder are multiple subfolders. Inside each of these are more subfolders along with a few files.
I need to run the batch from inside the Main folder and have it enter each subfolder in turn and simply run "dir *. > %date%.txt", then do the same to the next subfolder till all are done.
The only part I cannot get to work right is the change directory to each in turn till all are done.
Thanks
#echo off
for /f %%D in ('dir /b /s /ad') do (
pushd %%D
dir *. >"%date%.txt"
popd
)
to append to a existing file you should use >>, try this:
cd /d X:\main &rem put the path to your main folder here
for /r /d %%i in (*) do dir "%%~fi\*.">>"%date%.txt"
This reports all subfolder from X:\main and there subfolder recursively.
Try these options, if you weren't aware of them:
dir "c:\main folder">"%date%.txt"
dir /a-d "c:\main folder">"%date%.txt"
dir /b /s /a-d "c:\main folder">"%date%.txt"
dir /b /s /ad "c:\main folder">"%date%.txt"

delete all folder except one using batch

I have a folder named Scripts in current directory Say C:\QTP\Script\. I am not sure the folders names and count in that scripts folder. Inside each unknown folder name i have Objective Evidences folder. I want a code that deletes all the files and folders in unknown folders but not objective evidences folder.
EG:
Scripts\Folder1\Objective Evidences (this folder1 contains many files too)
Scripts\Folder2\Objective Evidences (This folder2 also contains many files)
I am not sure how many folders are present in scripts folder and wat are their names.
I should get the list of folder names present in Scripts folder and delete all files and folder present in it except Objective Evidences folder.
Please let me know the batch code for the same.
for /f "tokens=*" %%G in ('dir /b /s /a:d "C:\QTP\Script\*"') do (
echo %%~fG | FIND "Objective Evidences" || rd /s /q "%%G"
)
Try this...Or this
pushd "C:\QTP\Script\"
for /D /R %%a in (*) do echo "%%~fa" | find "Objective Evidences" || rd /s /q "%%a"
popd

Batch file command to copy files from one location/directory to another location based on condition

Scenario:
I want to copy the particular file to another locaton.
Destination folder path: D:\Correct\email
Source folder path: C:\Revert\email.
Here in email folder there will be many subfolders and each subfolder contains exactly two files like abc.csv and xyz.csv
so I want to copy the file abc.csv from the latest created folder(ie in the subfolders of email folder) to destination folder and need not to know the name of subfolder.
Thank you in advance.
You can get a sorted list of directories with
dir /B /TC /O-D /AD
You can get the first result into a variable with
for /f "usebackq delims=" %%D in (`dir /B /TC /O-D /AD C:\Revert\email`) do if not defined Newest set Newest=%%~fD
You can then copy the files with
copy /Y %Newest%\*.csv D:\Correct\email

Batch Script - Empty Multiple Directories In Paths With A Pattern

I'm looking to write a short batch script that will delete all files within a set of directories. More specifically, suppose I have the top directory "workspace" and it contains several directories beginning with the sting "project" (e.g. project-something, project-another). Then each of these "project" directories contain a "model" directory. I want to have the script empty each of these model directories.
I know this is doesn't work, but I looking for something along the lines of
del project*\model\*
But I know that the * after project will not select all directories starting with project then proceed into the model directories to clear them. What would be a correct way to go about doing this?
Thank you for your time!
Put this into a .bat file and run.
#echo off
for /F "usebackq delims=" %%F in (`dir /ad /s /b model`) do (
del /s /q "%%F"
echo Removed "%%F"
)
pause

Resources