Zxian Posted October 1, 2005 Share Posted October 1, 2005 I'm trying to follow this guide. For the very last step, we are told to delete any files that are not in the original directory from the new directory...So... if I have a directory structure as follows:D:\D:\NewD:\New\file1.txtD:\New\file2.txtD:\OldD:\Old\file1.txtI want to delete the file D:\New\file2.txt. That much I can do with the following... FOR /R D:\New %%a IN (*.*) DO IF NOT EXIST D:\Old\%%~nxa DEL %%a the problem I run into is when I have sub directories... imagine we addD:\New\Folder\file3.txtD:\New\Folder\file4.txtD:\Old\Folder\file3.txtI'm not sure how I would go about using the modifiers to get the child directories and filenames. Any help would be appreciated! Link to comment Share on other sites More sharing options...
Yzöwl Posted October 1, 2005 Share Posted October 1, 2005 You could always use robocopyrobocopy "D:\Old" "D:\New" /mir>nul Link to comment Share on other sites More sharing options...
Zxian Posted October 1, 2005 Author Share Posted October 1, 2005 Thanks Yzowl, but I don't want to copy over the files. The new directory will contain files that have the same filenames as the old (as well as some extras), but the files themselves are different. Link to comment Share on other sites More sharing options...
Yzöwl Posted October 2, 2005 Share Posted October 2, 2005 (edited) Okay, I understand now…This is the best I can come up with atm@echo off&setlocal enableextensionsset olddir=%userprofile%\desktop\old folderset newdir=%userprofile%\desktop\new folderpushd %newdir%for /f "tokens=*" %%c in ('cd') do set basepath=%%cfor /f "tokens=*" %%d in ('dir/b/s/a') do ( set fullpath=%%d call:chkit "%%fullpath:%basepath%=%%")popd&endlocal&goto :eof:chkitpushd %olddir%if not exist ".%~1" del /f/a/q "%newdir%%~1"popd&goto :eofJust change the locations of olddir and newdir to suit. Edited October 2, 2005 by Yzöwl Link to comment Share on other sites More sharing options...
Zxian Posted October 3, 2005 Author Share Posted October 3, 2005 Thanks for the help. However, when I try to run it, I get a lot of "The File could not be found" errors. To be honest, I'm not entirely sure what the script does...@echo off&setlocal enableextensionsset olddir=%userprofile%\desktop\old folderset newdir=%userprofile%\desktop\new folderpushd %newdir%That much I can figure out... set the variables and then move to the newdir.for /f "tokens=*" %%c in ('cd') do set basepath=%%cThis sets the variable to the current directory.for /f "tokens=*" %%d in ('dir/b/s/a') do ( set fullpath=%%d call:chkit "%%fullpath:%basepath%=%%")When I run this on its own (with echo instead of call:chkit), I get the same file listed many many times (probably the number of times that there are files in the current directory). Is it supposed to take the fullpath of each file and remove the basepath?popd&endlocal&goto :eof:chkitpushd %olddir%if not exist ".%~1" del /f/a/q "%newdir%%~1"popd&goto :eofSo... return to the previous directory before the pushd. Move to olddir... and that's where I get lost. I'm not entirely familiar with all the modifiers for variable names and such.Thanks again. Link to comment Share on other sites More sharing options...
JoeMSFN Posted October 3, 2005 Share Posted October 3, 2005 (edited) @echo offsetlocal enableextensions enabledelayedexpansionset olddir=%userprofile%\desktop\old folderset newdir=%userprofile%\desktop\new folderpushd %newdir%for /f "tokens=*" %%c in ('cd') do set basepath=%%cfor /f "tokens=*" %%d in ('dir/b/s/a') do ( set fullpath=%%d echo "!fullpath:%basepath%=!")endlocalI've altered it a little, namely setlocal enableextensions enabledelayedexpansion and echo "!fullpath:%basepath%=!" I've pulled my hair out on a similar issue. It has to do with the way cmd processes for loops. It seems variables don't get updated properly without delayed expansion enabled and the way to get variabled to update is to use ! instead of %. Also note that for that fancy replace line, you can't have !s around basepath.Note, this probably isn't a complete solution, but might give you a little help. Edited October 3, 2005 by JoeMSFN Link to comment Share on other sites More sharing options...
Yzöwl Posted October 3, 2005 Share Posted October 3, 2005 (edited) Delayed Expansion is not necessary, just changing it slightly to this should do it@echo off&setlocal enableextensionsset olddir=%userprofile%\desktop\old folderset newdir=%userprofile%\desktop\new folderpushd %newdir%for %%a in (.) do set basepath=%%~fafor /f "tokens=*" %%b in ('dir/b/s/a ^2^>nul') do ( if %errorlevel% equ 0 ( set fullpath=%%b call:chkit "%%fullpath:%basepath%=%%" ))popd&endlocal&goto :eof:chkitpushd %olddir%if not exist ".%~1" del /f/a/q "%newdir%%~1" 2>nulpopd&goto :eofIf your new directory is liable to have sub-directories in it which are also not in the old directory and need removing too, then this may be better@echo off&setlocal enableextensionsset olddir=%userprofile%\desktop\old folderset newdir=%userprofile%\desktop\new folderpushd %newdir%for %%a in (.) do set basepath=%%~fafor /f "tokens=*" %%b in ('dir/b/s/a ^2^>nul') do ( if %errorlevel% equ 0 ( set fullpath=%%b call:chkit "%%fullpath:%basepath%=%%" ))popd&endlocal&goto :eof:chkitset relpath=%~1if not exist "%olddir%%relpath%" ( pushd %newdir%%relpath% 2>nul&&( popd rd /s/q "%newdir%%relpath%" 2>nul )||( del /f/a/q "%newdir%%relpath%" 2>nul ))goto :eof<Edit>Codes changed according to the below two responses!</Edit><Edit 2>Added a couple of 2>nul to hide some standard error echoing</Edit 2> Edited October 3, 2005 by Yzöwl Link to comment Share on other sites More sharing options...
Zxian Posted October 3, 2005 Author Share Posted October 3, 2005 Ok.. this is strange. I took the code that JoeMSFN gave and ran it with my entries for the newdir and olddir and set it to output to a text file. (Office_clean.cmd > output.txt)The contents of the output.txt file are essentially a list of the contents of my C: drive... even though neither of the newdir or olddir variables are on C.... When I manually run the script line by line, the following linefor /f "tokens=*" %%a in ('cd') do set basepath=%%atells me that basepath is set to C:\. When I run cd it says G:\OFFICE\OFFICE2 (which is what it should be)...I'm so confused... Link to comment Share on other sites More sharing options...
Yzöwl Posted October 3, 2005 Share Posted October 3, 2005 (edited) Try replacing that line withfor %%a in (.) do set basepath=%%~fadoes it help?<Edit>I don't know if it'll make any difference to you but I will make that change in both codes in my previous response too!</Edit> Edited October 3, 2005 by Yzöwl Link to comment Share on other sites More sharing options...
Zxian Posted October 3, 2005 Author Share Posted October 3, 2005 Ok... with the edited line you gave me in Post #9, the basepath shows as it should.However, I tried the second code you gave me in #7, but I get errors that "The system cannot find the file specified". I haven't tried the first code yet... Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now