質問編集履歴

3

説明の修正

2021/07/23 13:14

投稿

anko__
anko__

スコア5

test CHANGED
File without changes
test CHANGED
@@ -64,12 +64,6 @@
64
64
 
65
65
 
66
66
 
67
- echo TOKYO:%TOKYO%
68
-
69
- echo OSAKA:%OSAKA%
70
-
71
-
72
-
73
67
  (
74
68
 
75
69
  for /f "delims= eol=#" %%a in (%BEFORE_CSV%) do (
@@ -122,6 +116,54 @@
122
116
 
123
117
 
124
118
 
119
+ 上記の処理でひとまず動くのですが、
120
+
121
+ 下記のイメージでサブルーチン化したいと考えています。
122
+
123
+ ```windowsバッチ
124
+
125
+ rem test.bat
126
+
127
+
128
+
129
+ ~中略~
130
+
131
+ call :returnFirstColumn %AFTER_TOKYO% TOKYO
132
+
133
+ call :returnFirstColumn %AFTER_OSAKA% OSAKA
134
+
135
+ ~中略~
136
+
137
+ if "%%b"=="%TOKYO%" (
138
+
139
+ ~中略~
140
+
141
+ rem csvの1項目目を取得する
142
+
143
+ :returnFirstColumn
144
+
145
+ setlocal
146
+
147
+
148
+
149
+ for /f "delims=," %%i in ("%1") do (
150
+
151
+ set val=%%i
152
+
153
+ )
154
+
155
+ endlocal && set %2=!val!
156
+
157
+ exit /b
158
+
159
+
160
+
161
+ ```
162
+
163
+
164
+
165
+
166
+
125
167
  ```csv
126
168
 
127
169
  #before.csv

2

文法の修正

2021/07/23 13:14

投稿

anko__
anko__

スコア5

test CHANGED
@@ -1 +1 @@
1
- windowsバッチ サブルーチンから値を返却する
1
+ buwindowsバッチ サブルーチンから値を返却する
test CHANGED
@@ -22,7 +22,13 @@
22
22
 
23
23
 
24
24
 
25
- rem 定義
25
+ @echo off
26
+
27
+
28
+
29
+ setlocal enabledelayedexpansion
30
+
31
+
26
32
 
27
33
  set BEFORE_CSV=before.csv
28
34
 
@@ -38,29 +44,49 @@
38
44
 
39
45
 
40
46
 
41
- call :returnFirstColumn %AFTER_TOKYO% TOKYO
47
+ if exist %AFTER_CSV% del %AFTER_CSV%
42
48
 
49
+
50
+
51
+ rem CSVから値を取得する
52
+
53
+ for /f "delims=," %%i in ("%AFTER_STRING_TOKYO%") do (
54
+
55
+ set TOKYO=%%i
56
+
57
+ )
58
+
43
- call :returnFirstColumn %AFTER_OSAKA% OSAKA
59
+ for /f "delims=," %%i in ("%AFTER_STRING_OSAKA%") do (
60
+
61
+ set OSAKA=%%i
62
+
63
+ )
64
+
65
+
66
+
67
+ echo TOKYO:%TOKYO%
68
+
69
+ echo OSAKA:%OSAKA%
44
70
 
45
71
 
46
72
 
47
73
  (
48
74
 
49
- for /f "tokens=1 eol=#" %%a in (%BEFORE_CSV%) do (
75
+ for /f "delims= eol=#" %%a in (%BEFORE_CSV%) do (
50
76
 
51
77
  set line=%%a
52
78
 
53
- for /f"delims=," %%b in("%%a") do(
79
+ for /f "delims=," %%b in ("%%a") do (
54
80
 
55
81
  if "%%b"=="%TOKYO%" (
56
82
 
57
- echo!line%BEFORE_STRING_TOKYO%=%TOKYO% (
83
+ echo !line:%BEFORE_STRING_TOKYO%=%AFTER_STRING_TOKYO%!
58
84
 
59
- else if "%%b"=="%OSAKA%" (
85
+ ) else if "%%b"=="%OSAKA%" (
60
86
 
61
- echo !line%BEFORE_STRING_OSAKA%=%OSAKA% (
87
+ echo !line:%BEFORE_STRING_OSAKA%=%AFTER_STRING_OSAKA%!
62
88
 
63
- else (
89
+ ) else (
64
90
 
65
91
  echo %%a
66
92
 
@@ -70,7 +96,7 @@
70
96
 
71
97
  )
72
98
 
73
- )>>%AFTER_CSV%
99
+ )>%AFTER_CSV%
74
100
 
75
101
 
76
102
 

1

誤字

2021/07/23 13:10

投稿

anko__
anko__

スコア5

test CHANGED
File without changes
test CHANGED
@@ -58,7 +58,7 @@
58
58
 
59
59
  else if "%%b"=="%OSAKA%" (
60
60
 
61
- echo !line%BEFORE_STRING_OSAKA%=&OSAKA% (
61
+ echo !line%BEFORE_STRING_OSAKA%=%OSAKA% (
62
62
 
63
63
  else (
64
64