0

i am making a batch base64 decoder and encoder but I want want to be able to drag and drop multiple files and not just one at a time.

I have tried this

if "%~1" == "" goto :EOF

for %%I in ("%~1\*.tex") do certutil -encode %* tmp.b64 && findstr /v /c:- tmp.b64 > %random%.b64

here is the working code for just one file drag and drop

certutil -encode %~1 tmp.b64 && findstr /v /c:- tmp.b64 > %random%.b64

the batch wont encode the files all at once.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • 4
    use [shift](https://ss64.com/nt/shift.html) and [goto](https://ss64.com/nt/goto.html) – Stephan Jun 11 '19 at 19:09
  • 2
    Take a look also at this: [Batch-Script - Iterate through arguments](https://stackoverflow.com/q/19835849) – aschipfl Jun 11 '19 at 19:27

1 Answers1

2

%random% will be constant throughout all iterations of the loop. You would need to enable delayed expansion and use !random! instead. But I would not even do that because !random! can give duplicate values. Unlikely, but possible. If it were not possible for duplicates, then it would not be random.

I would simply append .b64 to the original name.

You don't need FINDSTR to filter out the header/footer. There are options to control the format of the CERTUTIL output.

I assume you want the encoded output files in the same directory as the source.

Since the arguments will be a series of file names (paths), you can simply iterate %* with FOR.

@echo off
for %%F in (%*) do certutil -encodehex -f %%F %%F.b64 1

Another nice result of the above - you can call the script on the command line with wildcards, and the FOR loop will iterate all matching files.

But there is one potential problem when you drag and drop - Windows properly encloses the file in quotes if it has spaces. But it does not enclose the file in quotes if the name has & without any spaces.

So if you drag and drop any file names with & that don't contain space, then the simple solution above will fail.

The code is much more complicated if you want to handle drag and drop with & in the name.

dbenham
  • 127,446
  • 28
  • 251
  • 390
  • is there a way to use big files like a GB or 5 MB instead of little file like text file? I have these 5 MB or 1 GB files I want to encode and was if we could go pate the 32 bit limit? error: Input Length = 147058392 EncodeToFile returned Arithmetic result exceeded 32 bits. 0x80070216 (WIN32: 534 ERROR_ARITHMETIC_OVERFLOW) CertUtil: -encodehex command FAILED: 0x80070216 (WIN32: 534 ERROR_ARITHMETIC_OVERFLOW) CertUtil: Arithmetic result exceeded 32 bits. – unknown unknown Jun 12 '19 at 02:51
  • also what is the make limit of size? – unknown unknown Jun 12 '19 at 03:21
  • 1
    @unknownunknown - The CERTUTIL size limitations are undocumented. Others have tested and determined that the base64 encode source limit is 74,472,684 bytes. The hex encode limit is smaller at 21,510,272 bytes. Powershell is able to encode/decode base64 and hex, but I have no idea of the size limits. If that does not work for you than I think you will need to find a 3rd party utility. – dbenham Jun 12 '19 at 11:57
  • I have made it smaller to 29 files instead of just one so it works and thanks for your work. – unknown unknown Jun 12 '19 at 17:46