Was writing a short script to search out all files with a certain extension, rename them and delete them... but I noticed some odd behavior in the delete script.
Let's say I have four files: file.lo, file.log, file.logs, & file.logarithm
If I use the command del /s /q /f *.lo then only file.lo is deleted. If I use the command del /s /q /f *.logs then only file.logs is deleted.
But if I use the command del /s /q /f *.log then .log, .logs, and .logarithm are deleted. The only file that remains is file.lo
Can anyone explain this behavior?
Explanation: file.logarithm matches the 8.3 short name file.log
Workaround: del file.log.
According to this answer, using an extension of exactly 3 characters in a search pattern causes the search to also return matching files with longer extensions, and otherwise allways finds exactly the right files. You should apparently use "file?.log", "file.log." as Stephan suggests, or one of these suggestions
Related
I'm trying to create a batch file in Win7 that will copy any files that have been created or modified today and copy them to a destination with a similar directory structure. This is what I have so far:
set today="20180721"
robocopy "C:\temp\" "D:\backup\temp\" *.* /s /DCOPY:T /MINAGE:%today%
I know that /e copies empty directories and /xf excludes all files, but I'm not sure if that helps me. The code above seems to copy all files regardless of date, so I'm a little lost here.
Assigning quotes to your variables is not a best practice and will cause problems with some commands if you try to quote the variable later on. Regardless that was not your problem. Your problem is you need to use the /MAXAGE option. Reading the help file you should see this:
/MAXAGE:n : MAXimum file AGE - exclude files older than n days/date.`
So your code should be:
set "today=20180721"
robocopy "C:\temp\" "D:\backup\temp\" *.* /s /DCOPY:T /MAXAGE:%today%
Going to assume you thought the options were for INCLUDE.
robocopy's /MINAGE//MAXAGE options regard the full date and time, so specifying something like /MAXAGE:1 filters for files that have been modified within the last 24 hours.
If you want to process files which have been modified today only, hence regarding the date but not the time, you could use forfiles and its '/D' option, like this:
set "DEST=D:\backup\temp"
forfiles /P "C:\temp" /D +0 /C "cmd /C if #isdir==FALSE for %%Z in (#relpath) do #(2> nul md 0x22%DEST%\%%~Z\..0x22 & copy #relpath 0x22%DEST%\%%~Z0x22)"
I would like to make a batch script that can delete multiple directories at once. I've searched the web, but only seem to find methods for deleting multiple sub-directories or files. So far I can make a script to delete one specific folder using this structure:
rmdir "C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Cookies" /S /Q
I then tried a multitude of methods to declare other folders in different combinations and sequences but none seem to work. First I tried putting all other directories in a single line followed by /S /Q, then placing rmdir and /S /Q at the start and end of each new path, then putting each path on a new line by pressing enter. Am I using the wrong commands? Any help is appreciated.
for %%a in ("dirname 1" "dirname2" "as many as you want") do rd /s /q "%%~a"
should do what you want - %%a is set to each [optionally-quoted] argument in turn. You Must use the quotes if the directoryname contains separators like spaces.
You can just use a for loop to iterate over a list of directories, as per:
pax> for %d in (c:\dir1 c:\other\dir2) do echo %d
c:\dir1
c:\other\dir2
In your particular case, it would be something akin to (in a cmd file):
for %%d in (c:\dir1 c:\other\dir2) do rmdir "%%~d" /s /q
You can find subdirectories by name automaticly and delete them
for /f %%d in ('dir /b "C:\exampledir\searched_dir_name*"') do rd /s /q "C:\exampledir\%%d"
this command will delete all directories with name "searched_dir_name" in directory C:\exampledir\
Newbie question.
Would cd "C:del *.* delete all files within the C: drive?
I was also told if I add del . /F /Q it would bypass confirmation of the deletion, but I'm not 100% sure where to put it. Do I just add it onto the end like
cd "C:del *.* del . /F /Q?
This is a dangerous command. Be careful.
No, you probably want del /F /Q ..
The first part, del, is the command. This is telling it to delete something.
/F /Q are the options, denoted by the leading slash.
/F means to force it, so for example if a file isn't writable it will still delete it if able. It also skips some other checks.
/Q tells the program to be 'quiet', so less output will be generated than normal
.(dot) is the thing to delete, which is the place you currently are. If you are currently in C:\ (ie if the terminal displays C:\> at the beginning of every line), then it will be that that it deletes.
Note that I have only shown what you should type (del /F /Q .) and not what the entire line should look like when you're done (C:\> del /F /Q .). However, if this is going in a batch file then you want the first form only.
This is a destructive command and is provided merely to answer the query - don't try it at home or prank friends with it.
This will delete all files that are not locked, or opened for exclusive access by a program.
del c:\* /s /f /q /a
It will not delete every file in a Windows system drive and will not remove directories.
I would like to modify the following code to better target a set of folders.
I have the following folder structure:
C:\Root\Dir1\Data, C:\Root\Dir2\Data, ...
D:\Root\Dir1\Data, C:\Root\Dir2\Data, ...
The C: contains working copies, and the D: contains backup copies to be restored on-demand. With the help of #Marged, I utilize the following batch file:
for /d %%g in ("C:\Root\*") do rd /s /q "%%g\Data"
xcopy /s /q "D:\Root" "C:\Root"
pause
I now need a way to specify %%g to exclude folders in C:\Root\*. In example, I want line 1 to delete C:\Root\Dir1\Data and C:\Root\Dir2\Data but not C:\Root\Dir3\Data. It is required to make the exclusions explicit, as the set of inclusions change. I also need line 2 to better target folders. However, I believe that can be accomplished with XCOPY's /EXCLUDE:"D:\Root\Dir3" switch.
Thank you very much for reading and in advance for any discussion! :)
... do if "%%~g" neq "C:\Root\Dir3" rd ...? – Stephan
I have a follow bat command which will delete unnecessary files when i give respective file extension
#ECHO OFF
REM Change the path and the extension...
DEL /s /f /q "C:\Users\dell\Desktop\test\*.pdf"
DEL /s /f /q "C:\Users\dell\Desktop\test\*.csv"
ECHO Are you sure ? Press a key to continue.
PAUSE > NUL
ECHO Done
PAUSE
I am in need to insert specific sub-folder to delete along with files. Assume specific sub-folder name may be abc
Can any body help me on this how to insert "delete specific sub-folder"
Use the RD command to remove a (sub)directory. Since a directory may not be empty, you may want to add the /S switch to remove the entire directory tree. And since you are also going to use the command in a batch script, you may need the /Q switch as well, to avoid the confirmation prompt.
So add a command like this to your script:
RD /S /Q "C:\Users\dell\Desktop\test\Specific\Subfolder"