I use this script to clean history, cookies and cache (Temporary Internet Files) for all users AND it should also clean the temp dir BUT there seems to be something wrong.
Two things get mixed up I think, the %temp% variable (= D:\TEMP in my environment)
AND the users temp dir in the %userprofile%.
:: Works on Win XP -and- on Win 7
#echo off
Set "RegKey=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
set "regkey2=HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\shell folders"
call:getspecialfolders "Cache, History, Cookies"
For /f "tokens=*" %%? in (
'Reg.exe QUERY "%RegKey%" ^|findstr /ric:"\S-1-5-21-[0-9]*-[0-9]*-[0-9]*-[0-9]*$"'
) do (
For /f "tokens=2,*" %%A in (
'Reg.exe QUERY "%%?" /v ProfileImagePath ^|find /i "ProfileImagePath"'
) do call:Go %%B
)
start ""/w "%windir%\system32\RunDll32.exe" InetCpl.cpl,ClearMyTracksByProcess 255
:end ***
goto:EOF
:Go
call Set "Target=%*"
If EXIST "%Target%" call:Clear "%Target%"
exit /b 0
:Clear
REM echo.&echo.%~1\%$$Cache%
pushD "%~1\%$$Cache%" &&(
rmdir /S /Q .
popD)2>C:\test1_TEMP_IE.txt
REM echo.&echo.%~1\%$$History%\History.IE5
REM pushD "%~1\%$$History%\History.IE5" &&(
REM rmdir /S /Q .
REM popD)2>C:\test1_History_IE.txt
REM echo.&echo.%~1\%$$History%
pushD "%~1\%$$History%" &&(
rmdir /S /Q .
popD)2>C:\test1_History.txt
REM echo.&echo.%~1\%$$Cookies%
pushD "%~1\%$$Cookies%" &&(
rmdir /S /Q .
popD)2>C:\test1_Cookies.txt
ECHO.&echo.%~1\%$$temp%
pushD "%~1\%$$temp%" &&(
rmdir /S /Q .
popD)2>C:\test1_Temp.txt
exit /b 0
:getspecialfolders
Set "FoldersToClear=%~1"
For %%* in (%FoldersToClear%) Do (
For /f "tokens=2,*" %%A in (
'reg.exe query "%regkey2%" /v %%* ^|find /i "%%~*"'
) do Call:sf1 "%%~B" "%%~*"
)
Call:sf2 "%temp%" "temp" "%userprofile%"
exit /b 0
:sf1
Call set "sf=%~1"
Call set "$$%~2=%%sf:%userprofile%\=%%"
exit /b 0
:sf2
Call set "sf=%~1"
call Set "usr=%~dpns3"
Call set "$$%~2=%%sf:%usr%\=%%"
exit /b 0
BUT somehow I can't get the last "temp part" to function so it cleans the %temp% (D:\Temp in my environment) and to also find al "temp dir's" in the %userprofile%.
ie. this for instance does work for %temp%:
PushD "%Temp%" && (
ATTRIB -S -H -R -A /D /S & (
For /f "Tokens=*" %%* in ('dir "%Temp%" /B') Do (
RD "%Temp%\%%*" /S /Q || Del /F /S /Q "%Temp%\%%*"))&PopD)2>c:\test0b_TEMP.txt
and this ie. work for the "user(s) temp":
::Set Search directory to "Documents and Settings" folder
(Set Target=%AllUsersProfile:~0,-10%)
title,Finding the Temp subfolders in %Target%&COLOR 9E
If EXIST "%Target%",(
For /f "Tokens=*" %%* in ('dir "%Target%" /B') Do (
cd/D "%target%\%%*\Local Settings\Temp" && (
ATTRIB -S -H -R -A /D /S >nul & (
For /f "Tokens=*" %%* in ('dir /B') Do (
RD "%%*" /S /Q ||Del /F "%%*" )))>nul)
)
I hope some one can help me out fixing the script, I think it's in the :sf2 and/or in combination with the %temp% part, somehow 2 things get mixed-up now ("users temp" en "environment temp").
All right, this time I think there is a way to fix this, namely, by adding a check whether the variable contains a relative or an absolute path, directly before proceeding with the cleanup. The idea is to test for the presence of the colon (:) in the string. If the colon is present then the path could not be converted to a relative path previously and so should be used as is, without pre-pending the profile path, otherwise the profile path should be attached before going on.
Here's a basic example that you can test and play with, if you like:
#ECHO OFF
SET somepath=D:\TEMP
CALL :checkpath
SET "somepath=Local Settings\Temp"
CALL :checkpath
PAUSE
GOTO :EOF
:checkpath
IF "%somepath%"=="%somepath:*:=%" (ECHO Relative path) ELSE ECHO (Absolute path)
In your particular situation I would probably apply the method like this:
instead of
…
ECHO.&echo.%~1\%$$temp%
pushD "%~1\%$$temp%" &&(
rmdir /S /Q .
popD)2>C:\test1_Temp.txt
…
I would try
…
IF "%$$temp%"=="%$$temp:*:=%" (SET "tmppath=%~1\%$$temp%") ELSE SET "tmppath=%$$temp%"
ECHO.&echo.%tmppath%
pushD "%tmppath%" &&(
rmdir /S /Q .
popD)2>C:\test1_Temp.txt
…
As you can see, a temporary variable is used to store the actual path to be processed. I understand it is enough to replace only the part where the temporary folder is being cleared, but you can see that the method can be easily applied to other folders as well, if needed.
It seems like your sf2 subroutine is trying to get a relative path of the Temp folder by cutting off the beginning of the path, which is essentially the user profile path:
Call set "$$%~2=%%sf:%usr%\=%%"
where sf contains the Temp folder and usr the user profile. So instead of, for instance, C:\Documents and Settings\APOC\Local Settings\Temp you would get simply Local Settings\Temp.
But there's a problem. Though the sf variable is assigned with the proper path, the usr variable, on the other hand, for some reason is assigned with the short named variant of the user profile path. That is, instead of something like C:\Documents and Settings\APOC it receives something like C:\DOCUME~1\APOC. Here's the offending line:
call Set "usr=%~dpns3"
So, when the formerly quoted line is executed, the expected substitution never happens, because C:\Documents and Settings\APOC doesn't match C:\DOCUME~1\APOC, naturally. As a result, the $$temp variable ends up with the complete path instead of the relative path, and because of that, the relevant parts of your code that reference $$temp don't do their job as expected.
In short, I think you don't need that sf2 routine at all. The sf1 routine seems to do just the same as sf2, only with a different set of parameters to pass. So, instead of
Call:sf2 "%temp%" "temp" "%userprofile%"
I'd suggest you to try this:
Call:sf1 "%temp%" "temp"
I changed the line for temp a little, is this a correct way? (it does work but is it properly formated?)
IF "%$$temp%"=="%$$temp:*:=%" (SET "tmppath=%~1\%$$temp%") ELSE SET "tmppath=%$$temp%"
ECHO.&echo.%tmppath%
pushD "%tmppath%" &&(
ATTRIB -S -H -R -A /D /S & (
rmdir /S /Q . || Del /F /S /Q .
)&popD)2>C:\test1_Temp.txt
+Extra "Del /f /s /q" for possible hard/read-only files (and additional attrib)
By the way
There still seems something not going 100% in the script with the "temp"-part, I changed my default temp settings in windows to the default paths voor testing purposes and I get these 3 error's:
screenshot of the error | url: http://img199.imageshack.us/img199/9127/errorif.jpg
script run with the windows default temp-path's:
[user] %UserProfile%\Local Settings\Temp and [system] %SystemRoot%\Temp
I'm not sure why I get these error's, when I set both temp parameters to D:\TEMP, I get 3 times the same sort of error but than for D:\TEMP instead of available/active users \local settings\Temp. Perhaps the problem is in locals~1???
(The temp-directories do get cleaned though, but why does it say it can't find the path?)
Related
I'm trying to copy some non specific folders with their content, but can't find a way to do it. My problem is every time I try to use any of the copy commands it tells me it's an "Invalid number of parameters", or "0 File(s) copied", or "File not found -", or it copies everything from Folder1, (not just the non specific folders).
What I mean by "non specific" is a folder which has an unknown name, i.e. *.
This is what I have tried:
xcopy /y "C:\Folder1\Something_*" "C:\Folder2"
copy /y "C:\Folder1\Something_*" "C:\Folder2"
robocopy "C:\Folder1\Something_*" "C:\Folder2"
/+ adding " * " in front of "Something_*"
I have also tried a lot more smaller things, that I don't see a need to add.
I'm a little lost on what to do at this point, and feel like I have been looking everywhere for a solution with no luck.
Edit: i found a way to do this and it's so much better, this is how
for /R "%sourcedir%" %%A in (Something_*) do copy /y "C:\Folder1\%%~A" "C:\Folder2"
#ECHO OFF
SETLOCAL
rem The following settings for the source directory, destination directory, target directory,
rem batch directory, filenames, output filename and temporary filename [if shown] are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files\t w o"
SET "destdir=u:\your results"
:: Version 1 - all files arrive in the destination directory
FOR /f "delims=" %%b IN ('dir /b /ad "%sourcedir%\Something_*" ') DO (
COPY /b /y "%sourcedir%\%%b\*" "%destdir%\" >NUL 2>nul
FOR /f "delims=" %%c IN ('dir /b /s /ad "%sourcedir%\%%b" ') DO (
COPY /b /y "%%c\*" "%destdir%\" >NUL 2>nul
)
)
cls
dir/s "%destdir%"
PAUSE
:: This is a routine I use for testing. It simply deletes the directory named and restores it, empty
CALL deltreey /r "%destdir%">NUL
:: Version 2 - preserve tree structure
FOR /f "delims=" %%b IN ('dir /b /ad "%sourcedir%\Something_*" ') DO XCOPY /y /s /e "%sourcedir%\%%b" "%destdir%\" >NUL 2>nul
)
cls
dir/s "%destdir%"
PAUSE
:: This is a routine I use for testing. It simply deletes the directory named and restores it, empty
CALL deltreey /r "%destdir%">NUL
GOTO :EOF
I am trying to delete everything in a user defined location with exception on one pre-defined folder using a for loop. How do I go about adding a exception in order to not delete a folder.
I am trying to learn how to code, but I admit I am doing baby steps. I got some excellent tips for the first input part of this script, but I lack the knowledge to move forward. I have searched and found similar code, but none seems to work. This script is intended for flight simulation and hopefully ease the workload of installing a particular item.
This is just the part of the code due to stackoverflow guidelines, it deletes everything including the folder I want to exclude.
...
Rem This code is intended to delete all except one pre-defined folder
Echo Deleting all the files except testmappe3
del /s /q "%CD%"
for /d %%p in ("%CD%") do rmdir "%%p" except "%%testmappe3" /s /q
dir
Pause
...
I expected the output to delete all folders except testfolder3
for /d %%A in ("%CD%\*") do (
set "except="
if /i "%%~nxA" == "testmappe3" set "except=1"
if not defined except rmdir /s /q "%%~A"
)
This code will iterate the folders in the current directory.
If the name+extension of the folder is testmappe3,
then except will be set as 1 i.e. defined with a value.
If except is not defined, rmdir will remove the folder.
You can add more if lines for checking folders to except.
The modifiers will recognize a folder such as named
testmappe3.test1 as name testmappe3 and
extension of .test1.
View for /? and call /? about modifiers.
View for /?, set /?, if /? and rmdir /? for
help with those commands.
First of all, I would be a very careful deleting everything using %cd% especially if the script can accidently be run as Administrator, where %cd% would then be c:\windows\system32.
Instead, use %~dp0 as path to ensure that you are in the correct directory. This all assumes you did not cd somewhere else earlier in the script.
Then to the actual issue, I would include findstr to exclude your directory `testmappe3 as well as your script itself.
#echo off
cd /d "%~dp0"
for /f %%p in ('dir /b ^| findstr /vi /r /c:^testmappe3$') do (
rmdir "%%p" /s /q >nul 2>&1
if not "%%p"=="%~nx0" del /s /q "%%p" >nul 2>&1
)
If you want to stick to your original delete method, then it would be as below, but if your script is in the same dir, then it will also be deleted:
#echo off
cd /d "%~dp0"
del /s /q *
for /f %%p in ('dir /b ^| findstr /vi /r /c:^testmappe3$') do (
rmdir "%%p" /s /q >nul 2>&1
)
If your folder to exclude contains spaces, double quotes are required.. i.e
dir /b ^| findstr /vi /r /c:^"test mappe3"$
I want to write a batch file that creates a folder (if it does not exist) and copies a certain file into that folder. So far so good.
The problem is that one folder in the path varies slightly from time to time, so a wildcard becomes necessary.
The following code works just fine but obviously misses to create the folder (Reports). So if the folder is not there, it simply does nothing.
for /r "c:\Users\%USERNAME%\AppData\Local\Packages" &&G in ("LocalState\acn\Reports") do #if exist %%G xcopy /s /i /y c:\temp\Reporting "%%G"
The full path is:
c:\Users\FSchneider\AppData\Local\Packages\“WILDCARD"\LocalState\acn\Reports\
Any idea?
Add /d switch in for to indicate you're looking for a directory, not a file
Add * and omit quotes in the wildcard to indicate it's actually a wildcard
No need for if exist now
for /d /r "%LocalAppData%\Packages" %%G in (LocalState\acn.*) do xcopy /s /i /y c:\temp\Reporting "%%G\Reports"
Next script could help.
#ECHO OFF
SETLOCAL enableextensions
set "_fldrtop=%USERPROFILE%\AppData\Local\Packages"
set "_fldrsub=LocalState\acn"
if not "%~1"=="" set "_fldrsub=%~1" :: my testing data, remove this line
set "_fldrlow=Reports"
if not "%~2"=="" set "_fldrlow=%~2" :: my testing data, remove this line
for /F "delims=" %%G in ('dir /B /AD "%_fldrtop%"') do (
if exist "%_fldrtop%\%%G\%_fldrsub%\" (
if exist "%_fldrtop%\%%G\%_fldrsub%\%_fldrlow%\" (
echo echo "%_fldrtop%\%%G\%_fldrsub%\%_fldrlow%\"
) else (
echo md "%_fldrtop%\%%G\%_fldrsub%\%_fldrlow%\"
)
rem echo xcopy /s /i /y c:\temp\Reporting "%_fldrtop%\%%G\%_fldrsub%\%_fldrlow%\"
)
)
Output:
==>D:\bat\SO\31672436.bat
==>D:\bat\SO\31672436.bat "LocalState\Cache"
md "C:\Users\UName\AppData\Local\Packages\winstore_cw5\LocalState\Cache\Reports\"
==>D:\bat\SO\31672436.bat "LocalState\Cache" 2
echo "C:\Users\UName\AppData\Local\Packages\winstore_cw5\LocalState\Cache\2\"
Basically what's going on is that we are migrating about 50 desktops from XP to 7. I cannot install any extra programs to complete this task. What I am doing is writing a script that copies the Desktop, Favourites, and My Documents, along with a few specific file types from the originating machine to a shared drive for the user. Which later will be able to have all files moved to the new machine they are getting. I'm trying to recursively search through Windows and get all .pst files and other files you will see in the script, and back them up to a folder on the share (but not in a directory structure, I just want all the files in a single directory no matter where they originated from) with the exception of My Documents. Anything they have put in My Documents should be excluded in the search. Anyway, this is what I started with:
#echo off
cls
set USRDIR=
set SHARE=
set /P USRDIR=Enter Local User Directory:
set /p SHARE=Enter Shared Drive Name:
set UPATH="c:\Documents and Settings\%USRDIR%"
set SPATH="g:\!MIGRATION"
set ESRI="%UPATH%\Application Data\ESRI"
net use g: /delete
net use g: \\server\%SHARE%
md %SPATH% %SPATH%\GIS %SPATH%\Outlook %SPATH%\Desktop %SPATH%\Documents %SPATH%\Favorites
if exists %ESRI% md %SPATH%\ESRI
md %SPATH%\misc %SPATH%\misc\GISfiles %SPATH%\misc\XMLfiles %SPATH%\misc\CSVfiles
for /R %%x in (*.mxd) do copy "%%x" "%SPATH%\GIS\"
for /R %%x in (*.dbf) do copy "%%x" "%SPATH%\misc\GISfiles\"
for /R %%x in (*.xml) do copy "%%x" "%SPATH%\misc\XMLfiles\"
for /R %%x in (*.csv) do copy "%%x" "%SPATH%\misc\CSVfiles\"
for /R %%x in (*.pst) do copy "%%x" "%SPATH%\Outlook\"
if exist %ESRI% xcopy /y /d /s /i /z %ESRI% %SPATH%\ESRI && echo ESRI YES || ESRI NO
xcopy /y /d /s /i /z "%UPATH%\Desktop" "%SPATH%\Desktop" && echo DESK YES || DESK NO
xcopy /y /d /s /i /z "%UPATH%\My Documents" "%SPATH%\Documents" && echo DOCS YES || DOCS NO
xcopy /y /d /s /i /z "%UPATH%\Favorites" "%SPATH%\Favorites" && echo FAVS YES || FAVS NO
echo "Script Complete!"
pause
I then decided that I wanted to exclude My Documents just in case so I don't end up with a bunch of duplicates where anything they put in My Documents ends up getting copied twice. So I changed that recursive block to this:
for /R %%x in (*.mxd) do xcopy /y /d /z /exclude:"\My Documents\" "%%x" "%SPATH%\GIS\"
for /R %%x in (*.dbf) do xcopy /y /d /z /exclude:"\My Documents\" "%%x" "%SPATH%\misc\GISfiles\"
for /R %%x in (*.xml) do xcopy /y /d /z /exclude:"\My Documents\" "%%x" "%SPATH%\misc\XMLfiles\"
for /R %%x in (*.csv) do xcopy /y /d /z /exclude:"\My Documents\" "%%x" "%SPATH%\misc\CSVfiles\"
for /R %%x in (*.pst) do xcopy /y /d /z /exclude:"\My Documents\" "%%x" "%SPATH%\Outlook\"
Anyway, my question is this, is this the most efficient way of doing this? Is there a better way? I'm curious because I have no one here to bounce ideas or anything off of, I'm the sole on site support here. If someone sees a better way or thinks this is how they would do it, please let me know. There are a bunch more filetypes in there, but I removed most of them so the code sample wasn't so long, left enough to get the point across.
EDIT: Just to make it clear, I am not the IT Department for this organization. I'm a technical support liaison for the department to a separate IT division. Our actual IT department calls this a migration, but it's more or less a simple "lets in stall new machines without doing anything to prepare for it" sort of soup sandwich operation. I can't install, remove, or change the systems in any way, I can only backup the files.
Usually, the best option is reduce processing in batch files to the minimum, leaving as much as possible to commands. But if you have to iterate over the file system several times, it is better to do only one pass and process in batch.
Adapted from a more general batch. I have made changes to adjust to what you need, but somethings more specific (your ESRI folder) are not added. Adapt as needed.
#echo off
setlocal enableextensions enabledelayedexpansion
rem Ask for share name
set /P share=Enter Shared drive name:
if "%share%"=="" (
call :error "No share name provided"
goto endProcess
)
rem Configure target of copy
set target=g:
rem Connect to target
net use %target% /delete
net use %target% \\server\%share%
if not exist %target% (
call :error "No connection to server"
goto endProcess
)
rem Configure directory by extension
set extensions=.mxd .dbf .xml .csv .pst
set .mxd=GIS
set .dbf=misc\GISFiles
set .xml=misc\XMLFiles
set .csv=misc\CSVFiles
set .pst=Outlook
rem adjust target of copy to user name
set target=%target%\!MIGRATION\%username%
if not exist "%target%" (
mkdir "%target%"
)
rem Configure source of copy
set source=%userprofile%
if not exist "%source%" (
call :error "User profile directory not found"
goto endProcess
)
rem Resolve My Documents folder
call :getShellFolder Personal
set myDocPath=%shellFolder%
if not exist "%myDocPath%" (
call :error "My Documents directory not found"
goto endProcess
)
rem Ensure target directories exists
mkdir "%target%" > nul 2>nul
for %%e in ( %extensions% ) do mkdir "%target%\!%%e!" >nul 2>nul
rem We are going to filter file list using findstr. Generate temp file
rem with strings, just to ensure final command line is in limits
rem This will contain not desired folders/files or anything that will be
rem copied later
set filterFile="%temp%\filter.temp"
(
echo %myDocPath%
echo \Temporary Internet Files\
echo \Temp\
) > %filterFile%
rem Process user profile, excluding My Documents, IE Temp and not needed extensions
for /F "tokens=*" %%f in ('dir /s /b /a-d "%source%" ^| findstr /v /i /g:%filterFile% ^| findstr /i /e "%extensions%" ') do (
call :processFile "%%~xf" "%%f"
)
rem Now, process "especial" folders
mkdir "%target%\Documents"
xcopy /y /d /s /i /z "%myDocPath%" "%target%\Documents"
call :getShellFolder Desktop
if exist "%shellFolder%" (
mkdir "%target%\Desktop"
xcopy /y /d /s /i /z "%shellFolder%" "%target%\Desktop"
if errorlevel 1 (
call :error "Failed to copy desktop files"
)
)
call :getShellFolder Favorites
if exist "%shellFolder%" (
mkdir "%target%\Favorites"
xcopy /y /d /s /i /z "%shellFolder%" "%target%\Favorites"
if errorlevel 1 (
call :error "Failed to copy favorites"
)
)
rem Finish
goto :endProcess
rem ** subroutines *******************************************
:processFile
rem retrieve parameters
rem : %1 = file extension
rem : %2 = file
set ext=%~1
set file=%~2
rem manage .something files
if "%ext%"=="%file%" (
set file=%ext%
set ext=
)
rem manage no extension files
if "%ext%"=="" (
rem WILL NOT COPY
goto :EOF
)
rem determine target directory based on file extension
set extCmd=%%%ext%%%
for /F "tokens=*" %%d in ('echo %extCmd%^|find /v "%%" ' ) do set folder=%%d
if "%folder%"=="" (
rem file extension not in copy list
goto :EOF
)
copy /y /z "%file%" "%target%\%folder%" >nul 2>nul
if errorlevel 1 (
call :error "Failed to copy [%file%]"
) else (
echo %file%
)
goto :EOF
:getShellFolder
set _sf=%~1
set shellFolder=
if "%_sf%"=="" goto :EOF
for /F "tokens=2,*" %%# in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "%_sf%" ^| find "%_sf%"') do (
set shellFolder=%%$
)
goto :EOF
:error
echo.
echo ERROR : %~1
echo.
goto :EOF
:endProcess
endlocal
exit /b
Try User State Migration Tool from Microsoft - I used it to move around 400 systems in less than 30 days. It takes a bit of setup for the script to run right, but it does a really really good job (and no program to install).
In default mode, it gets all PST files, Document, Desktop, Favorites, and a ton of other folders/registry settings. It also does this for all users on a computer (which can be limited by last login date or number of profiles) as long as the user running it is a local admin.
It may or may not work for what you need, but it's a good choice when designing a migration. It sounds like what you are trying to do can be done with USMT just by removing extra code in the ini files.
This checks and excludes any folder that ends with documents\ (or includes it, so subdirectories too) as IIRC the folder may be called \documents\ or \my documents\ and is case insensitive.
setlocal enabledelayedexpansion
for /R %%x in (*.mxd) do set "check=%%~dpx" & if /i "!check!"=="!check:documents\=!" copy "%%x" "%SPATH%\GIS\"
for /R %%x in (*.dbf) do set "check=%%~dpx" & if /i "!check!"=="!check:documents\=!" copy "%%x" "%SPATH%\misc\GISfiles\"
for /R %%x in (*.xml) do set "check=%%~dpx" & if /i "!check!"=="!check:documents\=!" copy "%%x" "%SPATH%\misc\XMLfiles\"
for /R %%x in (*.csv) do set "check=%%~dpx" & if /i "!check!"=="!check:documents\=!" copy "%%x" "%SPATH%\misc\CSVfiles\"
for /R %%x in (*.pst) do set "check=%%~dpx" & if /i "!check!"=="!check:documents\=!" copy "%%x" "%SPATH%\Outlook\"
endlocal
I need to delete with a batch a list of directories (and subdir) which name starts with a number.
My tree is something like that:
|- root
|--event=aaa
|--photo
|--123
|--44
|--23
|--89
|--otherdir
|--event=bbb
|--photo
|--432
|--67
|--12
|--32
|--otherdir
I found this working example: http://www.dostips.com/forum/viewtopic.php?f=3&t=1496
And this is what I wrote
#ECHO OFF
SET path=\\mypath
SET dir=\parentDir\
:: loop on event=* folders
FOR /D /R %path%%dir% %%K IN ("*event*") DO (
:: if an event contains photos ..
IF EXIST %%K\photo (
ECHO %%K
:: loop on dir inside photo folder with a numeric name
PUSHD %%K\photo ||goto :eof
FOR /f "delims=" %%a IN ('dir /ad /b /s ^|findstr /rc:"\\[0-9]"') DO (
IF EXIST %%~a (
RD /s /q "%%~a"
)
)
POPD
) ELSE (
ECHO.%%K /photo does not exists
)
)
ECHO.completed
PAUSE
the only problem I found is that the execution seems to freeze when the last /event=xxx folder, and takes quite a long time to ECHO the last line ("completed"). I don't understand why .. any ideas?
Many thanks
You can try this. It will delete every folder under the *event* folders that have only numbers in the names.
Currently it will pause on each main folder after merely listing the set of subdirectories that would be deleted, and is not destructive. If the commands look right then remove the echo and the pause to activate the RD command to actually delete the folders.
#echo off
for /f "delims=" %%a in (' dir "*event*" /ad /b ') do (
for /f "delims=" %%b in (' dir "%%a" /ad /b /s ') do (
for /f "delims=0123456789" %%c in ("A%%~nxb") do (
if "%%c"=="A" if exist "%%b\" echo rd /s/q "%%b"
)
)
pause
)
#ECHO OFF
SETLOCAL
SET mypath=.
SET dir=\parentdir\
FOR /f "delims=" %%i IN (
' dir /ad /s /b "%mypath%%dir%" ^|findstr /r "\\.*event.*\\photo\\[0-9].*" ' ) DO (
IF EXIST "%%i" ECHO RD /s /q %%i
)
This should do as you ask - with some cautions.
I've changed the variable names. PATH is not a good name to use because PATH is the sequence of directories which the OS searches for an executable.
I've also changed the value to suit my system.
I've added an ECHO keyword rather than arbitrarily deleting the target directories - just in case of a typo - gives you a chance at verifying first. This ECHO would need to be removed to activate the delete functionality.
The findstr regular expression is \ (which requires an extra \ as an escape) any number of any characters, event, any number of any characters, \photo\, a digit then any number of any characters.
Please also note that the "broken label" form of comment (::) should not be used within a loop. Whereas it (and labels) works within a loop in W7 (and presumably later,) it terminates the loop in some earlier Windows editions, whereas REM works for all editions.