ShareX / ShareX

ShareX is a free and open source program that lets you capture or record any area of your screen and share it with a single press of a key. It also allows uploading images, text or other types of files to many supported destinations you can choose from.
https://getsharex.com
GNU General Public License v3.0
28.28k stars 3.11k forks source link

Feature Request: Zip Folder and Upload workflow #5733

Closed tnthhp96 closed 2 years ago

tnthhp96 commented 2 years ago

Can a task be added to zip a folder before upload it (optionally with a task to delete the zip file afterwards)? Perform action can only be activated after capturing a screenshot, not as a standalone task.

vertigo220 commented 2 years ago

I'm pretty new to ShareX, so I apologize if I'm not fully understanding what you're trying to do, but it sounds like you want to take multiple screenshots, zip them, then upload them, but ShareX doesn't have the means to issue a command, i.e. perform an action, before upload, only after capture. If that's the case, the obvious, and probably fairly easy, solution is to add a command task, which could then be setup to run the command line necessary to zip the files and added to a hotkey under workflows, so it could be triggered manually before an upload (or automatically if the option is added, which, again, should be fairly easy).

In the meantime, probably just setting up an AutoHotkey script to run the command would be the best bet, and that would be very easy. Assuming I'm understanding what you're trying to do and that all sounds right and you want to go that route, let me know if you need help.

tnthhp96 commented 2 years ago

Thank you for your response. The task I need to complete is a bit different. Sharex has an "Upload with Sharex" option in the Explorer context menu, but if I do it with a folder, then it will upload the files individually and give me a bunch of links. Therefore, I want to zip the folder so that I only have one link. Since ShareX already has the capability to zip files after capture, it would be nice if I could do it before uploading.

vertigo220 commented 2 years ago

Ok, that should be fairly easy to DIY. You could simply right-click > 7-Zip > Add to archive, then right-click > Upload with ShareX. If, however, you want to be able to do it in one step, that can be done. It looks like ShareX's command for uploading something is just sharex.exe %1, where %1 is the file. So steps to do this:

  1. Make a batch file that zips the files then issues the command sharex.exe %1
  2. Add link to the batch file to the context menu

Should be very easy. Again, let me know if you need help.

tnthhp96 commented 2 years ago

Please forgive me for asking, but I'm completely lost when you go to "batch files that zip". The process of making that happen is beyond my understanding. Would you be able to elaborate on those 2 steps further. Your help is greatly appreciated.

vertigo220 commented 2 years ago

7-Zip (the archiving tool most commonly used, which is far better than Windows' built-in functionality) has a command-line feature, and a batch file is simply a script that conditionally runs various commands. So you simply put the appropriate 7z.exe command in a batch file then run the batch file. For example:

@echo off

"C:\Program Files\7-Zip\7z.exe" a -r "%userprofile%\Documents\ShareX\Screenshots\Upload.7z" "%1"
REM OR "C:\Program Files\7-Zip\7z.exe" a -r "%userprofile%\Documents\ShareX\Screenshots\Upload.7z" "%userprofile%\Documents\ShareX\Screenshots\Folder to Upload"
"C:\Program Files\ShareX\ShareX.exe" "%userprofile%\Documents\ShareX\Screenshots\Upload.7z"
del /q "%userprofile%\Documents\ShareX\Screenshots\Upload.7z"

exit /b

That's the most basic script, but of course it can be expanded on to e.g. include the current date and time to the upload file name, determine which files/folders to archive and upload, etc. It will open ShareX which will perform the upload as normal. If there's a switch/argument for it, it may be possible to do it without ShareX opening, but I don't know off-hand, and I'm guessing you'd want it to open. Running the batch file directly will result in a black window popping up while it's working, but if that's not desired it's easy to have it run without that by running it indirectly. All you need to do is add a registry entry with the command for the batch file (or the indirect command) and call it Zip and Upload with ShareX and give it the ShareX icon, and it'll be as if it was built in to ShareX itself.

tnthhp96 commented 2 years ago

Thank you for your detailed response. The command above seems to use a predefined folder (ShareX screenshots folder) but what I would like to do is to upload a folder, either via the context menu in File Explorer (ShareX has an "Upload with ShareX" button) or using a folder picker. Is it possible to choose the folder I want using a command?

vertigo220 commented 2 years ago

I included two different commands in the example. The first would work as you want, the %1 refers to the folder you would right-click and select to upload. That would zip and upload all files within that folder, so if that's what you want, that's all that would be needed. Otherwise, the script would need more to determine which files to include and which not to, which is pretty easy. As an example and a test, simply copy paste that into a file, save it as a batch file, and drag a folder you want to upload onto it. Obviously, 7-Zip needs to be installed and in that location (or you can modify the location to point to it, or even have the script find the location on its own) and ShareX needs to be installed in that location, and you may want to use a different path for the 7z file that's created. And there needs to be a line to delete the 7z file, which I'll add above.

tnthhp96 commented 2 years ago

I'm using the first part of the code and modify the path (video in below), however, it produced a zip file that was 22KB out of a 40MB folder. Do I make a mistake somewhere? https://streamable.com/6vei73 ` @echo off

"C:\Program Files\7-Zip\7z.exe" a -r "%userprofile%\Desktop\Upload.zip" "%1" REM

exit /b `

vertigo220 commented 2 years ago

You need to make that a .bat file, not .cmd. Also, I recommend making it a .7z instead of .zip, as it's a much better compression method and will typically result in smaller archives, sometimes significantly smaller.

Also, be aware that your name is (mostly) visible in that animation you uploaded (edit: it's actually fully visible in the properties window). Be careful about self-doxxing. :)

tnthhp96 commented 2 years ago

The error persists even after changing to .bat and using .7z. Could I have gotten the code wrong somewhere? In addition, thanks for the take-care-of-your-privacy part. I must be smarter from now on.

vertigo220 commented 2 years ago

My fault. I thought quoting %1 might be an issue, and it was. It already quotes it, so that results in double quotes which breaks it. Just remove them, so it should be ...Upload.7z" %1 Also, "REM" isn't needed, that just says "this line is a remark/comment" so the interpreter doesn't execute it.

Also, 7z appears to have some strange behavior that I'm looking into, which makes it take longer than it should. But for now, what you have, minus the quotes, will work.

vertigo220 commented 2 years ago

Ok, figured it out. With the command I provided, it scans for the folder name (Driver Easy in your case) in all subdirectories of the folder it's in, which isn't what we want. We want to just include that folder from the current folder, so the command would be:

"C:\Program Files\7-Zip\7z.exe" a -r "%userprofile%\Desktop\Upload.zip" %1\*

This will put the files directly into the archive. If you want them put in it under the folder name (Driver Easy) that will require something a bit different.

tnthhp96 commented 2 years ago

Zip and upload functions perfectly, but the command ends right after upload without deleting the file. Can you check if I made a mistake? Also, if possible, could you give me a brief outline on how I can make the zip file have the same name as the folder. I'm sure I can figure out the rest using Stack Overflow.

"C:\Program Files\7-Zip\7z.exe" a -r "%userprofile%\Desktop\Upload.zip" %1\* "C:\Program Files\ShareX\ShareX.exe" "%userprofile%\Desktop\Upload.zip" del /q "%userprofile%\Desktop\Upload.zip" exit /b

vertigo220 commented 2 years ago

That was the other thing I was worried about. The script will only proceed on to the next line once the current line completes, and once it launches StartX, it basically gets stuck there. There's a way to make it keep going, but then the archive gets deleted and StartX can't find it to upload it. And there's no way that I can tell for StartX to say when it's done. There's several different workarounds I can think of, but they're all fairly hacky and each has its own issues. Probably the best way would be to have StartX show the "After upload" window, which the script could monitor for and, once found, delete the file, but that would result in another window opening up that you would have to manually close (though you could use some other software to watch for that window and close it, but that's getting messy). Or you could have the script watch for the window and, when it sees it, it could kill StartX, which would close the whole program, including the window, then restart it. Yet another option would be to not even use StartX, and find a different software to perform the uploads. Or you could have the files deleted when Windows loads, which wouldn't work well for me since I rarely reboot, but it might for you. Or delete them when unlocking the workstation, on the assumption that if it was locked, you weren't uploading screenshots, though the chance exists you could start an upload, lock it, then come back a minute later and unlock it before the upload finishes. And on and on, with many other possible methods. Perhaps I'll think of something, but I'm not hopeful.

As for naming the archive, that's easy. Here's the code, though the del line obviously doesn't work, but I included for completeness:

@echo off
setlocal

set "archive=%userprofile%\Desktop\%~n1.7z"

"C:\Program Files\7-Zip\7z.exe" a -r "%archive%" %1\*
"C:\Program Files\ShareX\ShareX.exe" "%archive%"
del /q "%archive%"

exit /b

And your code appears to be all on one, wrapped line. I'm assuming that's just a formatting error when posting it here, but in case it's not, make sure each line is on a separate line.

tnthhp96 commented 2 years ago

ShareX shows a notification with the uploaded link after uploading is complete. Are you aware if we could detect it and activate the deletion?

vertigo220 commented 2 years ago

I don't think so, since it's a notification and not a window, and it shows briefly so it would have to check very often in order to catch it, vs just checking every few seconds or more. I'll take a look.

vertigo220 commented 2 years ago

As expected, the pop-up doesn't show in tasklist since it's not an actual window. Another possibility is setting an after upload task to copy the URL, then clearing the clipboard beforehand and checking it periodically until it contains an appropriate link, but that would again have its own issues and be pretty hacky. I think I may have a way, though it's not ideal, but it's probably the cleanest and least intrusive.

vertigo220 commented 2 years ago

First idea was a bust, since I thought ShareX opened when uploading, but turns out that was just because I didn't have it configured. Was trying to use the percent complete in the title bar to tell when it was done, but can't do that without a window. Anyways, I came up with a different method, which should work. Try it out and let me know:

@echo off

set "archive=%userprofile%\Desktop\%~n1.7z"

"C:\Program Files\7-Zip\7z.exe" a -r "%archive%" %1\*
start "" "C:\Program Files\ShareX\ShareX.exe" "%archive%"

set /A i=0
:upload_started_check
timeout /t 1
set /A i+=1
REM Performs goto command only if file not locked, in order to loop until upload starts which locks file, protecting it from deletion
if %i% neq 60 copy /b "%archive%"+NUL "%archive%" >NUL && (goto upload_started_check)

set /A i=0
:archive_delete
del /q "%archive%"
timeout /t 1
if EXIST "%archive%" if %i% neq 3600 set /A i+=1& goto archive_delete

It checks until the archive is locked by ShareX uploading it, so it doesn't try deleting it before then, then it just tries over and over to delete it, which it won't be able to do until ShareX is done with it. It times out after ~60 seconds on the first check and ~10 minutes on the second, which should be more than enough time for ShareX to start and finish the upload, respectively, and prevents the script from just running endlessly in the background if something goes wrong. The timeout command isn't exact, so it could end up being a good bit less than 60 seconds and 10 minutes, but time will tell if it works or there are issues, and it would be easy to fix if there are.

Jaex commented 2 years ago

There is this after capture task which is executed after upload:

So you can create specific workflow for zip upload and use that workflow with cli, that way zip file will be automatically deleted after upload is complete.

You can check cli commands from here: https://getsharex.com/docs/command-line-arguments

tnthhp96 commented 2 years ago

@Jaex, I accidentally hit "Close with Comment" so I apologize if it caused any issues.

Are you suggesting that I create a Hotkey (Workflow) with "Upload file" task, and select "Delete file locally" in "Override after capture settings", then use a ShareX cli command, like -task “Hotkey description” to activate that Hotkey after the ZIP file was created by 7-zip?

Since if "Delete file locally" is enabled in the global "After capture task", it will delete every screenshot that is captured. Please tell me if I'm understanding you correctly.

tnthhp96 commented 2 years ago

@vertigo220 Thank you for stimulating my brain with many intrigue ideas. It seems that Jaex's solution is the most elegant, which is to create a Workflow in ShareX that only uploads and deletes that file, then run a Cli command after executing the Zip command.

Jaex commented 2 years ago

Are you suggesting that I create a Hotkey (Workflow) with "Upload file" task, and select "Delete file locally" in "Override after capture settings", then use a ShareX cli command, like -task “Hotkey description” to activate that Hotkey after the ZIP file was created by 7-zip?

Yes. It not activates the hotkey, it will use that workflow with cli file upload.

tnthhp96 commented 2 years ago

@Jaex Thanks so much for helping. I will close this issue.

tnthhp96 commented 2 years ago

@Jaex After uploading, the delete task does not execute. It appears that delete only executes after capture. Is it working for you or am I doing something wrong?

Jaex commented 2 years ago

Looks like I made it that way because people can accidentally delete files they are uploading from explorer. So removing that check can cause unintended behavior now, so not sure how to handle.

Jaex commented 2 years ago

Now it should work with file uploads too.

tnthhp96 commented 2 years ago

Now it should work with file uploads too.

Did you just update the changes to the app?

Jaex commented 2 years ago

https://getsharex.com/docs/dev-builds

tnthhp96 commented 2 years ago

Thanks a lot. Works flawlessly.