I have a folder That has subfolders as below : I was able to loop and copy the folder and subfolders from test1 to test 3. But I want to extend the functionality by removing the each folder after copying
but am not able to instead is removing all the test 1 to test 3 folder.The test folder is just an example, the folders can be any name.
"C:\BACKUP_FDM_FILES\localbackup\test1"
"C:\BACKUP_FDM_FILES\localbackup\test2"
"C:\BACKUP_FDM_FILES\localbackup\test3"
pushd "C:\BACKUP_FDM_FILES"
for /f "tokens=*" %%G in ('dir /b /s /a:d ') do (
echo Found %%G
XCOPY %%G "C:\FDM\Upload" /e /s /y
if errorlevel 0 (
echo Copy succesffully copied reason Files were copied without error and you can delete the content folder
rd "%%G\*" /s /q
mkdir "%%G"
)
)
popD
pause
If you resort to XCOPY, why not use the "successor" ROBOCOPY without any loop, deleting and re-creating the folder? Like in
robocopy "C:\BACKUP_FDM_FILES\localbackup\" "C:\FDM\Upload\localbackup" /s /e /COPYALL /dcopy:T /move
This will even keep the timestamp on the moved folder, and all attributes on moved files. Added benefit: you can use all the other helpful options of this command at no extra cost.
"C:\BACKUP_FDM_FILES\localbackup\test1"
"C:\BACKUP_FDM_FILES\localbackup\test2"
"C:\BACKUP_FDM_FILES\localbackup\test3"
pushd "C:\BACKUP_FDM_FILES"
for /f "tokens=*" %%G in ('dir /b /s /a:d ') do (
echo Found %%G
XCOPY %%G "C:\FDM\Upload" /e /s /y
if errorlevel 0 (
echo Successfully copied now you can delete folder
popd
rd "%%G\*" /s /q
mkdir "%%G"
)
pause
You are using pushd to go to backup folder so you need to use popd to go back to parent folder.
SS64 - POPD
Related
I have subfolders with the following naming convention:
000026867_20200722_222406_SS24
I want to combine the contents of all folders that share the same last portion of the name. In this case all folders ending in SS24. It will always be after the 3rd underscore, but there is a chance it might be more than 4 characters after the last underscore.
I want all the files in all the folders ending in SS24 to be in a new folder named, let's say, All_SS24
The original folders can be deleted.
#Echo off
pushd "C:\path\to\your\base\folder"
for /f "Tokens=1* Delims=-" %%A in ( 'Dir /B /AD -' ) Do If Not Exist "%%A" (
Ren "%%A-%%B" "%%A"
) Else (
Move /Y "%%A-%%B*" "%%A\" RmDir "%%A-%%B"
)
PopD
Thanks for your help.
This is more or less what you seem to want:
#echo off
pushd "C:\path\to\your\base\folder"
for /f "tokens=1-4*delims=_" %%a in ('dir /b /ad "*_*"') do (
mkdir "%%d">nul 2>&1
copy "%%a_%%b_%%c_%%d\*" "%%d" /Y
rd "%%a_%%b_%%c_%%d" /S /Q
)
popd
I am trying to delete all but the newest folder using a batch script.
A similar question has already been asked here, so I tried using that as a template and things are getting screwed up.
The folder that I need to purge of all but the newest folder is actually a folder containing backups. In another script, I am backing up the folder that contains this folder, which also includes the folder that this is a backup.
H:\LOS is the folder being backed up.
H:\LOS\DefaultChromeCopy is the folder that contains backups of H:\LOS\Default that are taken everyday.
I want to backup H:\LOS, but I don't need any of the folders in DefaultChromeCopy since I'm already backing up Default. I will be running this weekly, so I want to temporarily move the newest folder in the DefaultChromeCopy directory to T:\Backups for safe keeping, then delete all the folders in DefaultChromeCopy, and then backup H:\LOS. That backup will also go to T:\Backups, and then I can move the folder that was the newest folder in DefaultChromeCopy back to its original location.
So in the end, I will have a backup of H:\LOS with an empty DefaultChromeCopy folder, and all the folders in H:\LOS\DefaultChromeCopy will have been deleted except for the newest one (which was "moved" temporarily).
So, using the code from the other SO answer as a starting put, I have this (without the asterisks around the echo):
if not exist "T:\Backups" md "T:\Backups"
set "workdir=H:\LOS\DefaultChromeCopy"
set "folder="
for /f "tokens=* delims=" %%i in ('dir %workdir% /AD /B /TW /O-D') do (
set "folder=%%~fi"
**echo** robocopy H:\LOS\DefaultChromeCopy\%folder% T:\Backups\%folder% /e /w:0 /r:2
goto :break
)
:break
echo newest... %folder%
for /f "skip=1 tokens=* delims=" %%i in ('dir %workdir% /AD /B /TW /O-D') do (
echo rd /s /q "%%~fi"
)
I've set up a dummy drive H and dummy drive T and modeled the folders in order to test this out.
You can see that ne is the newest folder.
With the echo denoted by asterisks, I get:
newest... H:\ne
rd /s /q "H:\sdf"
rd /s /q "H:\6"
rd /s /q "H:\5"
rd /s /q "H:\4"
rd /s /q "H:\3"
rd /s /q "H:\2"
rd /s /q "H:\1"
The path for "newest" used to be right but now even that is not right.
I've stopped here, because so much is going wrong already. How can I fix the paths? Also, if I run without the echo, ALL the folders in DefaultChromeCopy (or sometimes, even stranger, all of them except ne and sdf) get copied to T:\Backups, which is not what is supposed to happen. Only the newest folder is supposed to get copied.
Here is the part that will actually do the "backup", and then move %folder% back where it came from (%folder% is the newest folder from DefaultChromeCopy).
robocopy "H:\LOS\" "T:\Backups\LOS_Backup_%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%TIME:~0,2%.%TIME:~3,2%" /e /w:0 /r:2
robocopy T:\Backups\%folder% H:\LOS\DefaultChromeCopy /e /w:0 /r:2
start "" "T:\Backups"
The directories in DefaultChromeCopy are not modified after they are created, so Date Created and Date Modified should be the same. I only want to keep the newest folder in DefaultChromeCopy, by moving it to T:\Backups so I can delete all the folders in DefaultChromeCopy for the backup (I don't need even the newest backup in the big backup I'm taking). Hope this all makes sense...
UPDATE:
Running:
setlocal ENABLEDELAYEDEXPANSION
set "workdir=H:\LOS\DefaultChromeCopy"
set "folder="
for /f "tokens=* delims=" %%i in ('dir %workdir% /AD /B /TW /O-D') do (
set "folder=%%~fi"
echo %%~fi
echo robocopy %%~fi T:\Backups\!folder! /e /w:0 /r:2
goto :break
)
:break
echo newest... !folder!
for /f "skip=1 tokens=* delims=" %%i in ('dir %workdir% /AD /B /TW /O-D') do (
echo rd /s /q "%workdir%\%%~i"
)
produces:
H:\ne
robocopy H:\ne T:\Backups\H:\ne /e /w:0 /r:2
newest... H:\ne
rd /s /q "H:\LOS\DefaultChromeCopy\sdf"
rd /s /q "H:\LOS\DefaultChromeCopy\6"
rd /s /q "H:\LOS\DefaultChromeCopy\5"
rd /s /q "H:\LOS\DefaultChromeCopy\4"
rd /s /q "H:\LOS\DefaultChromeCopy\3"
rd /s /q "H:\LOS\DefaultChromeCopy\2"
rd /s /q "H:\LOS\DefaultChromeCopy\1"
So it looks like the directories that are supposed to be deleted are getting deleted, but the "newest" part is still a little wonky. I need to figure out how to get "H:\LOS\DefaultChromeCopy\ne". Right now:
%%~fi --> H:\ne
%workdir%\%%~fi --> H:\LOS\DefaultChromeCopy\H:\ne
T:\Backups\!folder! --> T:\Backups\H:\ne
T:\Backups\%folder% --> T:\Backups\
?????????????!!!!! --> H:\LOS\Default\ChromeCopy\ne & T:\Backups\ne
Fortunately, I found a sort of a workaround. There should only be 1 folder left in DefaultChromeCopy, so even though I can't figure out how to reference it, I can reference its parent. Thus, running the following deletes all but the newest folder from DefaultChromeCopy, backs DefaultChromeCopy up to T:\Backups, deletes everything in DefaultChromeCopy, backs up H:\LOS to T:\Backups, then copies DefaultChromeCopy back to H:\LOS\DefaultChromeCopy and deletes it from T:\Backups.
Here is the final working script. Thanks for everyone's help:
#echo off
echo Beginning LOS backup - deleting all Chrome backups except the newest one
if not exist "T:\Backups" md "T:\Backups"
setlocal ENABLEDELAYEDEXPANSION
set "chromebackups=H:\LOS\DefaultChromeCopy"
set "folder="
for /f "tokens=* delims=" %%i in ('dir %chromebackups% /AD /B /TW /O-D') do (
set "folder=%%~fi"
echo folder is !folder!
goto :deleteolds
)
:deleteolds
for /f "skip=1 tokens=* delims=" %%i in ('dir %chromebackups% /AD /B /TW /O-D') do (
rd /s /q "%chromebackups%\%%~i"
)
robocopy "H:\LOS\DefaultChromeCopy" "T:\Backups\DefaultChromeCopy" /e /w:0 /r:2
set topurge="H:\LOS\DefaultChromeCopy"
cd /d %topurge%
for /f "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
setlocal DISABLEDELAYEDEXPANSION
robocopy "H:\LOS" "T:\Backups\LOS_Backup_%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%TIME:~0,2%.%TIME:~3,2%" /e /w:0 /r:2
robocopy "T:\Backups\DefaultChromeCopy" "H:\LOS\DefaultChromeCopy" /e /w:0 /r:2
rmdir "T:\Backups\DefaultChromeCopy" /s /q
start "" "T:\Backups"
echo LOS Backup has completed! You can now ZIP the folder in T:\Backups\, upload that to the LOS Repository server, and then delete T:\Backups manually.
I have on my computer an partition named H:
On that partition i have diffent folders with each a folder called "bin"
Example:
H:MyFolder\Bin
H:AnotherFolder\Bin
I know that I can delete bin with the follow command:
set folder="H:\Bin"
cd /d %folder%
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
Is it possible to empty all the bin folders from all folders in partition H: with a command?
This will remove the content of all the bin folders under H:\ while keeping the folders.
for /r "H:\" /d %%a in (bin) do #if exist "%%~fa\" ( pushd "%%~fa" && ( echo "%%~fa" & echo rmdir . /s /q & popd ))
There is an echo command that precedes the rmdir. This is included for testing and must be removed to perform the content removal.
I want to use this script below to clean out "tmp" and "cache" folders in websites like "C:\Storage\Websites\Site1".
It tries to delete files and subfolders in "C:\Storage\Websites\Site1\tmp" and "C:\Storage\Websites\Site1\cache".
Which is right, but it also tries to delete files and subfolders in, for example, "C:\Storage\Websites\Site1\MySpecialLittleProgram\tmp" and, for example, "C:\Storage\Websites\Site1\MySpecialLittleProgram\cache".
Which is wrong. It should only clean up the "tmp" and "cache" folder in the root of the website and not in other subfolders.
If I delete the /s parameter in 'dir /a:d /b /s tmp cache' it will not find anything.
How can I do this part?
(I have deleted the /q parameter in the file deleting part and the folder removing part if anyone copies my script.)
#echo off
call:CleanUp "C:\Storage\Websites"
echo.&pause&goto:eof
::--------------------------------------------------------
::-- Function section starts below here
::--------------------------------------------------------
:CleanUp
IF EXIST %~1 (
cd /d %~1
FOR /f "tokens=*" %%i in ('dir /a:d /b /s tmp cache') DO (
echo %%i
::DELETING FILES I FOLDERS AND SUBFOLDERS
del %%i /s
::DELETING NOW EMPTY FOLDERS AND SUBFOLDERS
FOR /D %%p IN ("%%i\*.*") DO rmdir "%%p" /s
)
)
goto:eof
UPDATE:
I updated my code to be (it is working now):
#echo off
call:CleanUp "C:\Storage\Web"
call:CleanUp "C:\Storage\Web-IIS"
goto:eof
::--------------------------------------------------------
::-- Function section starts below here
::--------------------------------------------------------
:CleanUp
IF EXIST %~1 (
cd /d %~1
FOR /f "tokens=*" %%i in ('dir /a:d /b') DO (
IF EXIST %%i\tmp (
del %%i\tmp /s /q
FOR /D %%p IN ("%%i\tmp\*.*") DO rmdir "%%p" /s /q
)
IF EXIST %%i\cache (
del %%i\cache /s /q
FOR /D %%p IN ("%%i\cache\*.*") DO rmdir "%%p" /s /q
)
)
)
goto:eof
This should remove the files in those two locations:
#echo off
del "C:\Storage\Websites\Site1\tmp\*.*" /a /s
del "C:\Storage\Websites\Site1\cache\*.*" /a /s
From your comment, this may be what you need to do: remove the echo keyword after testing it to see the commands on the console that would be executed.
#echo off
cd /d "C:\Storage\Websites"
for /d %%a in (*) do (
for %%b in (tmp cache) do (
pushd "%%~fa\%%b" 2>nul && (echo rd /s /q "%%~fa\%%b" 2>nul & popd)
)
)
pause
I suggest to use rmdir or rd for this task:
rd "C:\Storage\Websites\Site1\tmp" /S /Q
md "C:\Storage\Websites\Site1\tmp"
rd "C:\Storage\Websites\Site1\cache" /S /Q
md "C:\Storage\Websites\Site1"
Command rd with options /S for all subdirectories and /Q for quiet deletes also tmp and cache, but those 2 directories can be easily recreated using command md although I'm quite sure that this would not be really necessary here as the application creating tmp and cache would do it also automatically.
If that is not what you want, please show as directory listings with files and folders in one of the two directories before cleanup and after cleanup.
Hi I tried below command to delete files in UNC path
set folder="\\SERVERNAME\Publish"
cd /d %folder%
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
But I got error saying:
UNC paths are not supported. Defaulting to Windows Directory
Somehow I need to delete files that are residing in Server's shared path using batch command. Any help appreciated.
edited 2015-09-16 - Original answer remains at the bottom
Code reformated to avoid removal of non desired folders if the mapping fails. Only if the pushd suceeds the removal is executed.
set "folder=\\SERVERNAME\Publish"
pushd "%folder%" && (
for /d %%i in (*) do rmdir "%%i" /s /q
popd
)
original answer:
set "folder=\\SERVERNAME\Publish"
pushd "%folder%"
for /d %%i in (*) do rmdir "%%i" /s /q
popd
pushd will create a drive mapping over the unc path and then change to it. Then, all the operations are over drive:\folders. At the end popd will remove the drive assignation.
This deletes all files with name like 'ms' and over a year.
#echo off
set "year=-365"
PushD "\\SERVERNAME\FolderName" && (
"forfiles.exe" /s /m "*_ms_*" /d %year% /c "cmd /c del #file"
) & PopD