delete all folder except one using batch - batch-file

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

Related

Windows rmdir batch to delete FOLDERS but skip files

So i have a dir called c:\user\jdoe\desktop\Folder1\
Inside folder1 i have many folders with subfolder and files.
I have a 7-Zip batch that zips all the folders in place.
So now i have subfolders and subfolders.zip inside folder1.
I need a batch that will detele all the non zipped folders but skip the zip files and the .bat file that lives in c:\user\jdoe\desktop\Folder1\
Any ideas?
If i can get a 7-zip batch that will zip fodlers (example subfolder1.zip subfolder2.zip) and then afterwards delete all non zipped folders that would be great.
Been all over the internet and after deleting half my data by testing my own scripts i have now decided to come here.
So after a long collaboration with a friend we came up with this.
Tested this and it is working 100%
REM create list of directories
SET cwd="C:\Users\user\Desktop\New folder"
SET date=date /T
CD %cwd%
DIR /AD /B >dirlist.txt
REM Zip contents of each directory
for /f "tokens=*" %%a in (dirlist.txt) do (
"c:\program files\7-zip\7z.exe" a "%%a.zip" "%%a"
del /s /q "%%a"
rmdir /s /q "%%a"
)
del dirlist.txt
pause

7zip Batch-file, Wildcard Compress contents of subfolders

I have 5000 uniquely named parent folders. Each parent folder contains 5-10 subfolders. I need each subfolder within the 5000 folders compressed then deleted, leaving me with one .7z file per subfolder while maintaining the 5000 parent folder's structure.
I have always used the below script for compressing my parent folders. However I am unsure of how to change it to compress all folders within the 5000 folders. I assume it will involve some wildcards but extensive google searches have yielded nothing.
for /D %%A in (*) do "C:\Program Files\7-Zip\7z.exe" a -t7z -m9=lzma2 -mx -mmt2 "%%A.7z" -xr!*.bat "%%A"
The easiest solution would be to create a bat file to copy the above bat file into each folder, run it, then delete it. However I am assuming 7z has this functionality and I can just not find it.
Edit: Posted the wrong example
Add an inner loop which will enumerate the subfolders and rd to delete them:
for /D %%O in (*) do (
for /D %%I in ("%%O\*") do (
"C:\Program Files\7-Zip\7z.exe" a -t7z -m9=lzma2 -mx -mmt2 "%%I.7z" -r -xr!*.bat "%%I"
rd /s /q "%%I"
)
)
It will preserve absolute paths in the archives, so if you want to exclude the base folder use cd/d:
for /D %%O in (*) do (
for /D %%I in ("%%O\*") do (
pushd "%%~dpnxI"
"C:\Program Files\7-Zip\7z.exe" a -t7z -m9=lzma2 -mx -mmt2 "..\%%~nxI.7z" -r -xr!*.bat .
popd
rd /s /q "%%~dpnxI"
)
)
Be careful with rd, maybe you'll want to do a test run without it.

batch code to give paths to files in folders and subfolders

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.

Xcopy - Trying to copy a folder with certain arguments to another location

I'm trying to xcopy certain folders from a directory of 5000 folders, that start with the number 9097*
Approx 2000 folders start with 9097.
for /f %%A in ('DIR /b /a:D') do (
set temp=%%A
set ID=!temp:~0,4!
if !ID!==9097 xcopy *!ID!* F:\%INPUT% /S /i
My problem is that when it finds the first folder starting with 9097, the code then goes and copies all contents of all the folders, I want it to only copy the 9097 folders. I understand why the code above is doing what it is doing but I can't seem to think of the way for it to just copy the folders and contents starting with 9097.
Any advice would be greatly appreciated.
for /d %%a in (9097*) do xcopy "%%~fa" "f:\%input%\%%~nxa" /s /i
For each folder with a name starting with 9097, xcopy from the full path of the source folder to the target inside a folder with the name and extension of the source folder.

How can I batch copy certain files from multiple folders into multiple folders?

I am looking to copy files that are like "MAN" that reside inside multiple folders in one directory into all the folders in another directory.
This is what I have now, it copies the files correctly but not into all the folders inside the directory. just into the directory itself.
for /d %%a in ("C:\test123\*") do #copy "%%~Fa\MAN*" /d "c:\Test Destination\*" 2>NUL
to reiterate the problem, the files are copied into c:\Test Destination* but I want it to copy to every folder in that folder. It would be great if the file already exists there, to not copy it at all.
Thanks!
This may work, but is untested. Test it on some sample files.
#echo off
for /r "C:\test123" %%a in (man*) do (
pushd "c:\Test Destination"
for /d /r %%b in (*) do if not exist "%%b\%%~nxa" copy "%%a" "%%b"
popd
)
pause

Resources