1.

Solve : if file exists in one folder but not in another folder, delete the file?

Answer»

I need to be able walk thru and read all the files in G:\folderA and all files in subfolders under folderA and determine if the file exists in C:\folderA. If the file cannot be found in G:\folderA, then deleted the file in C:\folderA.

ie
If G:\1-AlsWork\file1.ext exists in C:\AlsWork ?      then next test,  if no, delete the file in G:\1-AlsWork\file1.ext
then move to the next file in G:\1-AlsWork and do the same test ...

also
If G:\1-AlsWork\subfolder1\somefile.ext exists in C:\AlsWork\subfolder1    then next test, if no, delete the file in G:\1-AlsWork\subfolder1\somefile.ext

Hope this explaination is clear ENOUGH for you to understand

Thanks in advance
cyberal Quote from: cyberal on December 22, 2011, 12:24:00 PM

I need to be able walk thru and read all the files in G:\folderA and all files in subfolders under folderA and determine if the file exists in C:\folderA. If the file cannot be found in G:\folderA, then deleted the file in C:\folderA.
I think you got that LAST sentence backwards.
If the file cannot be found in C:\folderA, then delete the file in G:\folderA.You are absolutely correct. My bad.
CyberalEasy enough to do with a Batch file.  Leaving work now.  Might get back online later tonight.Squashman,
No problem at all. I'll check later or tomorrow.
Thanks
CyberalThis turns out to be a simple one liner.
Code: [Select]FOR /R "G:\FolderA\" %%I in (*) DO IF NOT EXIST "C:%%~pnxI" DEL "%%~I"Squashman
Wow, this worked perfectly! Thanks so much.
Another quick question
I thought you could do multiple actions as long as you begin with '(' and end with ')'.  As shown below ....
Am I out in left field on this? I am on Windows 7 if that MAKES a difference

FOR /R "G:\testBUdir\" %%I in (*) DO (
IF NOT EXIST "C:%%~pnxI"
del %%~I
echo c:\%%~pnxI has been deleted
echo something again
)

Quote from: cyberal on December 23, 2011, 10:01:46 AM
Am I out in left field on this?

No, you are absolutely correct. You just need to make sure of your parentheses construction and that it follows all the proper syntax. For instance, what you have shown here will not work, because you did not "open" the IF NOT EXIST statement. It should be as follows:

Code: [Select]FOR /R "G:\testBUdir\" %%I in (*) DO (
  IF NOT EXIST "C:%%~pnxI" (
    del %%~I
    echo c:\%%~pnxI has been deleted
    echo something again
  )
)
I use the spacing for my own sanity, as it allows me to see exactly which parentheses CONSTRUCTS are being opened and closed, but you can use different or no spacing if you like.  
Works perfectly now!
Thanks for all your help and have a great holiday
cyberalYou are deleting from the G: DRIVE not from the C: drive so your echo statement should just be.
echo %%I has been deleted Quote from: Raven19528 on December 23, 2011, 10:38:04 AM
I use the spacing for my own sanity, as it allows me to see exactly which parentheses constructs are being opened and closed, but you can use different or no spacing if you like.
Same here but I like to use TABS.Truely appreciate all the help !
Thanks again and have a great holiday .... 
cyberal


Discussion

No Comment Found