Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: FOR.. each file that exist...  (Read 2877 times)

0 Members and 1 Guest are viewing this topic.

g4b

  • Guest
FOR.. each file that exist...
« on: November 28, 2005, 10:43:33 AM »
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


Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: FOR.. each file that exist...
« Reply #1 on: November 28, 2005, 01:31:19 PM »
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. 8)

You should have been here a few weeks back. We had a special two for one deal on FOR statements.  ;D
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

g4b

  • Guest
Re: FOR.. each file that exist...
« Reply #2 on: November 28, 2005, 03:19:58 PM »
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.


Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: FOR.. each file that exist...
« Reply #3 on: November 28, 2005, 03:55:46 PM »
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. 8)
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

g4b

  • Guest
Re: FOR.. each file that exist...
« Reply #4 on: November 29, 2005, 08:51:45 AM »
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


Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: FOR.. each file that exist...
« Reply #5 on: November 29, 2005, 10:23:20 AM »
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. 8)
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

g4b

  • Guest
Re: FOR.. each file that exist...
« Reply #6 on: November 30, 2005, 09:34:56 AM »
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)

g4b

  • Guest
Re: FOR.. each file that exist...
« Reply #7 on: November 30, 2005, 02:17:47 PM »
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!!!