Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

Bat delete files by old run number

Original Message
Name: WkEndHacker
Date: March 21, 2008 at 14:35:30 Pacific
Subject: Bat delete files by old run number
OS: XP Pro SP2
CPU/Ram: 3GB RAM
Model/Manufacturer: Dell Precision 360
Comment:
I realized I should post this new question in a new thread.

When I output CNC program files (txt files) with my CAD/CAM program, a script in the CAM database triggers a batch file which creates user named folders in a fixed local directory (C:\CncOut), and then puts the CNC program files into those folders and XCopies them to a mapped drive on the network.

Several of you have helped me with this so far, so I thank you very much. It's solved several problems and saved a good bit of time already.

I would like to delete previously output files left in this local directory though. The deletion would target old files of an earlier run number.

Let's suppose yesterday I stopped the batch file before it completed it's task of creating the folders, and it left these files in the CncOut directory.

R77001001.txt
R77001002.txt
R77001003.txt
R77002001.txt
R77002002.txt
R77003001.txt

77 is the run number, and the other numbers can be ignored for the time being. For example's sake, lets say I found a mistake in the program code and I re-output these files today. Subsequently the run numbers of the new output files would advance to the next number, looking like this:

R78001001.txt
R78001002.txt
R78001003.txt
R78002001.txt
R78002002.txt
R78003001.txt

So now I have yesterday's output and todays output in the CncOut directory, and currently if the older files are left on the local directory like this, my batch file will just add all of them to the new folder, and xcopy them to the mapped drive on the network.

I would like to delete the old folder (if it was created before the batch file was stopped), and the old files by comparing run numbers.

Is there a way to target just the old files by name?



Report Offensive Message For Removal


Response Number 1
Name: IVO
Date: March 21, 2008 at 15:36:31 Pacific
Subject: Bat delete files by old run number
Reply: (edit)
Here a code to delete CNCxxnnnnn.txt with xx less than the most recent one. Don't miss the setlocal statement to enable the dynamic variables marked with ! instead of the conventional %.

Beware the code is not tested.

@echo off
setlocal enabledelayedexpansion
......
set CNCprn=99
for /f %%j in ('dir /B C:\CNCOut\R*.txt') do (
set CNCfile=%%j
set CNCnum=+!CNCfile:~1,2!
set CNCnum=!CNCnum:+0=!
if !CNCnum! gtr !CNCprn! del C:\CNCOut\CNCprev.txt
set CNCprev=%%j
set CNCprn=!CNCnum!
)
......


Report Offensive Follow Up For Removal

Response Number 2
Name: IVO
Date: March 21, 2008 at 15:40:25 Pacific
Subject: Bat delete files by old run number
Reply: (edit)
Wait I have to correct the code...

Report Offensive Follow Up For Removal

Response Number 3
Name: IVO
Date: March 21, 2008 at 15:49:00 Pacific
Subject: Bat delete files by old run number
Reply: (edit)
Here the correct one, sorry...

@echo off
setlocal enabledelayedexpansion
......
set CNCprn=99
for /f %%j in ('dir /B C:\CNCOut\R*.txt') do (
set CNCfile=%%j
set CNCnum=+!CNCfile:~1,2!
set CNCnum=!CNCnum:+0=!
if !CNCnum! gtr !CNCprn! del C:\CNCOut\!CNCprev!*.txt
set CNCprev=!CNCfile:~0,3!
set CNCprn=!CNCnum!
)
......



Report Offensive Follow Up For Removal

Response Number 4
Name: Mechanix2Go
Date: March 22, 2008 at 02:35:45 Pacific
Subject: Bat delete files by old run number
Reply: (edit)
Hi IVO,

I did it a little different.

::== t.bat

@echo off
setLocal EnableDelayedExpansion

pushd C:\CNCOut

for /f "tokens=* delims= " %%a in ('dir/b/od R7*') do (
set str=%%a
)

set T=!str:~0,3!
echo today's run is !T!

for /f "tokens=* delims= " %%a in ('dir/b R7*') do (
set str=%%a
set D=!str:~0,3!
if !D! neq !T! echo del %%a
)

::== end of t.bat


=====================================
If at first you don't succeed, you're about average.

M2


Report Offensive Follow Up For Removal

Response Number 5
Name: WkEndHacker
Date: March 23, 2008 at 13:41:56 Pacific
Subject: Bat delete files by old run number
Reply: (edit)
I've spent some time examining these two examples (but apparently not enough) and I was wondering if you could provide an explanation?

Meanwhile I'll be looking at the help files...

Thanks


Report Offensive Follow Up For Removal


Response Number 6
Name: IVO
Date: March 24, 2008 at 04:36:26 Pacific
Subject: Bat delete files by old run number
Reply: (edit)
Hi WkEndHacker,

Here a improved and corrected version of my previous script (that retained some bugs) with comments embedded. It exploits dynamic variables that are marked with ! instead of the conventional % and are required if an environment variable is modified and referenced inside a loop or a compound statement, i.e. embraced by (). This behavior must be enabled by the statement

setlocal enabledelayedexpansion

introduced since Windows 2000.

@echo off
setLocal EnableDelayedExpansion
......
set count=1
:: select R*.txt files in ascending name order
for /f %%j in ('dir /B /ON C:\CNCOut\R*.txt') do (
set CNCnum=%%j
:: keep the first three chars of var
set CNCnum=!CNCnum:~0,3!
:: if not first file compare the first three chars
if !count! gtr 1 if not !CNCnum!==!CNCprn! del C:\CNCOut\!CNCprn!*.txt
set CNCprn=!CNCnum!
set /A count+=1
)
......

I hope that clarifies.

P.S.: the script posted deletes the old .txt files but doesn't handle folders as from your post this issue (user named folders) is not clear.


Report Offensive Follow Up For Removal



Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Bat delete files by old run number

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




Slow boot time

Trasnferring Documents from old HD

My k8T Neo-v usb's aren't working!

Date Modified = Date Created Time

system files on removable harddrive


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC