For example:
I have folder keep1, sub folder keep2 and sub folder of folder keep2 : keep3:
Keep1(not empty)
Keep2(not empty)
Keep3(empty)
Currently I'm using:
for /f "delims=" %%i in ('dir /s /b /ad ^| sort /r') do rd "%%i"
which deletes all empty folders but I don't want to delete some folders.In above example, I want to keep folder Keep3which is empty.
Could you not do use find?
for /f "delims=" %%i in ('dir /s /b /ad ^| sort /r ^| find /V "Keep3"') do rd "%%i"
?
EDIT
I've seen you might have multiple places you need to skip, so you could use http://www.computerhope.com/findstr.htm findstr
ie if you have to ignore Keep3 and Keep4 then its
for /f "delims=" %%i in ('dir /s /b /ad ^| sort /r ^| findstr /V "Keep3 Keep4"') do rd "%%i"
It also as (basic) regex capabilities
Related
My folder structure is as below:
How could I use compact /s /c only on those folders whose name ends in "Holder" to compress all files recursively within those folders? Thanks
Windows 10 64-bit
Set folder and / or file compress attribute with compact and cmd for loop.
Set the compress attribute for every folder whose name ends with holder. Compress the files that are not system or hidden files in every folder whose folder name ends with holder. Stop if COMPACT encounters an error.
CMD:
FOR /f "tokens=*" %g in ('DIR /ad /b /s "*holder"') do COMPACT /c "%g"
FOR /f "tokens=*" %g in ('DIR /ad /b /s "*holder"') do FOR /f "tokens=*" %h in ('DIR /a-d /b "%g"') do COMPACT /c "%g\%h"
rem un-set un-compress
FOR /f "tokens=*" %g in ('DIR /ad /b /s "*holder"') do COMPACT /u "%g"
FOR /f "tokens=*" %g in ('DIR /ad /b /s "*holder"') do FOR /f "tokens=*" %h in ('DIR /a-d /b "%g"') do COMPACT /u "%g\%h"
SCRIPT:
FOR /f "tokens=*" %%g in ('DIR /ad /b /s "*holder"') do ECHO COMPACT /c "%%g"
FOR /f "tokens=*" %%g in ('DIR /ad /b /s "*holder"') do FOR /f "tokens=*" %%h in ('DIR /a-d /b "%%g"') do ECHO COMPACT /c "%%g\%%h"
rem un-set un-compress
FOR /f "tokens=*" %%g in ('DIR /ad /b /s "*holder"') do ECHO COMPACT /u "%%g"
FOR /f "tokens=*" %%g in ('DIR /ad /b /s "*holder"') do FOR /f "tokens=*" %%h in ('DIR /a-d /b "%%g"') do ECHO COMPACT /u "%%g\%%h"
TEST SCRIPT:
FOR /f "tokens=*" %%g in ('DIR /ad /b /s "*holder"') do COMPACT /c "%%g"
echo.
echo Set compress attribute of folders whose name ends in holder.
echo.
pause
FOR /f "tokens=*" %%g in ('DIR /ad /b /s "*holder"') do FOR /f "tokens=*" %%h in ('DIR /a-d /b "%%g"') do COMPACT /c "%%g\%%h"
echo.
echo Compress and set compress attribute of files in folders whose folder name ends in holder.
echo.
pause
FOR /f "tokens=*" %%g in ('DIR /ad /b /s "*holder"') do COMPACT /u "%%g"
echo.
echo Unset compress attribute of folders whose name ends in holder.
echo.
pause
FOR /f "tokens=*" %%g in ('DIR /ad /b /s "*holder"') do FOR /f "tokens=*" %%h in ('DIR /a-d /b "%%g"') do COMPACT /u "%%g\%%h"
echo.
echo Un-compress and un-set compress attribute of files in folders whose folder name ends with holder.
exit /b
I have a folder contains a list of files. I am using following command to delete all the files except the required files. If there is a file with spaces in the name then following command is failing. Say "File Name with space.txt" or "File 1.txt"
for /f %F in ('dir /b /a-d ^| findstr /vile "file1 file2 file3"') do del "%F"
I tried putting the file names in "" but no success.
You have two options with the FINDSTR command to accomplish this.
The first is to list each file individually with the /C option.
for /f "delims=" %F in ('dir /b /a-d ^| findstr /V /I /L /E /C:"file1" /C:"file2" /C:"file3"') do del "%F"
The other option is put all your search strings in a file, one on each line and use the /G option.
for /f "delims=" %F in ('dir /b /a-d ^| findstr /V /I /L /E /G:"search.txt"') do del "%F"
Trying to get this to work -
FOR /f "tokens=*" %%i in ('dir /s /b *.rar ^| findstr /i ^"par.*1^"' 'dir /s /b *.rar ^| findstr /v /i ^"part^"') DO (
winrar x "%%~i" "v:\unpack\"
)
It works fine with only the first 'dir /s /b *.rar', but I can't seem to chain them together. I know there are other syntaxes I can use, like *.rar and *.zip only but I have to search the dir output as shown. I tried adding a comma between the two dir commands but still makes no difference.
So...is this possible?
Ok, got it working. Here is the cmd line -
'dir /s /b *.rar ^| findstr /i ^"par.*1^" ^& dir /s /b *.rar ^| findstr /v /i ^"part^"'
I am trying to delete all items NOT .aspx, but i wasn't sure how to do it. Right now i was just going through everything with
del /F *.html
del /F *.csproj
del /F *.ico
etc.
Is there a simpler command to just say: DELETE EVERYTHING in DIRECTORY NOT a FOLDER OR has an extension of .aspx
I cant seem to find a solution on the Internet which matches my question. I didn't know if DEL accepted Regex to be like /^.*(^\.aspx)$ or something
bash file says:
chdir C:\Users\william.francis\Desktop\Deploy\tmp
for /f "delims=ÿ" %a in ('dir /b /a-d ^| find /v ".aspx"') do del /s /q /f %a
chdir ..
Edit: would like a reason as to the downvotes so i could fix it. I find this question to be valid enough to not get penalized. shrug
You can do it this way:
for /f "delims=ÿ" %a in ('dir /b /ad ^| find /v ".aspx"') do rd /s /q %a
for /f "delims=ÿ" %a in ('dir /b /a-d ^| find /v ".aspx"') do del /s /q /f %a
Please run it on a test directory first.
Another form:
for /f "delims=ÿ" %a in ('dir /b /a-d') do if not "%~xa"==".aspx" del /q %a
As a batch file:
#echo off
for /f "delims=ÿ" %%a in ('dir /b /a-d ^| find /v ".aspx"') do del /s /q /f %%a
Or
#echo off
for /f "delims=ÿ" %%a in ('dir /b /a-d') do if not "%%~xa"==".aspx" del /q %%a
Save it to a file like file.bat, or file.cmd then run x:\path\to\file.bat or x:\path\to\file.cmd.
I am looking how to get list of all directories to be used in FOR loop.
So far I have work around:
set folderList = (folder1 folder2 folder3 folder4)
FOR %%i in %folderList% do zip %%i D:\...my_path...\%%i\*.*
is it possible that folderList would be generated dynamically ?
assuming you want to list subdirectories of c:\temp
for /f %%i in ('dir c:\temp /ad /b') do echo %%i
this will list foldernames of c:\temp, if you want get it recursively just add /s to dir command:
for /f %%i in ('dir c:\temp /ad /b /s') do echo %%i
as for #dbenham comment (thank you) to correctly handle dirs with space just add tokens=* to for :
for /f "tokens=*" %%i in ('dir c:\temp /ad /b') do echo %%i
Please try below code:
for /d %%F in ("d:\...my_path...\*") do zip "%%~nxF" "%%F\*.*"
I am not sure what is different but the double %% listed above are all failing to work.
This, however, works for me:
for /f "tokens=*" %i in ('dir c:\temp /ad /b') do echo %I