SOLVED Batch File Mutiple File Editing

Joined
Feb 5, 2017
Messages
5
Reaction score
0
Hi
First post here and I need some help please.

My day job is draughting, I use a CAD package called intellicad.
I need to modify 1000's of files performing the same change in each file.

The change in each file can be handled by using a script file within the program, this has been tested and works as needed.
Using various internet searches I came across a command line batch process using the 'FOR' command and some examples.
From here I generated my bat file:

FOR %%f in (E:\"0001-PURGE ALL"\*.dwg) do start /wait C:\"Program Files"\"CMS"\"CMS IntelliCAD 8.2 Premium Edition Plus"\Icad.exe "%%f" /b "E:\0001-PURGE ALL\Purge-ALL.scr"

(note: Purge-ALL.scr is the script file which is run within intellicad)

This has been tested and it works well.
So why do I need help??

Well this only works in folder 'E:\0001-PURGE ALL' however all sub folders are ignored.
again internet searches suggested either the /D or /R would enable sub folders but neither has worked.

I have added the /D or /R:
FOR /D %%f in(...........
FOR /R %%f in(...........

Can anyone help me get this to work in sub folder as well as the route folder??
(note: E:\0001-PURGE ALL is a test folder)

SteveN
Windows 10 home 64bit
 

Regedit32

Moderator
Joined
Mar 4, 2016
Messages
3,609
Reaction score
1,140
Hi Steven,

Welcome to the Forum.


Have you considered using the PowerShell built into Windows with your batch file?

You could then make use of the following flags:
  • -include *.dwg to force all files ending with extension .dwg to be removed from directory given
  • -recurse flag to remove items within subfolders.
  • -force to prevent administrative requests during recursion

For example:
Code:
PowerShell Remove-Item E:\0001-PURGE ALL -include *.dwg -recurse -force

In the event there was a specific DWG file you did need to keep though, for example alldogs.dwg you can make use of the -exclude flag to prevent its removal like so:
Code:
-exclude *dog*

This tells the recursion to comply with the previous -include flag, but to exclude any DWG files with the string dog contained in the file name.

You can include multiple file extensions, separated by a comma, for example:
Code:
PowerShell Remove-Item E:\0001-PURGE ALL -include *.txt,*.rtf,*.dwg -recurse -force

Or, to simply remove everything except for example the installer file then:
Code:
PowerShell Remove-Item E:\0001-PURGE ALL\* -exclude *install* -recurse -force

Regards,

Regedit32
 
Joined
Feb 5, 2017
Messages
5
Reaction score
0
Hi Regedit32

Thanks for the reply.

No I have not considered powershell, to be honest your post was the first time I heard of it's existence!

My simple batch file includes a 'delete *.bak' to delete the backup files created by the batch file as it runs.

The purpose of the exercise is to remove 'purge' all redundant data in the dwg files and the save them. For each file a '.bak' file is created and at the end of the run these 'bak' files are deleted.

Thanks
SteveN
 

Regedit32

Moderator
Joined
Mar 4, 2016
Messages
3,609
Reaction score
1,140
Hi Regedit32

Thanks for the reply.

No I have not considered powershell, to be honest your post was the first time I heard of it's existence!

My simple batch file includes a 'delete *.bak' to delete the backup files created by the batch file as it runs.

The purpose of the exercise is to remove 'purge' all redundant data in the dwg files and the save them. For each file a '.bak' file is created and at the end of the run these 'bak' files are deleted.

Thanks
SteveN


Could you post your complete batch file then?

I would have thought a simple:
Code:
del E:\0001-PURGE ALL\*.BAK /s
would delete any *.bak files in the main folder and also its subfolders.

Regards,

Regedit32
 
Joined
Feb 5, 2017
Messages
5
Reaction score
0
Regedit32

My problem is getting the first line:

FOR %%f in (E:\"0001-PURGE ALL"\*.dwg) do start /wait C:\"Program Files"\"CMS"\"CMS IntelliCAD 8.2 Premium Edition Plus"\Icad.exe "%%f" /b "E:\0001-PURGE ALL\Purge-ALL.scr"

to work on sub folders, not the delete *.bak files.

Sorry if I confused you.

SteveN
 
Joined
Feb 22, 2014
Messages
1,654
Reaction score
343
If you can list all the files in a directory listing, you can process the listings one by one within the for statement. But lets try this first instead.

http://ss64.com/nt/for_r.html
FOR /R
Loop through files (Recurse subfolders)

Syntax
FOR /R [[drive:]path] %%parameter IN (set) DO command

Key
drive:path : The folder tree where the files are located.

set : A set of one or more files. Wildcards must be used.
If (set) is a period character (.) then FOR will
loop through every folder.

command : The command(s) to carry out, including any
command-line parameters.

%%parameter : A replaceable parameter:
in a batch file use %%G (on the command line %G)
This command walks down the folder tree starting at [drive:]path, and executes the DO statement against each matching file.

If the [drive:]path are not specified they will default to the current drive:path.
Examples:
Delete every .bak file in every subfolder starting at C:\temp
For /R C:\temp\ %%G IN (*.bak) do Echo del "%%G"

Try this: [I rearranged your code to match the layout of the example. I've not tried it myself yet]
Code:
FOR /R "E:\0001-PURGE ALL\" %%f in (*.dwg) do start /wait "C:\Program Files\CMS\CMS IntelliCAD 8.2 Premium Edition Plus\Icad.exe" "%%f" /b "E:\0001-PURGE ALL\Purge-ALL.scr"
 
Joined
Feb 5, 2017
Messages
5
Reaction score
0
Hi Clifford,

Thanks for the input.
SS64 was one of the websites I visited in my earlier attempts to find info on the 'for' command, but adding the /R didn't work :(



I tried your code with partial success.

I received the following error message:
upload_2017-2-8_7-44-39.png

"There was a problem while sending the command to the program"

I recognise this as one I receive when opening a drawing by double click on a file in windows explorer.
Selecting OK clears the message (and the first drawing being opened) and opens ALL remaining drawings in the current folder and all sub folders. However the script file does NOT run in any of the opened files. (it does run using my code in post #1).

I then tried your code again, but this time opened the program first, this time all files were opened but again my script file was not activated. The program sat there with 9 files open.

The script file, cleans up the files as the are opened and then executes the 'save' followed by 'exit' commands. So each file is opened one by one, processed and then closed. Once the final file is closed all '.BAK' files, created by the 'save' command, are then deleted.


[For Info:-
E:\0001-PURGE ALL\ contains files 1.dwg, 2.dwg & 3.dwg
E:\0001-PURGE ALL\test 1\ contains files 4.dwg, 5.dwg & 6.dwg
E:\0001-PURGE ALL\test 2\ contains files 7.dwg, 8.dwg & 9.dwg]

Thanks
SteveN
 
Joined
Feb 5, 2017
Messages
5
Reaction score
0
Hi Clifford,

I compared your code to that in my first post and noticed that you only had (") marks at the beginning and end here:

"C:\Program Files\CMS\CMS IntelliCAD 8.2 Premium Edition Plus\Icad.exe"

Where I had them like this:

C:\"Program Files"\"CMS"\"CMS IntelliCAD 8.2 Premium Edition Plus"\Icad.exe

Changing your code to reflect this change works great :) ran through a project with 7 sub folders and all files updated.
A big thanks to helping me sort my problem :D

SteveN
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top