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
Related
I have a question about batch files. For instance,
forfiles /p "C:\Users\ ..." /s /m . /D -150 /C "cmd /c del #path"
Is it possible to run this, then change the -150, to -130, -110 automatically instead of writing multiple commands like
TITLE DELETE OLD FILES
forfiles /p "C:\Users\ ..." /s /m . /D -150 /C "cmd /c del #path"
forfiles /p "C:\Users\ ..." /s /m . /D -130 /C "cmd /c del #path"
forfiles /p "C:\Users\ ..." /s /m . /D -110 /C "cmd /c del #path"
PAUSE
Rationale: The system stores data about a semiconductor related equipment and holds files which are about more than 2 years old. However, deleting these files immediately will cause a crash. Hence, I am deleting these files periodically.
I will also appreciate if there is a command which allows the user to choose when to display the current storage space available, and then allows the user to choose whether to delete the next batch of files.
I have asked the same question, "https://stackoverflow.com/questions/72428986/how-to-delete-batch-files-cmd-from-the-oldest-date-first-then-iterate-till-a" and answered the responses. However, I am still stuck and unfamiliar on how to proceed.
Thank you.
I am trying to run a script to clear out the recycle bin on a QNAP NAS periodically.
The problem is the path for the recycle bin on the NAS includes an [at] sign:
"\nas01\SQLBackup\#Recycle" (Had to use double slash here to get it to display correctly)
Can someone please point me in the right direction as to what I'm doing wrong?
Thanks in advance for your help.
Batch File Code
#ECHO ON
NET USE X: "\\nas01\SQLBackup\#Recycle"
forfiles /p "X:\" /s /m * /c "cmd /c del #path"
NET USE X: /delete
PAUSE
Output
C:\Windows\system32>NET USE X: "\\nas01\SQLBackup\#Recycle"
The command completed successfully.
C:\Windows\system32>forfiles /p "X:\" /s /m * /c "cmd /c del #path"
ERROR: Invalid argument/option - '#path'.
Type "FORFILES /?" for usage.
C:\Windows\system32>NET USE X: /delete
X: was deleted successfully.
C:\Windows\system32>PAUSE
Press any key to continue . . .
The reason you get the error is due to the double quotes around the path, where backslash is present. The backslash escapes the last double quote. You should either use:
forfiles /p "X:" /s /m * /c "cmd /c del #path"
or
forfiles /p X:\ /s /m * /c "cmd /c del #path"
anyway, I would not use forfiles at all here. You can quite simply use del /s:
net use X: "\\nas01\SQLBackup\#Recycle"
pushd "x:\">nul 2>&1 && del /Q /S *.* || echo X:\ Not available.
popd
net use X: /delete
pause
Quite simply, we attempt pushd to x:\ if not available, it will fail with a message, if available it will del /s everything on X:\ where /s is basically recursive search throughout the root of X:\ in this instance.
The #-symbol in your path does not cause the error. It is the backslash in:
forfiles /p "X:\" /s /m * /c "cmd /c del #path"
that unintentionally escapes the closing quote (this is specific to forfiles!). To avoid that, simply append a . to the path, like:
forfiles /p "X:\." /s /m * /c "cmd /c del #path"
The . means current directory, so in a path it does not change anything, hence X:\ equals X:\., and D:\some\.\path equals D:\some\path. Of course, you could just remove the quotes around X:\ in your particular situation, but appending . is a general solution that even works with a relative path like X: (meaning the current directory of drive X:), and removal of quotes introduces problems with paths containing SPACEs.
By the way, are you aware that forfiles returns both files and directories, and that del is there to delete just files, and there is rd to delete directories?
So to ensure to handle only files, use this:
forfiles /S /P "X:\." /M * /C "cmd /C if #isdir==FALSE del #path"
And to ensure to handle only directories, use this:
forfiles /S /P "X:\." /M * /C "cmd /C if #isdir==TRUE rd /S /Q #path"
Of course you can handle both, if you want:
forfiles /S /P "X:\." /M * /C "cmd /C if #isdir==TRUE (rd /S /Q #path) else (del #path)"
When you use PushD, it will create a temporary drive map, (allocated in available reverse alphabetical order, Z:..A:), and will then use that new drive. For that reason you should be able to do this without using net.exe.
Example:
#PushD "\\nas01\SQLBackup\#Recycle" 2> NUL && (RD /S /Q . 2> NUL & PopD)
This example uses RD to Remove the Directory instead of your used Del command. When the target directory is the current working directory, it cannot be removed, (returning an error message), however its contents will be. The code above redirects the error message to the NUL device, so that it is not output.
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"
I have written a backup command that deletes files within a E:\Backup\ldap directory that are older than two days. I have tested and this works fine.
(forfiles /p E:\Backup\ldap /s /d -2 /c "cmd /c echo #path >> E:\Backup\files_deleted_log_%date:~-10,2%%date:~-7,2%%date:~-4,4%.txt & cmd /c del /q #path")
I am in the process of adapting this command so that it looks at the top level folders within the specified directory and recursively deletes the folder and its contents if it is older than two days.
(forfiles /p E:\Backup\Sybase /d -2 /c "cmd /c if #isdir == true echo #path >> E:\Backup\files_deleted_log_%date:~-10,2%%date:~-7,2%%date:~-4,4%.txt & cmd /c if #isdir == true del /s /q #path")
I took out the first /s from the previous file command as I do not want to search recursively, I only want to look at the last modified dates of the top level folders within the E:\Backup\Sybase directory.
I also added if #isdir == true to determine whether the file is a folder and updated the delete command to include /s so that a recursive delete is performed.
Each time I execute the command though, nothing is deleted.
I used if #ISDIR==TRUE to determine whether the file is a directory before using RD to recursively remove the directory.
(forfiles /p E:\Backup\Sybase /d -2 /c "CMD /C if #ISDIR==TRUE echo #PATH >> E:\Backup\files_deleted_log_%date:~-10,2%%date:~-7,2%%date:~-4,4%.txt & if #ISDIR==TRUE RD /Q /S #FILE")
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