-2

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.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • 1
    Ah! There is once again a beginner in batch file coding not reading the usage help of the used [Windows commands](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/windows-commands) like `set`. The most important hint we can gave you is: Open a [command prompt](https://www.howtogeek.com/235101/) window, run `help` and look on the list, run `set /?` and read the output help carefully and completely from top of first to bottom of last page. There is explained on an __IF__ and a __FOR__ example when and how to use __delayed expansion__. – Mofi Apr 07 '22 at 17:27
  • 1
    I recommend to run also `for /?` and read the entire output help. There is nothing written about the string assigned to the environment variable. It looks like a `for /F` can be used inside the already used `for /F` or the already used `for /F` is used with other options to get the substrings (tokens) of interest for further processing without usage an environment variable and so without the need to used delayed expansion at all. Very often on having knowledge about used commands the solution is much easier than having thought before without any knowledge about the used Windows commands. – Mofi Apr 07 '22 at 17:32
  • 1
    Please read also [Why is no string output with 'echo %var%' after using 'set var = text' on command line?](https://stackoverflow.com/a/26388460/3074564) Do you see the small syntax error made by you after reading this answer on the command line `set /p string =< %%A`? Yes, that´s fine. No, insert below this line `set str & pause` and look very carefully on the output when batch file processing is halted after execution of `set str` because of command `pause`. – Mofi Apr 07 '22 at 17:36
  • Does this answer your question? [Variables are not behaving as expected](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected) – Gerhard Apr 07 '22 at 17:55

1 Answers1

0
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.

Magoo
  • 77,302
  • 8
  • 62
  • 84