1.

Solve : FOR.. each file that exist...?

Answer»

I am trying to figure out how to write a simple BATCH file on windows that will do this:
1. I have two directories, one is called TEST and the other one is TRANSFER
I would like to CHECK the directory in TEST if all the files with file prefix (*.sqc and *.sqr) also exist in directory TRANSFER. If they do I , then I would like to delete them from the TEST folder.
So far I have this, but it doesn't work because I have no ideas how to specify to go through all the *.sqc and *.sqr

FOR %%F (C:\pstemp\TRANSFER) DO IF %%F equ %%c:\pstemp\TEST\F del (C:\pstemp\TEST\%%F)

Any help will be appreciated.
Thank you

Your FOR statement contradicts what you wrote in your thread. It also stretches DOS syntax to new levels. Where did the %%c variable come from?

If you look at what you wrote, logically it becomes a simple chore. First get a list of the Test directory, check to see if exists in TRANSFER and if it does then go back and delete it from TEST.

Code: [Select]
for %%f in ('dir /b c:\pstemp\test\*.sqc') do (
if exist "c:\pstemp\transfer\%%f" del c:\pstemp\test\%%f)
)


I only showed you how to process the sqc files. If you apply the same logic to the sqr files, you're all set.

Hope this helps.

You should have been here a few weeks back. We had a special two for one deal on FOR statements. I have only the code that you gave me in a file called test.bat and it would not work. I don't understand. You syntax makes sense. I have the directories created and files are there as well. Am I not running this proprerly? I am suposee to have something ELSE in the file?

Thank you for you help.

I mistakenly left out the /f switch.

Code: [Select]
for /f %%f in ('dir /b c:\pstemp\test\*.sqc') do (
if exist "c:\pstemp\transfer\%%f" del c:\pstemp\test\%%f)
)


I don't have your directory structure or any sqc or sqr files on my system, so it's untested code. The code should work though.

Keep in touch. Still doesn't work.
What I have done to test this. I have created a folder on my hard drive names test and transfer. In the test folder I created DUMMY files like u_test.sqc, u_test2.sqr, u_test3.sqc, and u_test4.sqc and in the transfer folder I have u_test.sqc, u_test2.sqr, u_test3.sqc.
SO, when the script run, it should delete the u_test.sqc and u_test3.sqc from testsqr becuase those files exist in the transfer folder already.

Thank you

I guess my typing skills could use improvement.

Code: [Select]
for /f %%f in ('dir /b c:\pstemp\test\*.sqc') do (
if exist "c:\pstemp\transfer\%%f" del "c:\pstemp\test\%%f"
)


You created a TEST and a TRANSFER directory and they are in the PSTEMP directory? And everything is on the C: drive?

Since your batch file presumably doesn't turn off echo, you should be able to see how everything is expanded and interpreted. Please post any error MESSAGES and give us a hint. Your code worked!!! Thank you. I noticed that there was an extra ")" at the end and that was the reason for the problem.

for /f %%f in ('dir /b c:\pstemp\test\*.sqc') do (
if exist "c:\pstemp\sqr\%%f" del c:\pstemp\test\%%f)

I am trying to add the MOVE cmd to the last part, but it won't recognize the move command. Do you know if there is and AND cmd that i can use?

for /f %%f in ('dir /b c:\pstemp\test\*.sqc') do (
if exist "c:\pstemp\sqr\%%f" del c:\pstemp\test\%%f move c:\pstemp\sqr\%%f c:\pstemp\mprodsqr)I got it:

for /f %%f in ('dir /b c:\pstemp\test\*.sqc') do (
if exist "c:\pstemp\transfer\%%f" del c:\pstemp\test\%%f
if exist "c:\pstemp\transfer\%%f" move c:\pstemp\transfer\%%f c:\pstemp\mprodsqr)

Thank you so much for all your help!!!



Discussion

No Comment Found