Can someone explain this batch script? - batch-file

Recently I've been trying to create my own batch commands to make everyday things a lot easier for me. I wanted to create a script that deletes everything within my Temp folder since it is always getting flooded with unnecessary files. I found this code online and it works, but I'm confused on what it's actually meant to do. I understand certain parts of it, but the majority has me confused. I understand it is using a forloop but that doesn't really tell me much. I know logically it makes sense, but I feel like the documentation isn't clear for what the entire script is doing. Anyway, here's the script:
set folder="C:\Users\%USERNAME%\AppData\Local\Temp"
cd /d %folder%
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
Any help would be much appreciated, thanks!

Okay, so the first two lines are very simple. It just sets a variable to a folder path, then CDs into the folder. The /d flag is for changing the drive (C:) if it's currently set to another one.
Now, the last one is a for cycle. What it does is, basically: for each file in dir /b, which shows all files in current directory in bare format (only names), it either executes rmdir, which deletes a directory, or, if rmdir failed because the argument is not a directory, it just dels it. The /s/q stuff in both cases means: /s - delete from all subfolders, /q - do not prompt a Y/N.
Hope I helped!

The first line sets the value of the variable folder.
The second changes the drive and directory to the value assigned to folder.
The third first performs a directory list in basic form (names only) of the current directory (as has just been changed) and assigns each line of the resultant list, in its entirety, to %%i in turn.
The name %%i is then removed using a rmdir or rd command in /q quiet mode and /s including any subdirectories. This works on directories, but not files. The || invokes a del command, again with /s /qwith the same meanings and del deletes files.
hence, any name that is a directory is deleted, and if it's a file, then the rmdir fails and so the del deletes it.
The quotes are to ensure that any name encountered that contains a separator like space or comma is interpreted correctly.

Related

Open multiple directories without knowing the names of said directories and delete folders within them using a Windows batch file

Okay, I do not really know how to describe fully what I want and I feel bad for asking something so advanced. I do not know how to do anything other than move folders and delete files and folders using batch.
I am trying to make a batch file to delete certain Steam files and folders which are used to cache data. I could easily make this for myself and call it a day however I would like to share it and have it work for other people also. The problem is one of the cache folders is named with a unique Steam identifier. I will put what I have made so far below.
REM Makes a temporary folder.
mkdir %LocalAppData%\Temp\steam_cache
REM Deleted all none essential files in Steam's configuration folder.
move "%ProgramFiles(x86)%\Steam\config\config.vdf" %LocalAppData%\Temp\steam_cache
move "%ProgramFiles(x86)%\Steam\config\loginusers.vdf" %LocalAppData%\Temp\steam_cache
rmdir "%ProgramFiles(x86)%\Steam\config" /s /q
mkdir "%ProgramFiles(x86)%\Steam\config"
move %LocalAppData%\Temp\steam_cache\config.vdf "%ProgramFiles(x86)%\Steam\config"
move %LocalAppData%\Temp\steam_cache\loginusers.vdf "%ProgramFiles(x86)%\Steam\config"
REM Delete all none essential files in Steam's userdata folder.
I will put what the file structure goes like below.
%ProgramFiles(x86)%\Steam\userdata\256283931
As you can see the number at the end is completely random and I do not know how to open that directory without knowing the number first. There are sometimes multiple of these folders with random names if you login with multiple account and I would like the batch file to go into them one by one and delete certain folder inside of them.
I will put below the folders that I would like to delete that are inside of the random numbered folder below.
%ProgramFiles(x86)%\Steam\userdata\256283931\ugcmsgcache
Sorry if what I am asking it too much if so just ignore this post, thanks.
What you are looking for is the for /D loop, which enumerates directories:
rem // Enumerate all directories under `userdata`:
for /D %%I in ("%ProgramFiles(x86)%\Steam\userdata\*") do (
rem // Check if there is really a sub-directory called `ugcmsgcache`:
if exist "%%~I\ugcmsgcache\" (
rem // Do something with the full path, like echoing, for instance:
echo "%%~I\ugcmsgcache"
)
)
If you want to ensure that the name of the enumerated directory/-ies name consist(s) of decimal figures only, you could use dir and findstr, together with a for /F loop:
rem // Change into parent directory, because `dir /B` returns plain directory names:
cd /D "%ProgramFiles(x86)%\Steam\userdata"
rem // Enumerate all directories under `userdata`, whose names are pure numbers:
for /F "delims=" %%I in ('
dir /B /A:D "*" ^| findstr /I /X "[0123456789]*"
') do (
rem // Do something with the full path, like echoing, for instance:
echo "%%~fI\ugcmsgcache"
)
The cd command could also be replaced by pushd and popd.
The ~f modifier ensures that the full path is derived, related to the current working directory.
Note that the pipe (|) needs to be escaped here, because it is in the set of the for /F loop.

Batch command to delete everything (sub folders and files) from a folder except one file

First of all, similar questions have been answered in past but not exactly the one I have. In some other solutions, hiding folders/files and changing attributes were suggested and I do not want this, unless there is no simpler way available. Also, I have tried the solution suggested here (and couple of others): MS-DOS command to delete all files except one.
But did not work for my need due to two reasons:
This also deleted the file, I did not want to.
This did not delete the sub-folders but only the files.
So, I have folder c:/users/data and in there, I have 5 folders and 6 files I want to delete everything except one which is web.config and to do that I run the batch file like this:
#echo off
for %%j in (C:\Users\data\*) do if not %%j == Web.config del %%j
pause
But when I run the batch file, this deletes all the files including web.config and also, does not delete any of the sub-folders and if I use the /d switch then it only deletes folders not files. How can I delete both files and folders?
Here is the way I would do it:
pushd "C:\Users\data" || exit /B 1
for /D %%D in ("*") do (
rd /S /Q "%%~D"
)
for %%F in ("*") do (
if /I not "%%~nxF"=="web.config" del "%%~F"
)
popd
The basic code is taken from my answer to the question Deleting Folder Contents but not the folder but with the del command replaced by a second for loop that walks through all files and deletes only those not named web.config.
Note that this approach does not touch the original parent directory, neither does it touch the original file web.config. The great advantage of this fact is that no attributes are going to be lost (for instance, creation date and owner).
Explanation
change to root directory by pushd; in case of failure, skip the rest of the script;
iterate through all the immediate sub-folders of the root by a for /D loop and delete them recursively by rd /S with all their contents;
iterate through the files located in the root by a standard for loop; use the ~nx modifier of the loop variable to expand base name (n) and extension (x) of each file, compare it with the given file name (if) in a case-insensitive manner (/I) and delete the file by del only in case it does not match (not);
restore former working directory by popd finally;
The main problems in your code are:
that you compare more that the pure file name with the prefedined name, so there is not going to be found any match.
you needed to use case-insensitive comparison, because Windows does not care about the case of file names for searching.
you did not put quotation marks around the comparison expressions, so you are likely going to receive syntax error messages, depending on what special characters occur in the file names.
that you are using the del command only which just deletes files; to remove directories, you needed to use rd also.
I'm not sure where the web.config file is stored or if there is more than one, so ...
Only one web.config file
Just lock the file (redirect the file as input) and remove anything else
#echo off
setlocal enableextensions disabledelayedexpansion
pushd "c:\users\data" && >nul 2>nul (
<"web.config" rmdir . /s /q
popd
)
The code will
(pushd) Change to the target folder (we need to be sure this will remove information only from the intended place) setting it as the current active directory and locking it (we can not remove the current active directory). If the command can change to the folder then
(rmdir) Redirect the web.config as input to the rmdir command. This will lock the file so it can not be deleted until the command ends. The rmdir . /s /q remove anything not locked inside (and below) the current active directory
(popd) Cancel the pushd command restoring the previous active directory
Several web.config files in multiple folders
Following the approach (copy, clean, restore) pointed by #Dominique
#echo off
setlocal enableextensions disabledelayedexpansion
pushd "c:\users\data" && >nul 2>nul (
for %%t in ("%temp%\%~n0_%random%%random%%random%.tmp") do (
robocopy . "%%~ft" web.config /s
robocopy "%%~ft" . /mir
rmdir "%%~ft" /s /q
)
popd
)
The code will
(pushd) Change to the target folder (we need to be sure this will remove information only from the intended place). If the command can change to the folder then
(for) Prepare a reference (a random name) to a temporary folder to use
(robocopy) Copy only the web.config files (and their folder hierarchy) from the source folder to the temporary folder
(robocopy) Mirror the temporary folder to the source folder. This will remove any file/folder not included in the temporary copy
(rmdir) Remove the temporary folder
(popd) Cancel the pushd command restoring the previous active directory
Once the web.config files are saved, as the files in the source will match those in the temporay folder, they will not be copied back with the second robocopy call, but any file/folder in source that is not present in the temporary folder will be removed to mirror the structure in the temporary folder.
I also managed to do it, very similar to #aschipfl
For /D %%k in (C:\Users\data) do (For /d %%m in ("%%k\*") do rmdir /s /q "%%m"
For %%m in ("%%k\*") do if not %%m == %%k\Web.config del /q "%%m")

How to delete all files and sub-folders in a folder except for 1 folder in a .bat script?

I have a folder that stores many log files and sub-folders. I would like to delete all files and sub-folders except 1 folder, named Excel_Export, which should not be deleted. I am using the following commands in my batch script:
move D:\ABC\Delete_Test\Retain_Folder D:\ABC
rd /s /q "D:\ABC\Delete_Test"
move D:\ABC\Retain_Folder D:\ABC\Delete_Test
However, after this script runs, even the 'Retain_Folder' is getting deleted except for the files inside it. What is it that I am doing wrong in the above command?
Also, is there a better way to do it?
NOTE:
All the other folders' names (that are to be deleted) starts with the '$' symbol.
This is untested - it deletes all normal files in d:\abc and then removes all folders beginning with $ in the same folder.
#echo off
del "d:\abc\*.*?"
for /d %%a in ("d:\abc\$*") do rd /s /q "%%a"
Your idea is probably the most efficient (fastest) way to accomplish your task, except you have one small bug. Your RD command removes the Delete_Test folder, so you must recreate it before you can move Retain_Folder back to where it belongs. You also probably want to redirect output to null when you move the folder - you don't need to see the move message.
move "D:\ABC\Delete_Test\Retain_Folder" "D:\ABC" >nul
rd /s /q "D:\ABC\Delete_Test"
md "D:\ABC\Delete_Test"
move "D:\ABC\Retain_Folder" "D:\ABC\Delete_Test" >nul
This strategy only works properly if you know that D:\ABC\Retain_Folder does not already exist before you start, or if it does exist, then it must be empty.

batch Moving all files in 100s of folders up one folder

I have a small issue. I'm not super fluent in batch scripting but i'm trying to get better.
I'm trying to get a script working at my current job that we can use to move all files in the last sub-directory of a parent up one directory.
The need is for moving image sequences delivered with a superfluous sub-directory up one sub-directory for effects workflow
essentially i'm being delivered 100s of image sequences as:
Episode/Footage/shot/resolution/shot.0000001.dpx
I would love a batch script i could move into the "Footage" folder that would parse the shot directories below them and move the image sequences out of the resolution folder and place them directly into the shot folder so the structure would look more like this:
Episode/Footage/shot/shot.0000001.dpx
This batch would need to be reused over many instances and just be robust enough to be reusable regardless of what the extensions on the image sequences are or the naming structure of the subfolders are.
for /f "delims==" %%i in ('dir /a:d /b') do for /f "delims==" %%f in ('dir "%%i" /a:d /b') do (move "%%i\%%f\*" "%%i"&&rd "%%i\%%f" /s /q)
The * can be replaced with * . * (no spaces) to only grab files.
Use % instead of %% for use on the command line.
This will grab from any dir under Footage. Delete the first for loop and replace all occuences of %%i with shot to only grab from there.
#!/bin/bash
mv Episode/Footage/Shot/*/* Episode/Footage/Shot
Or for more Fanciness:
#!/bin/bash
echo "Write the file path:"
read file_path
mv $file_path/*/* $file_path/
This will ask you what the path is (This is Episodes/Footage/shot in your example) then find all the files in that path and all the files inside them and put them all in the path.
You could also modify this if instead of putting the files in the path you want them in a different place you could add:
echo "Write the destination file path:"
read file_path2
in between: read file_path and mv ...
and change:
mv $file_path/*/* $file_path
to:
mv $file_path/*/* $file_path2
There is really no need to go through so much trouble.
With the exceptions of the PUSHD and POPD commands, the whole thing can be placed on one line like nephi12 did.
I'm puting everything on separate lines for clairity's sake, but will give two examples of it on one line later.
REM Set working directory
PUSHD C:\Episode\Footage\
REM Find all the sub directories of the working
REM directory, and if any exist execute the loop below:
For /d %%d in ( *.* ) do (
REM Move all files inside the subdirectory up one level.
move /s %%d\*.* %%d\..\.
REM Delete the directories if they are empty.
RD /y %%d
)
REM Return to your original working directory.
POPD
The above code pushes the current directory name into a stack, and changes dirs to Footage.
Then the For /d command grabs all (if any) of the sub directories of Footage, and feeds them one at a time first into the MOVE command, then into the Remove Directory command.
The MOVE /s command moves all files in directory %%d (including any in sub folders) up one level.
Next the RD /y command automatically deletes the now empty directories, or causes a soft error if they are not empty.
The final command returns the working directory to it's original location, POPing the path off the stack.
On one line, without the PUSHD and POPD commands and the directory cleanup, it looks like this:
Single Line Solution:
For /d %%d in ( *.* ) do move /s %%d\*.* %%d\..\.
Single Line Solution with Cleanup:
To include the cleanup just 'add' the and command && after the move command and before the RD, like so:
For /d %%d in ( *.* ) do ( move /s %%d\*.* %%d\..\. & RD /y %%d )
Why I did what I did.
The fastest way to move a whole bunch of files, is not to tell move how and where to move each file, but to use wild cards that will allow Move to figure out by it'self where to put them. So rather than tell it to move one file at a time, I give it wild cards, which allowsMOVE to work at it's fastest. So I only feed it the individual source and destination directories, and say "copy ALL files from here to there".
A set of brackets (...) can hold, what the interpreter considers a single line of code. Which causes a whole new set of problems, but it allows a special variable like %%d to exist on (what to us appears to be) multiple lines of code. I did that at the top. Later, I used another set of brackets to show the command interpreter what is part of a single line is inside a FOR statement, and what is not.
I also used a bit of old DOS shorthand to specify a parent directory, .. which allows me to not know the exact path of something, and still manipulate things in it's parent . As an example,C:\WINDOWS\.. is C:\, and C:\WINDOWS\SYSTEM32\.. is C:\WINDOWS\ .
You might notice that I also used the file specification . . That is probably unnecessary, but I've ran into enough trouble without it that I use it. It just makes clear to MOVE that we are moving these files to a folder, not a file. And, oh yes, MOVE will gladly move hundreds of your files on top of each other, ruining them all but the last one. You can imagine how I came to embrace the . .
The & allows you to string multiple commands, or programs, onto a single line of code, and allows me to tack the RD to the end of the line, and still have access to the special variables that the FOR command created.
So, now if you were paying attention, you'd have realized that my second sentence was a fib. There is nothing stopping you from tacking a PUSHD & and a & POPD onto the beginning and end of that line.

Deleting folders and subdirectories when a file extension is found in it

I'm new to the board so please go easy! :)
Basically, I'm writing a batch file to delete folders that don't have a file with a certain extension type in them.
so for example if i have the following folder
C:\MUSIC\FLAC\JAZZ\Jazzysong.ape
I need a bit of code that can delete the folder JAZZ because .ape is the wrong file type for the FLAC folder.
Any help would be awesome guys as I've tried variants of findstr, exist and writing to text file and trying to read the path using dir /s /b
I'm lost
I'm still unsure as to which kind of test you want to perform on a directory before deciding to remove it – "whether a certain file type exists" or "whether a certain file type doesn't exist". Your post seems to suggests it is the latter, so it must be the title that is misleading. For the purpose of this answer, I'm assuming that it is indeed the "whether a certain file type doesn't exist" test that you want.
Here's a very simplistic and unassuming approach to the problem:
#ECHO OFF
FOR /D /R "D:\directory_with_flac_files_only" %%D IN (*) DO (
IF NOT EXIST "%%D\*.flac" ECHO RD /S /Q "%%D"
)
That is, just traverse the directory tree and, for every subdirectory, check if it doesn't contain the necessary files. If it doesn't, say goodbye to it. (For safety, I added ECHO before the RD command, so that it does indeed say its goodbye but does not perform the real action, just in case you rush into executing the script without reading further.)
That approach is wrong, however. Even though a directory may not contain the required files, its sub-directories might, and this script does not verify that fact. As a consequence, you might lose some of your invaluable FLAC files. (And I've just saved you the grief.)
So, something more elaborate is needed here. The method I came up with consists in the following steps:
Sort the directories into two categories: those that contain the files with the necessary extension and those that don't.
Process the list of directories that do not contain the files:
1) if a directory is found in the other list, omit it;
2) if it is not found in the other list, remove it.
(In case you are wondering: a directory from the first list may indeed be found in the second list as part of its subdirectory's path.)
I'm far from being convinced that it's the best approach possible, but at least it should work (it did work for me, anyway). Here's my implementation of it:
#ECHO OFF
:: set the params
SET "ext=flac"
SET "rootdir=D:\dir_with_flac_files_only"
SET "todelete=%TEMP%\dirs_to_delete.txt"
SET "topreserve=%TEMP%\dirs_to_preserve.txt"
:: clear the temp lists
TYPE NUL >"%todelete%"
TYPE NUL >"%topreserve%"
:: sort the dirs
FOR /D /R "%rootdir%" %%D IN (*) DO (
IF EXIST "%%D\*.%ext%" (
ECHO %%D>>"%topreserve%"
) ELSE (
ECHO %%D>>"%todelete%"
)
)
:: process the sorted lists
FOR /F "usebackq delims=" %%D IN ("%todelete%") DO (
FIND "%%D" <"%topreserve%" >NUL || ECHO RD /S /Q "%%D" 2>NUL
)
You can still see the ECHO before RD, but consider it just my way of saying, ‘In case I missed something…’. Run the script and look at the output. If it correctly shows only the directories that are due to be deleted, remove ECHO and run the script again to get on with the action.

Resources