Batch file to delete certain files from....

Csdc Flatout 2 - virtual programming ltd
March 5, 2010 at 02:40:41
Specs: Windows 7, 2048
Hello Everyone,

I have two batch files I'm working on and I'm stumped.

The first is: I need a batch file that, when executed, will go to one certain folder and delete all MDMP files found and then close.

The second: I need a batch file that can modify .ini files... let me explain this one in depth...

The batch file, once executed, should ask the user "What do you want to name yourself". The user would type in, for example, "Steven".

Then the batch file would go to info.ini, find line 36 which contains "name = unnamed", delete and/or replace "unnamed" with the users given name.

Any help would be greatly appreciated. This is my first post/topic on here but I've been reading this site for about a month now. Extremely great community here!

Thanks kindly.

[NOTE: Sorry about the "Product" listed in this topic, I don't understand how that option functions and I couldn't delete any selection once I figured I didn't need it... that's a strange drop down box]


See More: Batch file to delete certain files from....

Report •


#1
March 5, 2010 at 02:45:32
One is easy, so just have that already. You'll want this:

@echo off

del /f /q "C:\this\is\my\directory\*.mdmp"


Report •

#2
March 5, 2010 at 02:47:40
> [NOTE: Sorry about the "Product" listed in this topic,
> I don't understand how that option functions and ..
>

Me neither, IMO it's useless they way it currently is. This site is great in many ways, but that part is just badly done.


Report •

#3
March 5, 2010 at 02:53:02
Good lord, way quick response... thanks!

Any ideas on the other batch process?

Again, thank you.


Report •

Related Solutions

#4
March 5, 2010 at 02:56:42
@echo on
setlocal enabledelayedexpansion

set line2change=36
set infile=test.txt
set outfile=test_out.txt

REM ---

set n=1
set /p name=What do you want to name yourself :

del /q %outfile%

for /F "tokens=*" %%f in (%infile%) do (
if !n! NEQ %line2change% echo %%f>> %outfile%
if !n! EQU %line2change% echo name=%name%>> %outfile%
set /a n+=1
)

REM rename %outfile% %infile%


Report •

#5
March 5, 2010 at 03:00:27
You are my savior today, I can't thank you enough.

Have a great day!

(Who would have thought I'd have my answer(s) within 10 minutes, lol)


Report •

#6
March 5, 2010 at 03:27:23
tvc, it seems the 2nd batch file code creates a new ini file with a different name.

I tried this edit afterward:

@echo on
setlocal enabledelayedexpansion

set line2change=8
set infile=test.ini
set outfile=test.ini

REM ---

set n=1
set /p name=What do you want to name yourself :

del /q %outfile%

for /F "tokens=*" %%f in (%infile%) do (
if !n! NEQ %line2change% echo %%f>> %outfile%
if !n! EQU %line2change% echo name=%name%>> %outfile%
set /a n+=1
)

REM rename %outfile% %infile%

I created a test.ini file and put the line I want changed to line 8 in this case.

When I tried to run just your original code, it didn't do anything. I know this is close... very close. I'm almost there.


Report •

#7
March 5, 2010 at 03:32:02
Here, let me clarify actually what happened.

I ran your code, the file was unchanged but a new file appeared with a different name. I'm guessing it was because the output file name is different (does it have to be for this code to work?). So making the changes I have up above, it now has both the input file and output file the same, unfortunately this deletes the file once the change is completed....

So, like I said... close, so close.


Report •

#8
March 5, 2010 at 05:26:48
try, change this line:
REM rename %outfile% %infile%


to:
:: this gives small chance of backup
copy /y %infile% .\backup.xxx
move /y %outfile% %infile%


Report •

#9
March 5, 2010 at 06:56:45
Yeah, I deliberate created a new file, since the initial file may still be needed (for whatever reason). The last line (the one with the REM) was there to exactly handle that issue : take away the REM command (in that line) and he would overwrite the old file with the new.

I should have mentioned, but the whole thing with those two variables is there because they can't be the same. When you set both variables to the same file, I expect them to error out. You cannot read and parse a file which is input and output file at the same time !

You can always add this one-liner:

if /I "%infile%"=="%outfile%" echo The input and output file (%infile%) may not be the same & goto :EOF


Report •

Ask Question