Suppress output string using FORFILES command - windows

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.

Related

IF multiple conditions

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"

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
:: *********************************************************************************************

Windows batch script - How to filter files with defined extension

I want to do some stuffs on images with extensions defined in a variable.
The following script run well:
set AllowExt="jpg png bmp"
forfiles /p D:\Pictures /m *.* /c "cmd /c if not %AllowExt:jpg=% == %AllowExt% echo #file
But the following script throws error
set AllowExt="jpg png bmp"
forfiles /p D:\Pictures /m *.* /c "cmd /c if not %AllowExt:#ext=% == %AllowExt% echo #file"
ERROR: Invalid argument/option - 'png'.
Type "FORFILES /?" for usage.
You might try this:
set "AllowExt=.jpg .png .bmp"
for %%a in (%AllowExt%) do (
forfiles /p D:\Pictures /m *%%a /c "cmd /c echo #file"
)
"cmd /c echo #file" is the default command, see forfiles /?.
This should get you the filenames you need.
#echo off
set "AllowExt=jpg png bmp"
set "AllowExt= %AllowExt%"
set "AllowExt=%AllowExt: = *.%"
pushd "D:\Pictures"
for %%a in (%AllowExt%) do (
echo "%%a"
)
popd

nested forfiles: path and extension filter

is there any possibility to nest two forfile commands so that I can filter by pathname and by extension and then run a command only on those double filtered files?
By example I'd like to get all Outlook HTML-signatures of all users. I can do this by
forfiles /s /p c:\Users /m *Signatures* /c "cmd /c forfiles /s /p #path /m *.htm"
But this will only display the filenames because it's the default behavior of forfiles to call cmd /c echo #file.
Changing this doesn't work because then I'd need to set the /c-option in the inner forfiles command which requires to set the command in quotes resulting in double quotes:
forfiles /s /p c:\Users /m *Signatures* /c "cmd /c forfiles /s /p #path /m *.htm /c "cmd /c echo #path""
How can I escape the inner quotes or use some different approach to run any command on all files filtered by a substring of path and the file extension?
Kind regardssc911
[edit] forgot /s for recursive searching [/edit]
Instead of forfiles you can use two nested FOR commands.
see the following one-liner example to test in a cmd prompt
#for /d %d in (c:\Users\*signature*) do #for %f in (%d\*.htm) do #echo %f
and use this code as a skeleton to include in a BAT file
for /d %%d in (c:\Users\*signatures*) do (
for %%f in (%%d\*.htm) do (
echo %%f
)
)
FORFILES seems to be able to recognise \" as the way of escaping inner ". So, the following should work:
forfiles /p c:\Users /m *Signatures* /c "cmd /c forfiles /p #path /m *.htm /c \"cmd /c echo #path\""

Resources