I have a messy situation on production server, there is a location on production which should only contain 3 folder however due to not following some process we have got more than 1000 folders and files and I am aiming to clean it via a batch file so that there is no chances of human error.
So I would like to copy all folders and files except 3 folders to a new location. can someone help in this as not able to put logic to exclude these 3 folders.
Create a file called ex.txt that includes 3 lines, each of which is the name of the folder that you would like to exclude from the copy, e.g.:
\folder1\
\folder2\
\folder3\
Now, go to the parent of the high-level directory (say, directory_to_copy), that you would like to copy, in which location exists the ex.txt file, and type
xcopy /e /i /exclude:ex.txt directory_to_copy destination_name
This will exclude the folders folder1, folder2, and folder3 from the copy.
Note: the backslashes \ are important to ensure that the other folders containing those strings (folder1, folder2, and folder3) are not excluded.
This writes tempmove.bat.txt in the same folder as the batch file that contains the move commands to move every folder except the three folders shown (testenv stageenv prodenv).
You can examine the text file before renaming it to .bat to actually use, if it shows the right commands.
Make sure "d:\wrong folders" folder already exists.
#echo off
cd /d "c:\production folder"
(
for /d %%a in (*) do (
if /i not "%%~nxa"=="testenv" if /i not "%%~nxa"=="stageenv" if /i not "%%~nxa"=="prodenv" echo move "%%~fa" "d:\wrong folders\%%~nxa"
)
)>"%~dp0\tempmove.bat.txt"
pause
Related
I have 3 folders relevant to this script.
Folder 1 - contains all existing images (PNG), sorted into subdirectories ONLY, totalling thousands of individual files. None exist in the root folder.
Folder 2 - newly obtained images all in the root folder. I don't feel it's important to explain how they are obtained, as opposed to the fact that there will be a mix of duplicate files and new files.
Folder 3 - empty folder.
Due to folder 1 having the most files, I need to run a check of all the files in folder 2 to everything in folder 1 recursively, (and not the other way around, for performance reasons) then copy/move whatever is new to folder 3 (this way I can manually sift through and sort them accordingly).
Comparison can be done purely based on file name.
I've looked around forums extensively and found only 1 script that works the best, but relies on both folders having no subdirectories.
My problem is that I can't seem to get it to compare files to folder 1 recursively. It ends up just copying everything in folder 2 to folder 3, because it's checking against the root folder of folder 1, which contains nothing but subdirectories.
Code in question:
#echo off
set "Folder1=Folder1"
set "Folder2=Folder2"
set "Folder3=Folder3"
for /f "delims=" %%F in ('dir /b "%folder2%"') do (
if not exist "%folder1%\%%F" copy "%folder2%\%%F" "%folder3%" && echo Copied "%%F"
)
pause
Exit
Behaviour of above code:
Loops through every file in folder 2, and if a file with the same name doesn't exist within folder 1 (root folder only), it will copy it to folder 3.
I'd like it to check folder 1 contents recursively, not just the root folder.
I've tried tweaking it in many ways, such as changing the IN to NOT IN, or trying to find something to make the "%folder1%\%%F" have a recursive parameter such as "%folder1%\..\%%F" but nothing has worked with my limited knowledge.
Preferrably I'd like the whole process to be done with only 1 script.
Thanks in advance for your help. Please let me know if further clarification on anything is needed.
if [not] exist works only if you give it the whole path (which you can't, because you don't know, were it might be). So this approach doesn't help you.
Instead you could do a dir /s /b "%folder1%\%%F" || copy ...., but that means, you have to do a recursive dir for each file in folder2 - that's not what I would call performance.
Better generate a text file with the files in folder1 and use that as reference. (%%~nxA lists only name and extension of the files):
#echo off
set "Folder1=Folder1"
set "Folder2=Folder2"
set "Folder3=Folder3"
>Folder1Files.txt (for /r "%folder1%" %%A in (*) do echo %%~nxA)
for %%F in ("%folder2%\*") do (
findstr /x "%%~nxF" Folder1Files.txt >nul || copy "%%F" "%folder3%\" && echo Copied "%%F"
)
pause
Exit /b
(judging by your workflow description, consider move instead of copy)
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.
I'm creating a batch file that deletes all Rar$DIa0.??? folders in the %TEMP% directory.
Is this possible, and how to do it?
The ??? is three randomized numbers. And that's where I have trouble with - deleting all folders that have Rar$DIa0. in the name.
for /d is designed for just this type of use. Something like this should work (remove one of the % if you're testing from the command line):
for /d %%i in ("%TEMP%\Rar$DIa0.???") do rd "%TEMP%\%%i"
The /d makes it work on directory names instead of file names.
If you want to make it easier on yourself, change to the %TEMP% folder first:
pushdir
cd /d %TEMP%
for /d %%i in ("Rar$DIa0.???") do rd "%%i"
The ??? makes it only act on folders that have three letters after a .. If your folders don't have just a three letter extension, change .??? to .*. If you've got a typo, and there is no actual . in the foldername, just remove it and use Rar$DIa0??? or Rar$DIa0*
You may want to test it first by changing rd to echo to make sure you get the folders you want before actually deleting them.
For more information about for (pun intended) type for /? from a command prompt.
The command line to use in a batch file for this task is:
#for /D %%I in ("%TEMP%\Rar$DIa0.*") do #rd /Q /S "%%I"
Command FOR with option /D searches in folder defined by environment variable TEMP for subfolders with folder name starting with Rar$DIa0. not having hidden or system attribute set.
The loop variable I holds for each found subfolder matching this folder pattern the name of found folder with full path without double quotes although the path to temp folder very often contains 1 or more spaces.
For that reason just the command RD with the parameters /Q for quiet execution and /S for deleting also all subfolders in the specified folder must be called with referencing the current value of loop variable I enclosed in double quotes.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
for /?
rd /?
By the way: WinRAR deletes the temporary folders usually automatically, except a file is opened from within an archive for viewing/modifying it in another application and WinRAR is closed before the other application is exited with the opened file. In this case WinRAR can't delete the temporary folder with temporarily extracted file because the file is still opened in another application. Of course also command RD can't delete the temporary folder if this folder is still the current directory of another application or a file in this folder is still opened by another application with a read/write access lock.
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.
With a batch (.bat), I want to copy all mp3 files that are in 1 subdirectory of D:\TEMP
D:\TEMP\\(anyfolder)\\(anyfile.mp3)
to
E:\MYFOLDER\
I tried with xcopy but
I don't know how to tell "just recurse subfolders of D:\TEMP and not subsubfolders, subsubsubfolders, etc."
When using xcopy, folders are created in the destination (in order to replicate source's folder tree), I don't want this : files should be copied in just 1 single folder.
for command is your friend. Read help for and then try this in the command prompt
for /d %a in (*) do #echo %a
as you see, it follows all subfolders in the current directory.
thus,
for /d %a in (*) do #copy %a\*.mp3 e:\myfolder
will copy all your mp3 to the destination folder.