0

I have a batch file that will read values from a .ini file line by line, and use these variables to do other steps.

config.ini

Folder=Z:\task\first task\archive\
FileName=aFileName.xlsx
break

Then in my batch file i read the lines like the following and copy it to somewhere else like the following: job.bat

for /f "tokens=1,2 delims==" %%a in (.\config.ini) do (
    if %%a==Folder set Folder=%%b
    if %%a==Format set Format=%%b
    if %%a==break (
       call :copyFile
    )
)
GOTO :EOF
:copyFile
xcopy %Folder%%Filename% D:\abc\ /f /y
GOTO :EOF

so the problem is at %Folder%%Filename% for some reason, there is a space between the folder path and File name so it turns out to be

Z:\task\first task\archive\ aFileName.xlsx

I have tried to echo the variable after reading from file line by line and do echo "%Folder%" and i can see there are white space at the end of the variable. is there anyways to trim ONLY the end of the variable? I have tried some suggestions online, but it seems like it would also remove the white space within the variable, as you can see I do have a white space in the folder variable

Also, is there a way to exclude some of the charaters during string replacement. eg:

set filename=name_mm.txt
call set replaceName=%filename:mm=01%

this will replace the character mm to 01 in result of name_01.txt but in another case, i got a filename that also have mm inside eg:

set filename=communicate_mm.txt

in this case, is there a way to ONLY replace the mm add the end? I know i could've replace the string _mm instead of mm, but there are other cases that the file name format like yyyymmdd.

kown owl
  • 43
  • 6
  • You're already aware of substring modification, So why not use it? `If "%filename:~-2%" == "mm" Set "filename=%filename:~0,-2%newsuffix"` – T3RR0R Nov 04 '20 at 03:47
  • 2
    Is there a _space_ behind `set Folder=%%b`? when you use the quoted syntax `set "Folder=%%b"` there would not be such problem. Or does in come from the source file? – aschipfl Nov 04 '20 at 04:50
  • *has* it to be `mm`? You could use `set "filename=communicate_@@.txt"` (or any other char - even chars that are invalid for filenames - as you replace them anyway) – Stephan Nov 04 '20 at 09:23
  • The loop could be written as `for /F "usebackq delims=" %%I in ("%~dp0config.ini") do if /I not "%%~I" == "break" (set "%%I") else call :copyFile`. However, read my answers on [Why is no string output with 'echo %var%' after using 'set var = text' on command line?](https://stackoverflow.com/a/26388460/3074564) and [Symbol equivalent to NEQ, LSS, GTR, etc. in Windows batch files](https://stackoverflow.com/a/47386323/3074564) and [Why does ECHO command print some extra trailing space into the file?](https://stackoverflow.com/a/46972524/3074564) – Mofi Nov 04 '20 at 13:46

1 Answers1

0
:copyFile
if "%folder:~-1%" equ " " set "folder=%folder:~0,-1"&goto copyfile
xcopy %Folder%%Filename% D:\abc\ /f /y

would probably cure the spaces-at the-end problem.

You really shouldn't ask two questions in one - at the price charged per question, it's not expensive.

But for the second question, why no replace _mm instead of mm?

But if you want the last 3 characters, then

%varname:~-3%

and all but the last 3

%varname:~0,-3%

which is documented in the set instruction - try set /? from the prompt.

Magoo
  • 68,705
  • 7
  • 55
  • 76