IF multiple conditions - windows

F.e. there is some folder on user's desktop. I want to remove any files within this folder except shortcuts. There are two types of shortcuts - to some local\network resources and shortcuts to some sites.
"shortcuts on some local\network resources" have .lnk extension
"shortcuts to some sites" have .url extension
I've already found how to do this task separately
forfiles /p "%userprofile%\Desktop\folder" /s /m *.* /c "cmd /c for %G in (#path) do #if /I [%~xG] neq [.lnk] del /F /Q %G"
and
forfiles /p "%userprofile%\Desktop\folder" /s /m *.* /c "cmd /c for %G in (#path) do #if /I [%~xG] neq [.url] del /F /Q %G"
But how to combine these conditions into one string? Something like if /I [%~xG] neq [.lnk] AND [.lnk] ...

There's absolutely no need to use a for-loop within your forfiles /C command.
Try either;
ForFiles /P "%UserProfile%\Desktop\folder" /S /C "Cmd /C If /I Not #ext==\"lnk\" If /I Not #ext==\"url\" Del /A /F #path"
Or use the hex codes as shown in the help information, available at the Command Prompt by entering, forfiles /?
ForFiles /P "%UserProfile%\Desktop\folder" /S /C "Cmd /C If /I Not #ext==0x22lnk0x22 If /I Not #ext==0x22url0x22" Del /A /F #path"

Related

Suppress output string using FORFILES command

I want ignore the seconds of the output #ftime using forfiles:
forfiles /P "%userprofile%\Desktop\folder" /M "test.rar" /C "cmd /c echo sucessufull at %#ftime:~0,2%:%#ftime:~3,2%h"
Tried using this way, but I think forfiles doesn't allow use of %.
Here's a basic example of your command directly using forfiles, to stay on topic:
#ForFiles /P "%UserProfile%\Desktop\folder" /M "test.rar" /C "Cmd /V /C Set _=#FTime&Echo successful at !_:~,5!"
And a more complete/robust example:
#"%__AppDir__%forfiles.exe" /P "%UserProfile%\Desktop\folder" /M "test.rar" /C "0x22%__AppDir__%cmd.exe0x22 /V /C If #IsDir==FALSE (Set 0x22_=#FTime0x22 & Echo successful at !_:~,5!)"
The special #-variables of forfiles, like #ftime, have nothing to do with normal environment variables, which %-signs are used for to expand them.
There are two options:
Capture the output of forfiles with for /F:
for /F "tokens=1-2 delims=:" %%I in ('
forfiles /P "%UserProfile%\Desktop\Folder" /M "test.rar" /C "cmd /C if #isdir==FALSE echo #ftime"
') do (
echo successful at %%I:%%Jh
)
nest for /F loop inside of forfiles:
forfiles /P "%UserProfile%\Desktop\Folder" /M "test.rar" /C "cmd /C if #isdir==FALSE for /F 0x22tokens=1-2 delims=:0x22 %%I in (0x22#ftime0x22) do echo successful at %%I:%%Jh"
The hex code 0x22 stands for a quotation mark " and is only understood by forfiles.

How to delete files in a location except executable files

I have created a batch script to delete 7 days older files as follows:
ForFiles /p "C:\Users\John.Kapen\Desktop" /s /d -7 /c "cmd /c del #file"
But this is deleting executable files and folders as well.
I tried below command to exclude executable file deletion. But not working:
for /f %%F in ('C:\Users\John.Kapen\Desktop /b /d -7 /a-d ^| findstr /vile ".exe"') do del "%%F"
Could someone help me here?
The forfiles /? "manpage" contains following information:
...
#ext - returns only the extension of the
file.
...
Examples:
...
FORFILES /M *.txt /C "cmd /c if #isdir==FALSE notepad.exe #file"
You can try something like (not tested):
ForFiles /p "C:\Users\John.Kapen\Desktop" /s /d -7 /c "cmd /c if NOT #ext=="""exe""" del #file"

batch delete old files and empty folders

I have a batch file that works on my local machine (Windows 7), but doesn't fully work on the server it is intended to live on(Windows 2008 R2 Service Pack 1).
It needs to delete files older than a specified number of days in all listed folders, then delete empty folders and sub-folders.
Script:
set DaysOld=3
set "folders[0]=C:\Test\BatchDel\1"
set "folders[1]=C:\Test\BatchDel\2"
set "folders[2]=C:\Test\BatchDel\3"
set "folders[3]=C:\Test\BatchDel\DNE"
for /F "tokens=1* delims==" %%s in ('set folders[') do (
forfiles /p "%%t" /s /m * /D -%DaysOld% /C "cmd /c del #path"
)
for /f "delims=" %%d in ('dir /ad/b/s ^| sort /R') do rd "%%d"
pause
When run on my local machine (spacing added):
C:\Test\BatchDel>set DaysOld=3
C:\Test\BatchDel>set "folders[0]=C:\Test\BatchDel\1"
C:\Test\BatchDel>set "folders[1]=C:\Test\BatchDel\2"
C:\Test\BatchDel>set "folders[2]=C:\Test\BatchDel\3"
C:\Test\BatchDel>set "folders[3]=C:\Test\BatchDel\DNE"
C:\Test\BatchDel>for /F "tokens=1* delims==" %s in ('set folders[') do (forfiles /p "%t" /s /m * /D -3 /C "cmd /c del #path" )
C:\Test\BatchDel>(forfiles /p "C:\Test\BatchDel\1" /s /m * /D -3 /C "cmd /c del#path" )
ERROR: No files found with the specified search criteria.
C:\Test\BatchDel>(forfiles /p "C:\Test\BatchDel\2" /s /m * /D -3 /C "cmd /c del#path" )
ERROR: No files found with the specified search criteria.
C:\Test\BatchDel>(forfiles /p "C:\Test\BatchDel\3" /s /m * /D -3 /C "cmd /c del#path" )
ERROR: No files found with the specified search criteria.
C:\Test\BatchDel>(forfiles /p "C:\Test\BatchDel\DNE" /s /m * /D -3 /C "cmd /c del #path" )
ERROR: The specified directory does not exist.
C:\Test\BatchDel>for /F "delims=" %d in ('dir /ad/b/s | sort /R') do rd "%d"
C:\Test\BatchDel>rd "C:\Test\BatchDel\3"
C:\Test\BatchDel>rd "C:\Test\BatchDel\2"
C:\Test\BatchDel>rd "C:\Test\BatchDel\1"
The directory is not empty.
C:\Test\BatchDel>pause
Press any key to continue . . .
When I run it on the server it asks for permission to delete each folder. (spacing added).
F:\data\Scripts>set DaysOld=5
F:\data\Scripts>set "folders[0]=F:\data\Environments\TestA\Cache_Data"
F:\data\Scripts>set "folders[1]=F:\data\Environments\TestB\Cache_Data"
F:\data\Scripts>for /F "tokens=1* delims==" %s in ('set folders[') do (forfiles /p "%t" /s /m * /D -5 /C "cmd /c del #path" )
F:\data\Scripts>(forfiles /p "F:\data\Environments\TestA\Cache_Data" /s /m * /D -5 /C "cmd /c del #path" )
F:\data\Environments\TestA\Cache_Data\0432f59d-9fd1-46ed-8579-9ebe358113fb\*, Are you sure (Y/N)?
I need it to delete the empty folders on the server without pressing Y for each one.
EDIT:
I updated the script. Thanks aschipfl! it is now closer to working. It is now asking Y/N to delete each folder.
EDIT 2: updated the outputs.
I found this solution:
for /F "tokens=1* delims==" %%s in ('set folders[') do (
forfiles /p "%%t" /s /m *.* /D -%DaysOld% /C "cmd /c del #path"
for /f "delims=" %%d in ('dir "%%t" /s /b /ad ^| sort /r') do rd "%%d"
)

How to use multiple commands in batch using forfiles command?

#ECHO OFF
SET backdir=backup
SET snapshotdir=snapshots
SET worldprefix=world_
SET itdate=%date:~10,4%-%date:~4,2%-%date:~7,2%
SET hour=%time:~0,2%
IF "%hour:~0,1%" == " " SET hour=0%hour:~1,1%
echo Current date: %itdate%. Current hour: %hour%. Current Minute:Second: %time:~3,2%:%time:~6,2%
forfiles /m "%worldprefix%*" /c (
echo Copying World: #path
cmd /c xcopy /e /c /h /i /v /r /y /q #file %snapshotdir%\#file\%itdate%-%hour%-%time:~3,2%-%time:~6,2%
cmd /c xcopy /e /c /h /i /v /r /y /q #file %backdir%\%itdate%D\worlds\#file
)
echo Copying Plugins
xcopy /e /c /h /i /v /r /y /q plugins %backdir%\%itdate%D\plugins\
xcopy /e /c /h /i /v /r /y /q %backdir%\%itdate%D %backdir%\%itdate%-%hour%H\
echo Backup Complete (assuming no errors above). Attempting to remove old files..
forfiles /p "%snapshotdir%" /c "cmd /c rmdir /s /q #path" /d -7
forfiles /p "%backdir%" /m "*H" /c "cmd /c rmdir /s /q #path" /d -2
forfiles /p "%backdir%" /m "*D" /c "cmd /c rmdir /s /q #path" /d -14
PAUSE
I am trying to copy all files with "world_" as a prefix. I run into a problem when I try to use multiple commands in a loop. I have attempted to write the batch script I want above.
The two commands have absolutely nothing in common, so no, you cannot use parentheses like that.
You must execute all the commands within a single CMD /C. You can concatenate commands on one line using &. I've defined a simple XCOPY "macro" to save a bit of typing.
set XCOPY=xcopy /e /c /h /i /v /r /y /q"
forfiles /m "%worldprefix%*" /c "cmd /c echo Copying World: #path&%XCOPY% #file %snapshotdir%\#file\%itdate%-%hour%-%time:~3,2%-%time:~6,2%&%XCOPY% #file %backdir%\%itdate%D\worlds\#file"
If you escape the quotes, then you can use line continuation to split the logical line accross multiple lines. But then you must also escape the &.
set XCOPY=xcopy /e /c /h /i /v /r /y /q"
forfiles /m "%worldprefix%*" /c ^"cmd /c ^
echo Copying World: #path ^&^
%XCOPY% #file %snapshotdir%\#file\%itdate%-%hour%-%time:~3,2%-%time:~6,2% ^&^
%XCOPY% #file %backdir%\%itdate%D\worlds\#file^"
Or you could put the & in the front of the line so that you don't need to escape it. The line continuation also escapes the first character of the next line:
set XCOPY=xcopy /e /c /h /i /v /r /y /q"
forfiles /m "%worldprefix%*" /c ^"cmd /c ^
echo Copying World: #path^
&%XCOPY% #file %snapshotdir%\#file\%itdate%-%hour%-%time:~3,2%-%time:~6,2%^
&%XCOPY% #file %backdir%\%itdate%D\worlds\#file^"
I realize this is an old post, but another way of doing it is to call a batch file and pass it the parameters that it needs. And I do not believe that there is any limitations in what can be in that batch file. For example:
forfiles /M "*.img" /C "cmd /c ProcessFile.bat #file #fdate #ftime"

How can I remove quotes from forfiles variable fname

Can anyone explain me how to do this?
Problem is #fname contains quotes so concatting %source% and #fname gives an error...
forfiles /P "%source%" /M %file%.* /D -1 /C "cmd /c if exists %source%\#fname.pdf del #path"
The double quotes are not the issue. You've got a syntax error in your command line: instead of if exists … it should go if exist ….
For anyone interested this is the full script.
#echo off
set ERRORLEVEL=0
::variables
set source=C:\ASWFORM\argus
set ps2pdf=C:\Progra~1\gs\gs9.07\gs\lib\ps2pdf
::parameters
if [%1]==[] goto hell
if [%2]==[] goto hell
set file=%1
set hotfolder=%2
:: *********************************************************************************************
::delete files from yesterday
forfiles /P "%source%" /M %file%.* /D -1 /C "cmd /c if exist %source%\#fname.pdf del #path"
::create pdf files
forfiles /P "%source%" /M %file%.ps /C "cmd /c call %ps2pdf% #path"
::move files to hotfolder
xcopy /Y /V %source%\%file%.pdf %hotfolder%
xcopy /Y /V %source%\%file%.xml %hotfolder%
xcopy /Y /V %source%\%file%.adj %hotfolder%
forfiles /P "%source%" /M %file%.* /C "cmd /c if exist %source%\#fname.pdf del #fname.ps"
goto heaven
:hell
echo Usage argus.bat [filename without extension] [path to archive hotfolder]
:heaven
exit 0
:: *********************************************************************************************

Resources