Question:
I have to write a batch program to change the text inside 1000 files at the same time?
anonymous
1970-01-01 00:00:00 UTC
I have to write a batch program to change the text inside 1000 files at the same time?
Four answers:
anonymous
2016-04-03 04:25:22 UTC
Here is a way that you can do it using Excel or some other spreadsheet. Import the file into Excel. If you have one entry per text line, you will get one entry in the first cell of each row. Sort the file so that duplicate rows sort together. In column B, row 2, define the cell to be +IF(A2=A1), 1, 0) Duplicate that formula down all the rows of the spreadsheet. It will be 0 for the first item of each duplicate set. Save the page as a text file. The formulas go away and you save only the text and the 0's and 1's. Reopen the saved file in Excel and sort on column B. All the 0 items will be together. They are your unique items. Delete the other rows, clear column B, and save.
anonymous
2008-01-03 02:36:02 UTC
I have no idea about batch programs, but apparently your v1 variable is encased in %... yet, your %MultiServerClient isn't... perhaps it would be... %MultiServerClient%%v1%, and I'm wondering if the V1, v1 would be a problem or is it not case sensitive.
mrkumarji
2008-01-03 03:16:25 UTC
It would be better use a script language like Perl,



-- you need perl installed on your system -- easy to install.



The Perl will support all platform.



you can easily write a Perl script - thru which you can modify the file. - The you will sample code on the web.



I hope this will be helpful.



Good Luck.

kumar
Kevin
2008-01-06 07:07:30 UTC
[skip to the bottom for a better way to do this]



There are a couple of issues with your syntax that you'll need to address.



1) The %variable in a "for" command should be a single letter. That's why you're getting the error that you see. You

can do a quick test of this on a command line with the following incorrect syntax,



for /d %test in (*) do @echo %test



which results in,

%test was unexpected at this time.



To correct this, try

for /d %t in (*) do @echo %t



You should see a listing of directories in the current directory, provided there are directories to list. This leads us to the second part..



2) The "/d" switch plus a wildcard in the (...) section will assume that you're trying to list directories instead of filenames.



So the line (corrected for the variable),

for /d %%m in (D:\MulticlientServer1000\*) do ...



will attempt to run your next commands on directory names that it finds in "D:\MulticlientServer1000\" instead of any individual filenames it finds.



You can demonstrate this by creating a couple empty test directories in "D:\MulticlientServer1000\" then run this command line to list the directories,



for /d %m in (D:\MulticlientServer1000\*) do echo %m



You should see a listing of those subdirectories.



The way that I like to get a clean listing of filenames that may include spaces is like this,



for /f "tokens=*" %i in ('dir /a-d /b') do @echo "%i"



This gets a bare directory listing of all files and uses the "tokens=*" to prevent truncating filenames that contain spaces. You can change the dir command to match the filenames you're looking for, like,



for /f "tokens=*" %i in ('dir d:\MultiServerClient1000\ /a-d /b') do @echo "%i"



3) It looks like you're trying to use the "V1" variable as an incrementer to append to your filenames. The problem I see here is that you have your initial declaration (set v1=1) inside your do loop. You should probably put that at the top of your batch file and outside of your do loop. Then it will start at 1 and increment inside the 'do' with your 'set /a v1=v1+1' command.



However, this brings up another issue - enviroment variable expansion versus delayed environment variable expansion. Basically, without delayed environment variable expansion, you're going to have a hard time getting your increment counter to work properly because of the way that the environment variable is stored and when/how it is read.



You can demonstrate this issue with the following batch file,



@echo off

set v1=1

for /L %%i in (1,1,10) do (

set /a v1=v1+1

@echo %v1%

)



This sets v1=1, then runs through a loop to increment v1 and echo it back to the screen. The problem is that the v1 is echo'ed back during the batch as "1". If you look at your environment variables after you run this batch, then V1=11. So something fishy is going on with how the environment variable is read and displayed during runtime.



You can get around this by enabling delayed environment variable expansion when you invoke a cmd window by calling it with "cmd /v", then using the "!" delimiter during variable expansion, like this,



set v1=1

for /l %%i in (1,1,10) do (

set /a v1=!v1!+1

@echo !v1!

)



and now, the incremented variable is properly displayed during runtime. (Note: make sure to close this window when you're finished testing delayed environment variable expansion - or else you'll introduce issues with variable expansion in any batches you run in this command window later.)



===================

[BETTER WAY]

===================

However, this is getting WAY TOO complicated for what you need to do, in my opinion. Instead of using an environment variable as an incrementer, let's just use a different "for" command ... maybe something like,





for /L %%m in (1,1,1000) do (

@echo ssr 0 MultiServerClient MultiServerClient%%m MultiServerClient%%m.java

)





This uses the "for /L" command/switch to automatically increment the %%m variable. You can use that to call the appropriate mutliserverclient%%m filenames.



In my example I'm just doing a screen echo of an example ssr command so you can see how it might run. For your batch, you would remove the "@echo" that comes before the "ssr" command. And, you would run this in the directory that contains your java files.



Also, Yahoo! Answers wraps long lines. The whole "@echo..." line should be on one line. In fact, now that we've simplified this command, you could put the entire thing on one line either in a batchfile or called from a commandline.



In a batch,



for /l %%m in (1,1,1000) do (@echo ssr 0 MultiServerClient MultiServerClient%%m MultiServerClient%%m.java)



On a commandline,



for /l %m in (1,1,1000) do (@echo ssr 0 MultiServerClient MultiServerClient%m MultiServerClient%m.java)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...