Batch file (ForFiles multiple conditional commands for logic)? - batch-file

I am just getting into Bat files.
I am trying to delete old folders on a network shared drive but skip 2 of the containing folders by name.
Basically I need to all files that I make daily and always keep 2 old files.
Code that deletes all files that are older than 3 days:
PushD "\\****-****\build" &&(
ForFiles /D -3 /C "CMD /C if #ISDIR==TRUE echo RD #FILE &RD /S #FILE
) & PopD
And I was thinking something like this: if NOT #FNAME == %name%. I don't totally understand the process, am I able to have two conditions in the forFiles? do I have to have /c before?
PushD "\\****-****\build" &&(
ForFiles /D -3 /C "CMD /C if NOT #FNAME == %name% if #ISDIR==TRUE echo RD #FILE &RD /S #FILE
) & PopD
I can't seem to get it, would you mind helping me out?
Thanks!

Yes, nesting if commands in their then branches is the way how-to have logical AND. Note proper quoting in next code snippet:
#ECHO ON >NUL
#SETLOCAL enableextensions
set "name=SO"
set "nam2=SU"
pushd "D:\VB_scripts"
#rem all directories
ForFiles /D -2 /C "CMD /C if #ISDIR==TRUE echo #FILE"
#rem all directories except "SO"
ForFiles /D -2 /C "CMD /C if #ISDIR==TRUE if not #FNAME=="""%name%""" echo #FILE"
#rem all directories except "SO" and "SU"
ForFiles /D -2 /C "CMD /C if #ISDIR==TRUE if not #FNAME=="""%name%""" if not #FNAME=="""%nam2%""" echo #FILE"
popd
#ENDLOCAL
Output:
==>D:\bat\SO\31346676.bat
==>set "name=SO"
==>set "nam2=SU"
==>pushd "D:\VB_scripts"
==>ForFiles /D -2 /C "CMD /C if #ISDIR==TRUE echo #FILE"
"Class Pack"
"Oldies"
"SO"
"SU"
"WMI"
==>ForFiles /D -2 /C "CMD /C if #ISDIR==TRUE if not #FNAME=="""SO""" echo #FILE"
"Class Pack"
"Oldies"
"SU"
"WMI"
==>ForFiles /D -2 /C "CMD /C if #ISDIR==TRUE if not #FNAME=="""SO""" if not #FNAME=="
""SU""" echo #FILE"
"Class Pack"
"Oldies"
"WMI"
==>popd

It is a little bit messy but I was able to do it on a local directory:
#echo off
mkdir Temp\Temp
REM Copy all old file to Temp dir
forfiles -p "%cd%" -m *.* /D -2 /C "cmd /c xcopy #path %cd%\Temp"
REM copy a random file from Temp dir to Temp/Temp dir and then delete it
FOR %%A in (%cd%\Temp\*) do (
COPY "%%A" %cd%\Temp\Temp\
DEL "%%A"
GOTO :Second
)
:Second
Rem copy second random file from Temp dir
FOR %%A in (%cd%\Temp\*) do (
COPY "%%A" %cd%\Temp\Temp\
GOTO :Del
)
:Del
Rem delete all old files from local dir
forfiles -p "%cd%" -m *.* /D -2 /C "cmd /c del #path"
Rem Copy back two random old files to local dir
xcopy %cd%\Temp\Temp\* %cd%
Rem remove Temp dir
rmdir /s /q Temp

Related

Batch script Mix Forfiles Cmd to print a PDF

I'm stuck with this script
echo off
SET pathAdobe="C:\Program Files\Adobe\Acrobat DC\Acrobat\Acrobat.exe"
SET pathDestination=T:\
cd %pathDestination%
(1)
forfiles /P %pathDestination% /M *8.pdf /D +0 /C "cmd /c echo #PATH"
(2)
"C:\Program Files\Adobe\Acrobat DC\Acrobat\Acrobat.exe" /o /h /s /t "%pathDestination%\pdf8.pdf" "MyPrinterName"
pause
(1) Work fine, i got a list of pdf according my forfiles
(2) Work fine, print my file
(3) But when i want to mix the 2 first step that doesn't work like i want
forfiles /P %pathDestination% /M *8.pdf /D +0 /C "CMD /C "%pathAdobe%" /o /h /s /t #PATH"
I got this error:
Error: Invalid argument or option - « Files\Adobe\Acrobat »
I try to escape with ^ " \ but don't change the result
Can't find a solution!
Thanks for any help you can give me :)
J
Your issue is that you are including double quotes, in the wrong places, and that those double quotes require escaping. You can escape those using backward slashes (\"), or by using their hexadecimal character code, (0x22).
Backward slash example:
#Echo Off
Set "pathAdobe=%ProgramFiles%\Adobe\Acrobat DC\Acrobat\Acrobat.exe"
Set "pathDestination=T:\"
CD /D "%pathDestination%" 2> NUL || Exit /B
%SystemRoot%\System32\forfiles.exe /M "*8.pdf" /D 0 /C "%SystemRoot%\System32\cmd.exe /D /C \"\"%pathAdobe%\" /o /h /s /t #Path \"MyPrinterName\"\""
Pause
Hexadecimal character example:
#Echo Off
Set "pathAdobe=%ProgramFiles%\Adobe\Acrobat DC\Acrobat\Acrobat.exe"
Set "pathDestination=T:\"
CD /D "%pathDestination%" 2> NUL || Exit /B
%SystemRoot%\System32\forfiles.exe /M "*8.pdf" /D 0 /C "%SystemRoot%\System32\cmd.exe /D /C 0x220x22%pathAdobe%0x22 /o /h /s /t #Path 0x22MyPrinterName0x220x22"
Pause

Batch script erroring out with thumb.db not found error

I am using following code for deletion of older files
rem declaration des variables
set path=E:\somefiles
set delai_retention=15
rem Purge recursive des fichiers
%WINDIR%\system32\forfiles /p %path% /s /m * /d -%delai_retention% /c "cmd /c del /q #path"
opsexit %errorlevel%
rem Purge recursive des dossiers vides
%WINDIR%\system32\forfiles /p %path% /d -%delai_retention% -c "cmd /c if #ISDIR==TRUE rmdir /S /Q #FILE"
opsexit %errorlevel%
But while running the code every time my script ends with the following error
Could Not Find E:\somefiles\Thumbs.db
How to fix this error?
First, a hint, forfiles works without you need to go to the winsys32 folder; in fact, every .exe in winsys32 is available without that code. Code without that reduces the file lenght, that is good.
Either, some files needs supervision to being deleted. Use del /q /f /s, is more efficient.
Don't use "path" as a name, is a system variable, this will have conflict. And i think that cmd /c is useless.
I think so that is best use #path instead of #file in the other command.
These will be like that:
rem declaration des variables
set epath=E:\somefiles
set delai_retention=15
rem Purge recursive des fichiers
forfiles /p %epath% /s /m * /d -%delai_retention% /c "del /q /f /s #path"
opsexit %errorlevel%
rem Purge recursive des dossiers vides
forfiles /p %epath% /d -%delai_retention% -c "if #ISDIR==TRUE rmdir /S /Q #PATH"
opsexit %errorlevel%
You can also add a >nul if your problem is with the responses. Like that:
rem declaration des variables
set epath=E:\somefiles
set delai_retention=15
rem Purge recursive des fichiers
forfiles /p %epath% /s /m * /d -%delai_retention% /c "del /q /f /s #path 0^>nul 1^>nul 2^>nul"
opsexit %errorlevel%
rem Purge recursive des dossiers vides
forfiles /p %epath% /d -%delai_retention% -c "if #ISDIR==TRUE rmdir /S /Q #PATH 0^>nul 1^>nul 2^>nul"
opsexit %errorlevel%
Now even if have some problem in deleting the file, won't be show.
I hope I helped you.

Batch file to "echo" files older than 7 days

I have tried the following command:
forfiles /P "C:\test\" /S /M *.* /D -7 /C "cmd /c echo #path"
but it returns an error:
screenshot here
I'm not sure what's wrong, the syntax should be fine.
Remove the last backslash in your search path : "c:\test" instead of "c:\test\"
So it should be :
forfiles /P "C:\test" /S /M *.* /D 7 /C "cmd /c echo #path"
/D 7 is "seven days in the future". Unlikely you have such files (except you own one of those awesome time travel machines).
forfiles /P "C:\test\" /S /M *.* /D -7 /C "cmd /c echo #path"

How do I make this bat file output the results to a .txt file?

forfiles /p "C:FILEPATH\TO\BE\DELETED\User_1" /s /m *.* /c "cmd /c Del #path" /d -08
forfiles /p "C:FILEPATH\TO\BE\DELETED\User_2" /s /m *.* /c "cmd /c Del #path" /d -08
forfiles /p "C:FILEPATH\TO\BE\DELETED\User_3" /s /m *.* /c "cmd /c Del #path" /d -08
forfiles /p "C:FILEPATH\TO\BE\DELETED\User_4" /s /m *.* /c "cmd /c Del #path" /d -08
forfiles /p "C:FILEPATH\TO\BE\DELETED\User_5" /s /m *.* /c "cmd /c Del #path" /d -08
#echo off
#echo This purge was successfully completed on the date listed in the file name.
>purgelog_%date:~-10,2%%date:~-7,2%%date:~-4,4%_.txt
( command1
command2
...
commandN
)
If this is stored in my.bat, simply use redirection.
my.bat >my.txt
If you also want stderr to go into the same file, use:
my.bat >my.bat 2>&1
Note that this will not put the purgelog* information go into my.txt because its target file is explicitly specified.

deleting files only in sub folder using .batch

i have a question. I need to delete all files in subfolders but not the sub folder or the main folder older than 1 day.
#echo off
forfiles -p "C:\Users\remote\Downloads" -s -m *.* /D -1 -c "cmd /c del #path"
i have this code which delete all files older than 1 day in downloads but what i need /seek is:
C:\users\remote\downloads\%variable%
and that only the files whitin variable will be deleted. i have many "variables".
it is for my work and as a test i do it local.
FOR /D will allow you to iterate over subdirectories. How about
FOR /D %d IN (C:\Users\remote\Downloads\*) DO ( forfiles /P "%~d" /D -1 /C "cmd /c del #path" )
You'll need to "escape" the percent sign in the iteration variable if you put this command in a script:
FOR /D %%d IN (C:\Users\remote\Downloads\*) DO (
FORFILES /P "%%~d" /D -1 /C "cmd /c del #path"
)

Resources