Page 1 of 1

where that space come from?

Posted: 14 Sep 2023 11:37
by goodywp
I have a task to copy some files from different subfolders as this below code. Basically based upon the files from subfolders to copy to one folder as below:

Code: Select all

@ECHO OFF
set Base_Scripts=C:\CA_CONVERT
set scheme_path="%Base_Scripts%\Tetra_Schemes"
set profmaster=mockup



del /F /Q %scheme_path%\%profmaster%\*.*


cd /d %Base_Scripts%\Tetra_Schemes

if exist *.txt del *.txt

set sch_1=T505-08667-0112
set sch_2=T501-08700-0100
set sch_3=T505-08667-0203
set sch_4=T505-08667-0208
set sch_5=T505-08961-0103
::set fld=%~2
::set fld=%fld:"=%
::set snfile=%~3
::set snfile=%snfile:"=%
::set comp=%snfile:~0,6%
set comp=829501

dir /s /b>>all_scheme.txt

find /i "%sch_1%"<all_scheme.txt>>list_sch.txt
find /i "%sch_2%"<all_scheme.txt>>list_sch.txt
find /i "%sch_3%"<all_scheme.txt>>list_sch.txt
find /i "%sch_4%"<all_scheme.txt>>list_sch.txt
find /i "%sch_5%"<all_scheme.txt>>list_sch.txt

find /i "S3S"<list_sch.txt>>sch_path.txt

copy "%Base_Scripts%\Scripts\Sch_Temp\%comp%\*_Sch.txt" "%Base_Scripts%\Tetra_Schemes"

find /i "%sch_1%"<sch_path.txt>>%sch_1%.txt
>>"%sch_1%_list.txt" FINDSTR /G:"%sch_1%_Sch.txt" "%sch_1%.txt"
For /F "delims=" %%a in (%sch_1%_list.txt) DO COPY "%%a" "%scheme_path%\%profmaster%\%%~nxa"

find /i "%sch_2%"<sch_path.txt>>%sch_2%.txt
>>"%sch_2%_list.txt" FINDSTR /G:"%sch_2%_Sch.txt" "%sch_2%.txt"
For /F "delims=" %%a in (%sch_2%_list.txt) DO COPY "%%a" "%scheme_path%\%profmaster%\%%~nxa"

find /i "%sch_3%"<sch_path.txt>>%sch_3%.txt
>>"%sch_3%_list.txt" FINDSTR /G:"%sch_3%_Sch.txt" "%sch_3%.txt"
For /F "delims=" %%a in (%sch_3%_list.txt) DO COPY "%%a" "%scheme_path%\%profmaster%\%%~nxa"

find /i "%sch_4%"<sch_path.txt>>%sch_4%.txt
>>"%sch_4%_list.txt" FINDSTR /G:"%sch_4%_Sch.txt" "%sch_4%.txt"
For /F "delims=" %%a in (%sch_4%_list.txt) DO COPY "%%a" "%scheme_path%\%profmaster%\%%~nxa"

find /i "%sch_5%"<sch_path.txt>>%sch_5%.txt
>>"%sch_5%_list.txt" FINDSTR /G:"%sch_5%_Sch.txt" "%sch_5%.txt"
For /F "delims=" %%a in (%sch_5%_list.txt) DO COPY "%%a" "%scheme_path%\%profmaster%\%%~nxa"
The above code is working fine. However, if these scheme folders not hardcoded as above, instead they come from a file as below then I got an issue

Code: Select all

@ECHO OFF
set Base_Scripts=C:\CA_CONVERT
set scheme_path="%Base_Scripts%\Tetra_Schemes"
set profmaster=mockup



del /F /Q %scheme_path%\%profmaster%\*.*
for /f "tokens=1-9 delims=," %%A in (pkg.txt) do call :schemeUpdt %%A %%B %%C %%D %%E %%F %%G %%H %%I

EXIT /B %ERRORLEVEL% 


:schemeUpdt

cd /d %Base_Scripts%\Tetra_Schemes

if exist *.txt del *.txt

set sch_1=%~5 
echo %sch_1%
set sch_2=%~6
echo %sch_2%
set sch_3=%~7
echo %sch_3%
set sch_4=%~8
echo %sch_4%
set sch_5=%~9
echo %sch_5%
set snfile=%~3
set snfile=%snfile:"=%
set comp=%snfile:~0,6%
echo sch


dir /s /b>>all_scheme.txt

find /i "%sch_1%"<all_scheme.txt>>list_sch.txt
find /i "%sch_2%"<all_scheme.txt>>list_sch.txt
find /i "%sch_3%"<all_scheme.txt>>list_sch.txt
find /i "%sch_4%"<all_scheme.txt>>list_sch.txt
find /i "%sch_5%"<all_scheme.txt>>list_sch.txt

find /i "S3S"<list_sch.txt>>sch_path.txt

copy "%Base_Scripts%\Scripts\Sch_Temp\%comp%\*_Sch.txt" "%Base_Scripts%\Tetra_Schemes"

find /i "%sch_1%"<sch_path.txt>>%sch_1%.txt
>>"%sch_1%_list.txt" FINDSTR /G:"%sch_1%_Sch.txt" "%sch_1%.txt"
For /F "delims=" %%a in (%sch_1%_list.txt) DO COPY "%%a" "%scheme_path%\%profmaster%\%%~nxa"

find /i "%sch_2%"<sch_path.txt>>%sch_2%.txt
>>"%sch_2%_list.txt" FINDSTR /G:"%sch_2%_Sch.txt" "%sch_2%.txt"
For /F "delims=" %%a in (%sch_2%_list.txt) DO COPY "%%a" "%scheme_path%\%profmaster%\%%~nxa"

find /i "%sch_3%"<sch_path.txt>>%sch_3%.txt
>>"%sch_3%_list.txt" FINDSTR /G:"%sch_3%_Sch.txt" "%sch_3%.txt"
For /F "delims=" %%a in (%sch_3%_list.txt) DO COPY "%%a" "%scheme_path%\%profmaster%\%%~nxa"

find /i "%sch_4%"<sch_path.txt>>%sch_4%.txt
>>"%sch_4%_list.txt" FINDSTR /G:"%sch_4%_Sch.txt" "%sch_4%.txt"
For /F "delims=" %%a in (%sch_4%_list.txt) DO COPY "%%a" "%scheme_path%\%profmaster%\%%~nxa"

find /i "%sch_5%"<sch_path.txt>>%sch_5%.txt
>>"%sch_5%_list.txt" FINDSTR /G:"%sch_5%_Sch.txt" "%sch_5%.txt"
For /F "delims=" %%a in (%sch_5%_list.txt) DO COPY "%%a" "%scheme_path%\%profmaster%\%%~nxa"
I got below error
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
T501-08700-0100
T505-08667-0112
T505-08667-0203
T505-08667-0208
T505-08961-0103
sch
C:\CA_CONVERT\Scripts\Sch_Temp\829501\T501-08700-0100_Sch.txt
C:\CA_CONVERT\Scripts\Sch_Temp\829501\T505-08667-0112_Sch.txt
C:\CA_CONVERT\Scripts\Sch_Temp\829501\T505-08667-0203_Sch.txt
C:\CA_CONVERT\Scripts\Sch_Temp\829501\T505-08667-0208_Sch.txt
C:\CA_CONVERT\Scripts\Sch_Temp\829501\T505-08961-0103_Sch.txt
5 file(s) copied.
File not found - .TXT
FINDSTR: Cannot read strings from T501-08700-0100 _Sch.txt
The system cannot find the file _list.txt.
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.

+++++++++++++++++++++++++++++++++++++++++++++++++++
where these errors, mainly the extra space...came from? I double checked the pkg.txt
they have no space as below
==========================================
A_Infra,TSA,8295011142_TSA.P3A,DL_customer,T501-08700-0100,T505-08667-0112,T505-08667-0203,T505-08667-0208,T505-08961-0103
==========================================
It always hit the first variable as in this cases, T501-08700-0100 the rest are fine....
Just can not figure out why the space always hit the first as
set sch_1=%~5

Tanks for your input!

Re: where that space come from?

Posted: 14 Sep 2023 13:16
by goodywp
sorry my bad, it was from
set sch_1=%~5
This one there was a space after %~5

Re: where that space come from?

Posted: 15 Sep 2023 08:49
by jeb
It's always a good idea to use the safe SET-Syntax.
This is safe against trailing white spaces and additionally it avoid problems with special characters

Code: Select all

set "sch_1=%~5"

Re: where that space come from?

Posted: 18 Dec 2023 15:50
by goodywp
Thanks a lot!