how to delete folders but not files in a folder by cmd - cmd

How to delete folders but not files in a folder by cmd?
Such as, there is a direcory:
c:\image\1.png
c:\image\2.png
c:\image\3.png
c:\image\*.png
c:\image\folder1\*.png
c:\image\folder2\*.png
....
Now I don't know how to delete all the folders (and subfiles) folder1, folder2...", only remain the files 1.png, 2.png, 3.png, 4.png ...

for /d %F in (c:\image\*) do rd /s /q "%F"
Double up the percents if used within a batch file

Related

How do i compress multiple folders into zipped folders without creating subfolders?

I am trying to compress multiple folders (about 100) into individual zipped folders. I did this by making a .bat file like so - for /d %%X in (*) do "c:\Program Files\7-Zip\7z.exe" a "%%X.zip" "%%X"
My problem is that it makes a folder inside the zipped folder, but I need it to just compress the insides of the folder into a zipped folder. I can compress the content of each folder individually but that would take hours. If anybody knows how to do this I would be very grateful :)
for /D %%X in (*) do c:\Program Files\7-Zip\7z.exe"%%X.zip" ".%%X*"
pause
just needed to add .\ to to "%%X" (from "%%X" to ".%%d*")

Is it possible to self destruct the directory of a batch and all its contents?

I have a batch which copies some files from a directory and then should delete the directory the batch file is in, but it just deletes the contents and not the folder itself. I tried with rmdir and with del. The folder structure is the following:
F:\Copy\
copy.cmd
files\
something.jar
something2.jar
Copy.cmd should copy the .jar files, to the documents folder of the user and then delete the "Copy" folder, so it leaves no traces and does not occupy disk space, but my solutions always left the empty "Copy" folder.
My three tries
#echo off
title Copy
color 20
Xcopy "%cd%\files" "%userprofile%\Documents"
rmdir %cd%
and
#echo off
title Copy
color 20
Xcopy "%cd%\files" "%userprofile%\Documents"
del %cd%
and like Compo suggested
You cannot remove the directory if it's your current working directory. Just step out of it, then delete it using its absolute or relative path.
#echo off
title Copy
color 20
Xcopy "%cd%\files" "%userprofile%\Documents"
cd ..
rmdir %~dp0 /S
did not work.

Batch-file forcing script to remove folder inside subfolders

I need to delete 4 files, and 1 folder in a specific location and in unknown number of subfolders(different names). I would like to keep this as simple as possible. I managed to make this almost work, without any loops, but it doesn't work with folder removal in subfolders. All 4 files are deleted in main folder and all subfolders, but folder "scss" with all his content is still present in subfolders.
How to force this script to additionally delete folder in subfolders too ??
START /c cmd %this_dir%
DEL gulp_start.bat, .brackets.json, package.json, gulpfile.js /s /q scss
rmdir scss
You will have to use a For Loop.
Example
Add following to your batch.
FOR /d /r . %%d IN (scss) DO #IF EXIST "%%d" rd /s /q "%%d"

Move multiple files from multiple folders to one folder

I am looking for a .bat file that can look inside multiple folders that contain sub-folders that are named the same and move their contents to a specified folder.
Folder Structure:
Main_folder
MainTextures_folder
Props_folder
--model1_subfolder
------Textures_subfolder
----------image files
--model2_subfolder
-------Textures_subfolder
----------image files
--model3_subfolder
-------Textures_subfolder
----------image files
--model4_subfolder
-------Textures_subfolder
----------image files
I need all image files moved from their Textures_subfolder to the MainTextures_folder.
Thanks for any help.
This expects model* folders under the props folder, and that each model* folder has a textures folder. The MainTextures folder is in the Main_folder.
It's untested and should move all files from each textures folder into the MainTextures folder.
#echo off
pushd "Main_folder\Props"
for /f "delims=" %%a in ('dir model* /b /ad') do (
move /-y "%%a\textures\*.*" "..\MainTextures"
)
popd
for /r "Props_folder" %%x in (*.jpg *.png *.bmp *.whatever) move "%%~fx" "MainTextures_folder"
This code will overwrite all existing files.

How to replicate a directory structure using xcopy in windows

I have a text file containing a list of folders.My text file looks like this:
"D:\old\FOLDER1"
"D:\old\FOLDER2"
"D:\old\FOLDER3"
"D:\old\FOLDER4"
"D:\old\FOLDER5"
all these folders have subfolders and files under it
what I want to do is use xcopy to copy FOLDER1, FOLDER2 , FOLDER3 FOLDER4 and FOLDER5 replicate folders ,replicating the structure of those folders
so in output , I want to get
D:\output\bkup\FOLDER1\............Including all subfolders and files D:\output\bkup\FOLDER2\............Including all subfolders and files D:\output\bkup\FOLDER3\.......... Including all subfolders and files
D:\output\bkup\FOLDER4\............Including all subfolders and files
D:\output\bkup\FOLDER5\............ Including all subfolders and files
I have written below script which works fine for one folder
set sourceFolder="D:\old\FOLDER5"
set destinationFolder=%sourceFolder:~7,-1%
echo %destinationFolder%
xcopy /s /e /i /h /r /y %sourceFolder% "D:\output\bkup%destinationFolder%"
but since # of directories to copy is 100+ ,I like to use a for loop or pass the list of directories to copy in a text file , that's what I don't know how to handle it.
please help me ,I'm not expert in batch file writing.
You can use for /f to read (parse if needed) a textfile line by line. Use "delims=" to read the line as a whole. Don't forget to add quotes to prevent having multiple arguments and strip the quotes in the subprocedure
for /f "delims=" %%a in (yourtextfile.txt) do call :docopy "%%a"
goto :eof
:docopy
set sourceFolder=%~1
set destinationFolder=%sourceFolder:~7,-1%
echo %destinationFolder%
xcopy /s /e /i /h /r /y %sourceFolder% "D:\output\bkup%destinationFolder%"
goto :eof
try robocopy, it is more powerfull for your needs, available for XP Prof. or newer:
set "sourceFolder=D:\old\FOLDER5"
set "destinationFolder=%sourceFolder:~7,-1%"
robocopy "%sourceFolder%" "%destinationFolder%" /MIR
This makes a complete MIRror at "%destinationFolder%".
If you want to copy folders from a text file with xcopy, use the following code:
set "sourceFolder=D:\old\FOLDER5"
set "destinationFolder=%sourceFolder:~7,-1%"
for /f "usebackqdelims=" %%i in ("My File With Source Folders.txt") do xcopy /seihry "%%i" "D:\output\bkup%destinationFolder%"
Text file with source folders is "My File With Source Folders.txt".
Taking the solution from this Microsoft forum thread:
To copy folder structure of another without copying files.
Here "ProjectsX" folder is replaced into "ProjectsY" - within SVN directory
/E - include empty directories
/XF * - all files are excluded
/XD ".svn" "obj" - specified directories are excluded
D:\SVN>robocopy ProjectsX ProjectsY /E /XF * /XD ".svn" "obj"
Thanks to the hint to robocopy in the previous answer by Endoro. But you don't need mirroring for this really.

Resources