Solved Swapping The Data In 2 Elements Of An Array

September 20, 2013 at 12:07:55
Specs: Windows Vista, 1.6 gb 4gb
I've come unstuck with trying to transpose 2 elements of an Array and can't find any info on how this might be done. I can read the text file into the array Ok but after that I'm lost. Basically the text files all start of the same way:
Line 1 - short piece of text
Line:2 - blank
Line:3 - short piece of text
after that there is a summary which is not not the problem it is only the first 3 lines I am interested in. I wish to keep line 2 as it is and replace line 1 with line 3 and line 3 with line 1 - in other words just swapping them round. I can't do a Replace because the pieces of text are all different. Obviously, I could open each file and change it ,amually but there are so many of them that I am looking for a quick way of doing this.

Any help, pointers would be greatly appreciated

Thank you


See More: Swapping The Data In 2 Elements Of An Array

Report •

✔ Best Answer
October 4, 2013 at 01:03:06
FWIW, someone may want to swap several pairs of lines.

:: swaps lines in files according to the pairs of nums in SWAPLIST

:: lineswpr.bat 3 October 2013 16:20
::====== script starts here ===============
@echo off & setLocal enableDELAYedeXpansioN

set/p WORK= work dir ?

for /f "tokens=* delims= " %%X in ('dir/b !WORK!') do (
copy !WORK!\%%X # > nul
call :main
move/y NEWFILE !WORK!\%%X
)
del #
goto :eof

:main
@echo off > NEWFILE
for /f "tokens=1* delims=[]" %%a in ('find /v /n "" ^< #') do (
set S%%a=%%b
set/a M=%%a
)

for /f "tokens=1-2 delims= " %%i in (SWAPLIST) do (
if %%i leq !M! if %%j leq !M! (
set X=!S%%i!
set S%%i=!S%%j!
set S%%j=!X!
)
)

for /L %%i in (1 1 !M!) do (
echo.!S%%i!
) >> NEWFILE
goto :eof
::====== script ends here =================

=====================
M2 Golden-Triangle



#1
September 20, 2013 at 13:05:13
::====== script starts here ===============
::
:: juniper.bat 2013-09-21 2:36:31.67
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

:main
for /f "tokens=1* delims=[]" %%a in ('find /v /n "" ^< myfile') do (
if "%%b" equ "" (
set S%%a=
) else (
set S%%a=%%b
)
set M=%%a
)

set X=!S1!
set S1=!S3!
set S3=!X!

for /L %%i in (1 1 !M!) do (
echo.!S%%i!
) >> NEWFILE
goto :eof
::====== script ends here =================

=====================
M2 Golden-Triangle


Report •

#2
September 20, 2013 at 23:41:27
Hi, Mechanix2Go, not VBscript, but this is certainly ace! It definitely does the trick and changes the text to what I need. I won't profess to understand the intricacies of your coding in the batch file. So as I would struggle to modify it properly, would you be able to spare the time to code it so that it would process not just a single file but a whole directory of similar files?

Thank you

message edited by JuniperGreen


Report •

#3
September 21, 2013 at 01:51:25
:: modify the pushd to the folder where your files are
::
::
::
::======  script starts here  ===============
@echo off & setLocal enableDELAYedeXpansioN
pushd C:\TEMP\-\09-21\X

for /f "tokens=* delims= " %%X in ('dir/b') do (
move/y %%X #
call :main
move/y NEWFILE %%X
)
del #
goto :eof

:main
@echo off > NEWFILE 
for /f "tokens=1* delims=[]" %%a in ('find /v /n "" ^< #') do (
if "%%b" equ "" (
  set S%%a=
  ) else (
  set S%%a=%%b
  )
set M=%%a
)

set X=!S1!
set S1=!S3!
set S3=!X!

for /L %%i in (1 1 !M!) do (
echo.!S%%i!
) >> NEWFILE
goto :eof
::======  script ends here  =================

=====================
M2 Golden-Triangle


Report •

Related Solutions

#4
September 21, 2013 at 05:46:57
Hi, not sure what the problem is but I can't get the batch file to work. The only change I made to it was to change pushd to D:\X the directory into which I put the text files and the .bat file. When run the result was all the text files as well as the .bat file were deleted while a file named "#" was created and turned out to be the sole converted text file. As I said, I'm not competent enough to follow the syntax of the .bat file but I did wonder how it could output multiple files as NEWFILE seemed (to me anyhow) to be the only named output. Were there any other changes I should have made other than to pushd?

I appreciate your taking the time to code this for me and it will probably turn out to be something that I haven't done right.

Thank you.


Report •

#5
September 21, 2013 at 13:29:37
You need ONLY the files to process in D:\X [not the bat].

I have:

C:\TEMP\-\09-21\x\MYFILE
C:\TEMP\-\09-21\x\thatfile
C:\TEMP\-\09-21\x\THISFILE

=====================
M2 Golden-Triangle


Report •

#6
September 22, 2013 at 04:02:43
Hi, Mechanix2Go, I discovered that the bat file couldn't exist in either D:\X or a directory off D:\X. It have to be an entirely separate one eg D:\Swap. Once I got that sorted ...that was me sorted! I told you it would be something I'd done!
This is fantastic, does the job perfectly and is greatly appreciated.

Very many thanks for your time and support!

message edited by JuniperGreen


Report •

#7
October 4, 2013 at 01:03:06
✔ Best Answer
FWIW, someone may want to swap several pairs of lines.

:: swaps lines in files according to the pairs of nums in SWAPLIST

:: lineswpr.bat 3 October 2013 16:20
::====== script starts here ===============
@echo off & setLocal enableDELAYedeXpansioN

set/p WORK= work dir ?

for /f "tokens=* delims= " %%X in ('dir/b !WORK!') do (
copy !WORK!\%%X # > nul
call :main
move/y NEWFILE !WORK!\%%X
)
del #
goto :eof

:main
@echo off > NEWFILE
for /f "tokens=1* delims=[]" %%a in ('find /v /n "" ^< #') do (
set S%%a=%%b
set/a M=%%a
)

for /f "tokens=1-2 delims= " %%i in (SWAPLIST) do (
if %%i leq !M! if %%j leq !M! (
set X=!S%%i!
set S%%i=!S%%j!
set S%%j=!X!
)
)

for /L %%i in (1 1 !M!) do (
echo.!S%%i!
) >> NEWFILE
goto :eof
::====== script ends here =================

=====================
M2 Golden-Triangle


Report •

Ask Question