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.

Ryza Jr.
  • 105
  • 9
  • Use PowerShell or msys? (I'm not really kidding on either account.) – user2864740 Sep 02 '15 at 02:25
  • If you use `for` instead of `forfiles` you can convert the command into a subroutine. (See `call /?`) – Harry Johnston Sep 02 '15 at 02: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? – Ryza Jr. Sep 02 '15 at 04:11
  • The environment variable is expanded before the loop executes. See for example http://stackoverflow.com/a/6500797/886887 ; you can fix the problem either using delayed expansion (as per the linked answer) or by using a subroutine. – Harry Johnston Sep 02 '15 at 04: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),"` – Ryza Jr. Sep 02 '15 at 05:13
  • Hard to say without seeing the latest version of the code. But looking at the original code, you're setting variables to values with quote marks around them and then removing the quote marks ... you realize you don't have to do that? You can just say, for example, `set DIR1=C:\Misc\Programs\EasyImgur` ? – Harry Johnston Sep 02 '15 at 05:28
  • I posted the latest code as an answer. The directories are fine, and I want them in quotes in case there are spaces. The problem is the output. – Ryza Jr. Sep 02 '15 at 05:43
  • The `set` command would still work even if there are spaces in the path. But it *is* preferable to use quote marks to prevent any inadvertent spaces at the end of the line from becoming part of the value - you just need to put the opening quote before the variable name, as you've done in your posted answer. – Harry Johnston Sep 03 '15 at 23:01

2 Answers2

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
Ryza Jr.
  • 105
  • 9
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
foxidrive
  • 40,353
  • 10
  • 53
  • 68
  • 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. – Ryza Jr. Sep 02 '15 at 05:34
  • 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? – foxidrive Sep 02 '15 at 06:28