-2

I understand that this is a common problem but I am unable to decipher the answers and suggestion in other questions to make my specific script work.

@echo on
setlocal EnableDelayedExpansion

set directory=%1
set file_list=%2

for /f "tokens=*" %%i in (%file_list%) DO (
set newName=!%%i:~0,5!
move "%directory%\%%i" "%directory%\!%newName%!s.jpg" )

endlocal

This is giving me this output with echo on

set newName=!image.png:~0,5!  
move "\\server\path\to\image.png" "\\server\path\to\!!s.jpg"

So clearly newName is null when I need it, so I can't even begin to troubleshoot the most likely syntax issues when trying to cut it to 5 characters.

THANKS!

  • 3
    Hi Adam, Did you see an example on some where on the Internet or StackOverFlow that said you could sub-string a FOR variable? Just want to make sure there isn't any bad answers on StackOverFlow that we should take care of. – Squashman Nov 07 '16 at 14:36

1 Answers1

1
set "newName=%%i"
set "newName=!Newname:~0,5!"

You can't substring a metavariable like %%i. You need to assign it to a standard environment variable, then substring using the method indicated (!var...! in delayedexpansion mode because the altered value of the variable is required within a code block)

The quotes simply delimit the strings involved to ensure that any invisible stray spaces on the line are not included.

Magoo
  • 77,302
  • 8
  • 62
  • 84