I have a simple line in a bat file. The line works perfect in a command window but when I add it to a bat file it does not do anything. The line that I am using in below.
forfiles -p E:\Apps\data\Upload-Copy -s -m *.* /d -120 /c "cmd /c del #file"
My goal is that I have multiple locations that I would like to add to this bat file and set up a scheduled task to run it daily.
I have also tried this and it didn't work either.
ForFiles /p "E:\Apps\data\Upload-Copy" /s /d -120/c "cmd /c del /q #file"
I have tried with no luck again this works just by typing in Command prompt but not in the .bat file
%SystemRoot%\System32\forfiles.exe /P "E:\Apps\data\Upload-Copy" /S /C "%SystemRoot%\System32\cmd.exe /D /C 0x22If #IsDir==FALSE Del /F /A #File0x22" /D -120
I have the bat file stored in the root of the C:drive and verified that the path is correct. I am just double clicking on the file C:\File2Delete.bat and the command prompt flashing on the screen for a second or less but does not delete any of the files
this will be on Windows 2008 R2 server and Windows 2019 server
Related
I have made a batch file to robocopy /MOVE files with xxxx* in the file name to a temp folder, then a second command using forfiles to delete any files in the original directory older than -xx days old, then using the robocopy /move to move all the other files back to the original directory. Is there a way I can run the forfiles command to delete all files older than -xx days EXCEPT files with xxxx* in the name from the original director without moving files back and forth?
My original code is:
forfiles -p "%USERPROFILE%\Documents\Media\TV" -s -m . -d -45 -c "cmd /c del #path"
Here is one option using FORFILES with some added help of the FOR and FINDSTR commands. But I believe you could do this all with ROBOCOPY if you wanted to .
FORFILES /S /D -45 /C "cmd /q /c FOR /F %%G IN (#file) do echo %%~G|findstr /v /b xxxx >nul && del #path"
I'm working with an application that creates a generic file type extension for data log information. I'm trying to create a batch file script that would delete data log files that are 5 days or older. I don't have the most Batch File experience, but I have found the following script below works correctly for many file types, except for the generic .File type extension.
forfiles /p "C:\SOAP_Data" /s /m *.* /d -5 /c "cmd /c del #path" &
forfiles /p "C:\HL7_Data" /s /m *.* /d -5 /c "cmd /c del #path"
Is there some tweak I can do to the script to make it include the deletion of the .File extension files as well? I've tried to add *.file to the folder path, but that didn't seem to work.
Through troubleshooting using the ECHO of the delete path, I was able to find the following solution:
forfiles /p "C:\SOAP_Data" /d -5 /c "cmd /c DEL #path" &
forfiles /p "C:\HL7_Data" /d -5 /c "cmd /c DEL #path"
can anyone pls explain the below command
FORFILES /S /M *.imp /C "cmd /c if #fsize gtr 200000 del #file"
I want to delete the .imp file if the file is more than 200kb. I got the command but when I try to put the path //flxxx/g_ff_gg$/CDO_MTK_SMT/Hari krishna/gggg_SCRIPTS/ I am getting an error. Where shall I put the path in the above command.
or is there any command to check the file which is more than 200kb and deletes the file with the given directory
One possible solution, I use in such cases, is to first change into the directory. Then call the command and final leave. pushd and popd are made for such actions.
pushd \\ComputerName\ShareName\Directory1\Directory2
FORFILES /S /M *.imp /C "cmd /c if #fsize gtr 200000 del #file"
popd
I found a sample batch file I am trying to use to delete files after they are 5 days old. I've been testing it using the "echo" command and it works. The only problem I am having is that I want it to delete every file inside of the folder but not the folder.
forfiles -p "E:\Programs\SickBeard\SickBeard\lib" -s -m *.* -d -5 -c "cmd /c echo #file"
I've tried changing the path to "...\lib\" and I've tried changing #file to #path but none of these seem to work.
How can I specify all files inside the folder to be deleted but not the folder itself? I am going to be using task scheduler to run this file once a day. Thank you in advance.
EDIT: Oh and for the record, that is just a test folder. Also, will this work on Windows 8.1? I am currently using Windows 7 and it seems to be working fine, but I will be using this on a new Windows 8.1 build.
By default forfiles command deals only with the files and not with folders. So your command is absolutely fine (should be on Win8 too..).
If you want to use the forfiles command for deleting the folders, below is one such example.
forfiles -p "C:\Sample" -d -5 -c "cmd /c IF #isdir == TRUE rd /S /Q #path"
Cheers, G
Here you go. Delete those files you want and avoid the pesky No files found "error" and the corresponding errorlevel of 1--while retaining other more valuable errors from the FORFILES command:
cmd /e:on /c "forfiles /p "<insert your directory here>" /s /m *.* /d -5 /c "cmd /c del #path" 2>&1 | findstr /V /O /C:"ERROR: No files found with the specified search criteria."2>&1 | findstr ERROR&&EXIT 1||EXIT 0"&EXIT %errorlevel%
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