tags:

views:

213

answers:

2

I need to create a batch file that reads a file with one line and then renames the same file based on the contents. The file will have one number and the condition to rename the file is this:

If content of file > 100 then rename new.txt to old.txt else rename new.txt to new1.txt

Thanks for the help!

+2  A: 

Note: I'm assuming you don't need to do this in DOS but instead with a Windows batch file. Otherwise this gets far uglier than it needs to be.

If the file only has a single line the easiest way of reading the contents of that file is to use set /p and redirect the file's contents:

set /p Content=<new.txt

You then have the first line of the file in %Content% and can compare based on that:

if %Content% GTR 100 (ren new.txt old.txt) else (ren new.txt new1.txt)

(GTR is the “greater than” operator; using > would obviously a bad idea.)

Joey
Thank you for the help with this.I used the solutions to create a windows batch file that looks like this:@echo off:BEGINpushd D:\Data_Files\FTP\MISCset /p Content=<Count.txt if %Content% GTR 100 (ren Count.txt OK.txt) else (ren Count.txt Abort.txt) :ENDBut every time I run it, I get a "The process cannot access the file because it is being used by another process. More? " error...Ideas?
Irshan Syed
A: 
@echo off
set /p line=<new.txt
if %line% GTR 100 (
  ren "new.txt" "old.txt"
)else (
  ren "new.txt "new1.txt"
)
ghostdog74