Delete files and not replace them.

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
Ianw
Junior Coder
Posts: 26
Joined: Mon Jan 29, 2007 8:37 pm
Location: UK

Delete files and not replace them.

Post by Ianw » Tue Aug 25, 2009 3:50 pm

Hi,
I've done a search resulting in my head is in a spin. :?

What I want to do is to search a partition for files with the same name, that is, in all the folders in the partition and not to replace them, i.e. simply delete the file.

The file is Blueprint.bak and its in RailWorks simulator program folders.
When a route is installed there is that file in a lot of the folders, if they are not removed the sim won't run.
I've tried a few things but I can't get anywhere really.

Looking at the commands I had a look at IfFileExists, due to my lack of knowledge in this area of computing I have not got anywhere.

Can anyone assist please.
Thanks in advance.
Ian
Many thanks
Ianw

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Aug 25, 2009 4:07 pm

Will this do you:

Code: Select all

Let>root=c:

Let>RP_WINDOWMODE=2
Let>RP_WAIT=1
Run>cmd /c dir "%root%\Blueprint.bak" /s /b > "%TEMP_DIR%~temp_dir_list~"
ReadFile>%TEMP_DIR%~temp_dir_list~,dir_list

Separate>dir_list,CRLF,files
If>files_count>0
  Let>k=0
  Repeat>k
    Let>k=k+1
    Let>this_file=files_%k%
    DeleteFile>this_file
  Until>k=files_count
Endif
Last edited by Marcus Tettmar on Thu Aug 27, 2009 11:08 am, edited 2 times in total.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Ianw
Junior Coder
Posts: 26
Joined: Mon Jan 29, 2007 8:37 pm
Location: UK

Post by Ianw » Wed Aug 26, 2009 9:15 pm

Marcus,
I've tried your macro above but it does not work.
The macro is very, very fast, it has hardly started when it stops.

As the Blueprint.bak files are not deleted I'm thinking the macro may not be actually scanning the partition.
May I ask, in the first line :-
Let>root=c:\

Is it OK if I change the c to another drive letter, that is, e:
so that is will scan the E: partition.

Thanks for your help.
Many thanks
Ianw

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Wed Aug 26, 2009 9:45 pm

Is it OK if I change the c to another drive letter, that is, e:
so that is will scan the E: partition.
Hi Ian.
It is not only OK.... it is required that you change the letter in the "root" variable to the letter that relates to your partition. That is why Marcus placed the drive designation in a variable and put it at the top of the script where it can easily be found.

Hope this is helpful.

Ianw
Junior Coder
Posts: 26
Joined: Mon Jan 29, 2007 8:37 pm
Location: UK

Post by Ianw » Thu Aug 27, 2009 8:58 am

Thanks JRL, I did do that though the macro still does not work.

I ommited to inform that when I ran the macro there is a very short duration, a better description is a flash, of a message or something when I try to run the macro and that is the sum total of the macros activity.

I'm using MS v11 latest build.
Many thanks
Ianw

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Thu Aug 27, 2009 9:20 am

One thing I missed in my code was quotes - if any paths have spaces it would fail. Correct code:

Code: Select all

Let>root=c:

Let>RP_WINDOWMODE=2
Let>RP_WAIT=1
Run>cmd /c dir "%root%\Blueprint.bak" /s /b > "%TEMP_DIR%~temp_dir_list~"
ReadFile>%TEMP_DIR%~temp_dir_list~,dir_list

Separate>dir_list,CRLF,files
If>files_count>0
  Let>k=0
  Repeat>k
    Let>k=k+1
    Let>this_file=files_%k%
    DeleteFile>this_file
  Until>k=files_count
Endif
If it still doesn't work please get analytical. Use the debugger. Step through it. What is the value of dir_list? What happens after the Separate? What is the value of files_count? Try the CMD manually. E.g. type the following in a CMD prompt:

dir e:\Blueprint.bak /s /b

What do you see? Does DOS produce a list of files?
Last edited by Marcus Tettmar on Thu Aug 27, 2009 11:07 am, edited 1 time in total.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Ianw
Junior Coder
Posts: 26
Joined: Mon Jan 29, 2007 8:37 pm
Location: UK

Post by Ianw » Thu Aug 27, 2009 11:02 am

This is my first with the Debugger I hope I did it correctly.

I went into Edit, Debug, Run and Step.
------------------------------
ROOT=C:\
RP_WINDOWMODE=2
RP_WAIT=1
RP_RESULT=1
DIR_LIST= (no result on this one)
---------------------
Separate :-
FILES_Count=0
--------------------------------------
Try the CMD manually.
What do you see?
dir c:\Blueprint.bak /s /b = 4

Does DOS produce a list of files?
Yes it does.
There are a total of 4 files, 1 in each of 4 folders.
I tried it in E with 2 files and it found and listed them.

I hope that helps.
Many thanks
Ianw

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Thu Aug 27, 2009 11:07 am

Try removing the trailing slash from the first line where you set up your root folder. I just realised it is adding another slash in the Run line. Perhaps both slashes cause an error for the DOS command. I'll update my post accordingly.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Ianw
Junior Coder
Posts: 26
Joined: Mon Jan 29, 2007 8:37 pm
Location: UK

Post by Ianw » Thu Aug 27, 2009 12:08 pm

It works, great stuff.

VERY many thanks Marcus, you have no idea just how much time and searching that the macro saves me.
Many thanks
Ianw

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts