Home > Mobile >  Cannot set environmental variable inside a loop in a batch script
Cannot set environmental variable inside a loop in a batch script

Time:04-08

I have a batch script that works great and it does the following:

  • download EDI files from sftp server
  • generate a dynamic ftp scripts based on the filenames that were downloaded
  • uploads the file to an internal ftp server via FTP script

I need to modify the batch file to look at the content as some data coming in maybe missing a 0. I do have the logic on how to do this, however, I cannot add the logic to the loop I have as variables do not work.

Below is loop where I want to add the part to correct missing leading zero (that would be in position 4):

for /F "tokens=*" %%A in (c:\scripts\permitsin.txt) do (
  echo put %%A /qsys.lib/MAEPFTDTA.lib/PERMITIN.file/%%A >> c:\scripts\ftp-to-scoopsoft.ftp1
)

What I need to do is add an environment variable which will be the content of the file and below is the sample code:

set /p string =< %%A
echo Value for string is set to %string%
echo %string:~9,1% | find "ECHO is on." > nul
if not errorlevel 1 goto Found0
echo no space found
goto End
:Found0
echo found space
echo below is what the correct number needs to be
echo %string:~0,3%0%string:~3% > %%A
:End

I need this to work in batch file as security requirements does not allow PowerShell on the servers.

CodePudding user response:

for /F "tokens=*" %%A in (c:\scripts\permitsin.txt) do (
 for /F "delims=" %%S in (%%A) do (
  setlocal enabledelayedexpansion
  set "string=%%S"
  if "!string:~9,1!"==" " echo !string:~0,3!0!string:~3!> %%A
  endlocal
 )
 echo put %%A /qsys.lib/MAEPFTDTA.lib/PERMITIN.file/%%A >> c:\scripts\ftp-to-scoopsoft.ftp1
)

should do what you appear to need. Test on sample data first, of course.

Here's the how : Stephan's DELAYEDEXPANSION link

And it saves a lot of work if you tell us what you want to do rather than how you want to do it.

  • Related