Using cmd compact with folder name patterns - windows

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

Related

How to delete empty folders with some exceptional folders in shell script?

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

batch file remove all but the newest 10 files

I have the following in a batch file:
:REMOLDFILES
ECHO Removing files older than 14 days. >>%LOGFILE%
cd /d %BKUPDIR%
FOR /f "skip=14 delims=" %%A IN ('DIR /a:-d /b /o:-d /t:c %1*.zip ^2^>nul') DO IF EXIST "%%~fA" ECHO "%%~fA" >>%LOGFILE%
FOR /f "skip=14 delims=" %%A IN ('DIR /a:-d /b /o:-d /t:c %1*.zip ^2^>nul') DO IF EXIST "%%~fA" DEL "%%~fA" >>%LOGFILE%
FOR /f "skip=14 delims=" %%A IN ('DIR /a:-d /b /o:-d /t:c %1*.log ^2^>nul') DO IF EXIST "%%~fA" ECHO "%%~fA" >>%LOGFILE%
FOR /f "skip=14 delims=" %%A IN ('DIR /a:-d /b /o:-d /t:c %1*.log ^2^>nul') DO IF EXIST "%%~fA" DEL "%%~fA" >>%LOGFILE%
IF [%3]==[Y] GOTO SECONDBACKUPDIR
IF [%3]==[y] GOTO SECONDBACKUPDIR
GOTO END
The problem I ran in to is that the backup was not running for a couple of weeks and ended up deleting all of my backups since they were over 2 weeks old.
What I need is for it to keep the last 10 latest backups.
Anyone have an idea how I would do that? I did not write this once since I'm not that familiar with batch files.
You could use FOR /F SKIP to ignore the last 10 most recently modified entries after sorting by last modified date:
for /f "skip=10 eol=: delims=" %%F in ('dir /b /o-d *.zip') do #del "%%F"
You can get a listing of files in reverse order by modified date using the DIR command. Then you just tell your FOR loop to skip the first 10 (note your post code shows 14, but you ask for 10) entries, so whatever is processed is deleted.
REM Update to 14 if needed.
SET Keep=10
FOR /F "usebackq tokens=* skip=%Keep% delims=" %%A IN (`DIR *.zip /B /O:-D /A:-D`) DO DEL "%%A">>%LOGFILE%
Since you are unfamiliar with batch, you can test this command (to see what will be deleted instead of actually deleting it) by replacing DEL with ECHO.
Edit
Since you are also processing log files, why not just delete them in the same loop?
REM Update to 14 if needed.
SET Keep=10
FOR /F "usebackq tokens=* skip=%Keep% delims=" %%A IN (`DIR *.zip /B /O:-D /A:-D`) DO (
ECHO Processing: %%~nA
REM Delete ZIP file.
DEL "%%A"
REM Delete LOG file.
DEL "%%~nA.log"
)>>%LOGFILE%

Using a DIR command to display subdir's with files...suppress subdir's with no files

I am currently running the command dir /b/s *.* on a specific directory that has subdirectories. How do I only get a list of directories that does not show empty directories, but only directories that have files.
Assuming the following structure (and this would be the result of dir /s /b)
test\1
test\2
test\3
test\1\fred.txt
test\2\wilma.txt
For a list of directories containing at least one file:
for /f %i in ('dir /b /s /ad') do #for /f %j in ('dir %i ^| find "File(s)"') do #if not %j==0 echo %i
Results:
test\1
test\2
For a list of files but not including lines that are just the directories:
for /f %i in ('dir /b /s /ad') do #for /f %j in ('dir %i ^| find "File(s)"') do #if not %j==0 dir /b /s %i
Results:
test\1\fred.txt
test\2\wilma.txt
Remember that if this is done inside of a batch file you have to double up the % signs, e.g. %%i and %%j
This will give you a list of folders without files:
#echo off
(for /f "delims=" %%a in ('dir /s /b /ad') do dir "%%a" /b /a-d >nul 2>&1 && echo %%a)>"folderlist.txt"

how do you delete all files NOT a flag in windows command prompt

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.

Setting a start position in a Path(Batch processing)

i'm a newbie in batch scripting,started learning this from last week only and this is the first question that i'm asking here.here is my situation,
Consider this example,this lists all directories under D:/Jose/test1 and append this to a text file.
Code:
#echo off
SETLOCAL EnableDelayedExpansion
cd /d D:\Jose\test1
FOR /F "delims=" %%G in ('dir /ad /on /s /b') DO (
ECHO %%~pG%%~nG>>D:\test2\list.txt
)
ENDLOCAL
pause
Text file output :
\Jose\test1\1
\Jose\test1\2
\Jose\test1\1\12
\Jose\test1\1\13
\Jose\test1\1\12\131
\Jose\test1\1\12\Copy of 131
\Jose\test1\1\12\131\1311
\Jose\test1\1\12\131\1311\13111
\Jose\test1\1\12\131\1311\13112
\Jose\test1\1\12\Copy of 131\1311
\Jose\test1\1\12\Copy of 131\1311\13111
\Jose\test1\1\12\Copy of 131\1311\13112
\Jose\test1\1\13\132
\Jose\test1\1\13\132\1321
\Jose\test1\1\13\132\1321\13211
I want to remove '\jose' from all line ie, i want to set '\test1' as the starting path. Need help guys..Thanks in advance...
try it with sed for Windows:
for /d /r %%G in (*) do sed -r "s/^\\[^\]+(\\.*)/\1/" "%%~pnxG">>D:\test2\list.txt
..solution with pure batch:
#echo off &setlocal
(FOR /f "tokens=2* delims=\" %%a IN ('dir /ad /on /s /b') DO ECHO(\%%~b)>D:\test2\list.txt
TYPE D:\test2\list.txt
PAUSE
..and more batch:
#echo off &setlocal
(FOR /d /r %%G in (*) DO (
SET "fname=%%~G"
SETLOCAL ENABLEDELAYEDEXPANSION
SET "fname=!fname:*\Jose=!"
ECHO(!fname!
ENDLOCAL
))>D:\test2\list.txt
TYPE D:\test2\list.txt
PAUSE

Resources