I'm running this in batch file:
forfiles /p "%~dp0" /c "echo #FILE"
and when I run it, I get this error:
C:\Users\Zach\Desktop\New folder>forfiles /p "C:\Users\Zach\Desktop\New folder\" /c "echo #FILE"
ERROR: Invalid argument/option - '#FILE'.
Type "FORFILES /?" for usage.
What's the problem with #FILE?
%~dp0 includes a tailing backslash. And this is what is causing your problems (this and what monacraft points)
So, change your code to
forfiles /p "%~dp0." /c "cmd /c echo #FILE"
Two things:
Use cmd /c
forfiles /p "%~dp0" /c "cmd /c echo #FILE"
And why use #echo file in the first place?
forfiles /p "%~dp0"
Both should work.
Mona
Related
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
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"
i would like to know how you can delete multiple files in different folders with batch commands.
I have the following code, this code works fine for 1 map but i need to do it for multiple maps :
forfiles /p "D:\CHILI_Publisher\Data\Environments\Adecco\Cache_Data\Assets" /s /d -10 /c "cmd /c echo #file"
PAUSE
This is the code for the various maps and various file types with wildcards (this one gives an error : The directory name is invalid:
forfiles /p "D:\CHILI_Publisher\Data\Environments\*.*\Cache_Data\*.*" /s /d -10 /c "cmd /c echo #file"
PAUSE
Tl;DR : I have an error and would like to know how to use a wildcard correctly in batch files.
You can wrap FORFILES in a FOR loop:
for /d %D in (c:\temp\a*;c:\temp\b*;c:\temp\c*) do forfiles /p %D /s /c "cmd /c echo #file" /d -10
If you need to find all folders named CACHE_DATA under a super folder you can navigate to the super folder (cd D:\CHILI_Publisher\Data\Environments) and run this:
for /f %F in ('dir /B /S /AD cache_data') do for /d %D in (%F) do forfiles /p %D /s /c "cmd /c echo #file" /d -10
If you put the script in a BATCH file remember to escape % with %%.
In the below batch script the line with command FORFILES is not working.
The same statement works fine when it is run separately from a different batch file or from command prompt.
All other statements in the script works fine.
I have gone through all solutions for similar problems.
#echo off
setlocal
SET vFileShare=C:\Users\asande\task\
SET archiveFileList=ArchiveFilesList.txt
SET archFileTimestamp=%date:~10,4%%date:~7,2%%date:~4,2%%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%
rem SET archFileName=%fSignature%%archFileTimestamp%
SET archiveFolder=C:\Users\asande\task\archives\
SET PATH=C:\Program Files\7-Zip
FORFILES /P %archiveFolder% /M *.zip /C "cmd /c del #file" /d -1
IF EXIST %vFileShare%%archiveFileList%. (
cd %vFileShare%
7z a -tzip %archiveFolder%%archFileTimestamp%.zip #ArchiveFilesList.txt
) ELSE (
ECHO %archiveFileList% missing.>>%vFileShare%\Polaris_DatedConversionRate.log.
)
endlocal
GOTO: EOF
Got the answer, the problem actually is due to following 2 statements
SET PATH=C:\Program Files\7-Zip
FORFILES /P %archiveFolder% /M *.zip /C "cmd /c del #file" /d -1
SET PATH ...... statement should be after FORFILES command .
FORFILES command will not work even in command prompt if you set PATH to some value
before running FORFILES.
(it throws FORFILES is not recognized as an internal or external command.)
Can anyone enhance the answer why PATH does so. And i think it effects some other commands like this.
#file is just the file name. Your working directory is probably different from %archiveFolder%, so del doesn't find the files it's supposed to delete, because it's looking in the wrong place (%CD% instead of %archiveFolder%). Try using #path instead:
forfiles /p "%archiveFolder%" /m *.zip /d -1 /c "%comspec% /c del #path"
SET archiveFolder=C:\Program Files\7-Zip
FORFILES /P %archiveFolder% /M *.zip /C "cmd /c del #file" /d -1
I am running a batch file and I have one forfiles command in it
FORFILES -p%spinputarchrootpath% -m*.csv -d-365 -c"CMD /C DEL #FILE"
%spinputarchrootpath% variable maps to a folder location (Y:\Temp Documents\testfolder).
Now the above command is throwing an error because of the space in the folder name (Temp Documents).
How to handle this space? I have tried putting quotes around %spinputarchrootpath% variable but it is not working.
I'd the same problem and found the solution.
I think your folder-variable of the folder you wish to empty has a backslash at the end.
This will NOT work:
echo J|forfiles /P "C:\temp files\" /S /M * /D -7 /C "cmd /c del /F /S /Q #path"
... but this works (without backslash)
echo J|forfiles /P "C:\temp files" /S /M * /D -7 /C "cmd /c del /F /S /Q #path"
Regards
Tino
Enclose the path in quotes:
FORFILES -p "%spinputarchrootpath%" -m *.csv -d -365 -c "CMD /C DEL #FILE"
Note, there's a space between -p and "%spinputarchrootpath%". Without a space in this case it won't work.
As a work around first change directories to the folder you want, and then execute forfiles without the /p parameter.
CD %spinputarchrootpath%
FORFILES -m*.csv -d-365 -c"CMD /C DEL #FILE"
Check post:
How to Tell FORFILES to Execute Command on Path?
The problem lies in the part:
-c"CMD /C DEL #FILE"
Use:
-c"CMD /C DEL ^0x22#FILE^0x22"
to put extra double quotes around the file