Batch for swap char.

  • Thread starter Thread starter Alan Tang
  • Start date Start date
A

Alan Tang

Hello:

I would like to write a batch to swap the char with the following
format!

Original:
"08/01/2003 00:02:26.271","1132","6229"

After Swap:
"01/08/2003 00:02:26.271","1132","6229"

Would you mind to give me some hints?

Thanks!
 
Alan Tang said:
Original:
"08/01/2003 00:02:26.271","1132","6229"
After Swap:
"01/08/2003 00:02:26.271","1132","6229"

Look into the SET command, something like:-

@echo off & setlocal ENABLEEXTENSIONS
set var="08/01/2003 00:02:26.271","1132","6229"
set a=%var:~1,3%
set b=%var:~4,3%
set c=%var:~7,100%
set var="%b%%a%%c%
echo/%var%
 
Hi Ritchie,

Very thanks for your information. I am sorry to say that I need to perform in a file that will
have about 3000 line need to process.
I have try to use the FOR but it doesn't support SET at each process!

E.G.

for /f "tokens=1,* delims=," %a in (P01.csv) do echo %a,%b

Thanks!
 
Alan said:
Hello:

I would like to write a batch to swap the char with the following
format!

Original:
"08/01/2003 00:02:26.271","1132","6229"

After Swap:
"01/08/2003 00:02:26.271","1132","6229"

Would you mind to give me some hints?

Thanks!
- - - - - - - - - - begin screen capture - - - - - - - - - -
<Win2000> c:\cmd>demo\FixMySpreadsheet

<Win2000> c:\cmd>type c:\temp\MyNewSpreadsheet.txt
"01/08/2003 00:02:26.271","1132","6229"
"02/08/2003 00:02:27.271","1132","6229"
"03/08/2003 00:02:28.271","1132","6229"
"04/08/2003 00:02:29.271","1132","6229"

<Win2000> c:\cmd>rlist c:\cmd\demo\FixMySpreadsheet.cmd
=====begin c:\cmd\demo\FixMySpreadsheet.cmd ====================
01. @echo off
02. type nul > c:\temp\MyNewSpreadsheet.txt
03. for /f "tokens=*" %%a in (c:\temp\MySpreadsheet.txt) do call :swap %%a
04. :: del c:\temp\MySpreadsheet.txt
05. :: move c:\temp\MyNewSpreadsheet.txt c:\temp\MySpreadsheet.txt
06. goto :EOF
07. :swap
08. set rec=%*
09. set rec=%rec:~0,1%%rec:~4,2%/%rec:~1,2%%rec:~6%
10. echo>>c:\temp\MyNewSpreadsheet.txt %rec%
11. goto :EOF
=====end c:\cmd\demo\FixMySpreadsheet.cmd ====================
- - - - - - - - - - end screen capture - - - - - - - - - -

If this appears to work as you intend, then remove ':: ' from
lines 4 and 5 and you'll be all set.
 
Alan Tang said:
I have try to use the FOR but it doesn't support SET at each process!

Do something like this:-

@echo off & setlocal ENABLEEXTENSIONS
for /f "tokens=1-3 delims=," %%a in ('more^<P01.csv') do call :FixDate %%a %%b %%c
goto :EOF

:FixDate
for /f "tokens=1-2* delims=/" %%a in ('echo/%~1') do (
echo/"%%b/%%a/%%c",%2,%3
)
 
Back
Top