1

I'm writing a command line batch script in Windows 7 to upload multiple images for hosting, paste the url (using this program) and filename into a line of text, then append the text to a file.

The following code gives me an error because the command "cmd /c" is too long.

@echo on
set DIR1="C:\Users\My\Desktop\temp"
set DIR2="C:\Misc\Programs\EasyImgur"
set DIR3="C:\Users\My\Desktop"

set DIR2=%DIR2:"=%
set DIR3=%DIR3:"=%

forfiles /m *.png /p %DIR1% /c "cmd /c %DIR2%\EasyImgur.exe /anonymous @path && timeout /t 2 /nobreak > NUL && paste > %DIR3%\temp.txt && set /p URL= < %DIR3%\temp.txt && echo if(G9="@fname",IMAGE(%URL%,3), >> %DIR3%\test.txt"

del %DIR3%\temp.txt

pause
exit

Is there any way around this? Some way to call all that text after "cmd /c" without breaking the character limit? I need all that done for every file in the directory.

Thanks in advance.

8
  • Use PowerShell or msys? (I'm not really kidding on either account.) Commented Sep 2, 2015 at 2:25
  • If you use for instead of forfiles you can convert the command into a subroutine. (See call /?) Commented Sep 2, 2015 at 2:50
  • Well, for worked better as it did not have the character limit. My only problem now is why the following line doesn't import from the .txt: paste > temp.txt && set /p URL=< temp.txt && echo %URL% It did write to the .txt file just fine, but why can't it import it to the URL variable? Commented Sep 2, 2015 at 4:11
  • The environment variable is expanded before the loop executes. See for example stackoverflow.com/a/6500797/886887 ; you can fix the problem either using delayed expansion (as per the linked answer) or by using a subroutine. Commented Sep 2, 2015 at 4:29
  • So close! I used delayed expansion, and I get exact text I want but its surrounded by quotes. How do I remove JUST the surrounding quotes, not the quotes in the inside? The text below is set to a variable called TEXT. My result is the following: "if(G11="Akadi",IMAGE(http://i.imgur.com/mr7GBBD.png,3)," Commented Sep 2, 2015 at 5:13

2 Answers 2

1

Thanks to Harry Johnston's help, I got it! Check it out.

The code is:

@echo off

setlocal EnableDelayedExpansion

set "DIR1=C:\Users\RyzaJr\Desktop\temp"
set "DIR2=C:\Misc\Programs\EasyImgur"

for %%a in (%DIR1%\*.png) do (

    "%DIR2%\EasyImgur.exe" /anonymous "%%a"
    timeout /t 4 /nobreak > NUL
    paste > temp.txt
    set /p URL= < temp.txt
    echo if(G11="%%~na",IMAGE(!URL!,3^), >> "output.txt"

)

del temp.txt
exit
Sign up to request clarification or add additional context in comments.

Comments

0

This might work for you - the term you echo into the file has mismatched parenthesis though.

@echo on
set "DIR1=C:\Users\My\Desktop\temp"
set "DIR2=C:\Misc\Programs\EasyImgur"
set "DIR3=C:\Users\My\Desktop"

for %%a in ("%DIR1%\*.png") do (
  start "" /w /b "%DIR2%\EasyImgur.exe" /anonymous "%%a"
  set "flag="
   for /f "delims=" %%b in ('paste') do (
     if not defined flag >>"%DIR3%\test.txt" echo if(G9="%%~na",IMAGE(%%b,3^),
     set flag=1
   )
)
pause

2 Comments

Your answer is really close to working, but it needs the timeout because it doesn't upload to imgur fast enough, so the URLs were wrong. Yeah, I know about the mismatched parenthesis. I did it on purpose because the if() parameters would be nested inside eachother.
The start command with the /wait switch is supposed to stop the batch file proceeding until the command is finished. Maybe the easyimgur.exe doesn't behave as a normal command line tool should behave - or the paste tool (presumably a clipboard reading utility) is at fault. But does it work correctly if you put the timeout command back in?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.