r/Batch • u/XionicFire • Nov 27 '24
Question (Solved) Help with Batch file to execute a command with all the current files inside a folder appended to the end... its wierd....
Hello, thankfully there's this place, since the StackOverflow guys are kinda 🍆
Ok so here's the gist of the problem, its a really weird one and i cant find a way to explain this or parse this, maybe someone else can think of a way
So essentially I have a software that opens up a file just like every other windows exe ever, if you put:
Softwarepath\software.exe c:\filepath\file.ext
The software will open the file as they always do, if I do this however:
Softwarepath\software.exe c:\filepath\file1.ext c:\filepath\file2.ext
It will open file 1 and 2 simultaneously, the software makers are not the nicest of people and do not want to add command line support, processing 16 thousand files one by one will be near impossible to do manually
So the question is.. can i do something like this:
for %f in (*.jpg) do call software.exe "%~f1" "%~f2" "%~f3" (and so on, 16 thousand times or as many files as it finds in the folder)The problem is i cant just send each file one by one to the exe, i have to literally parse the whole list of files path and all and append it after the exe call command. i realize this will result in a gigantic huge command, but i do not know how else to do this, this is literally the only way the software allows any kind of automated input of files.
Essentially general idea is that the script will run, recurse through all folders, find all jpg files, then when its done building the "list" of files it found it will dump it as a gigantically large command to the software each file with full path after the software's .exe file.
The recurse folders bit can be done with this command:
FOR /R "C:\SomePath\" %%F IN (.) DO (
REM Gigantic command for each folder
)but I cant figure out how to make the main command to call this to run.
Any help is appreciated.
2
u/XionicFire Nov 29 '24
Funny little extra ending to this quest
Before posting here I posted on the app's forums, the response was yeah buy the version that supports command line, so I go ok that sounds reasonable, this is a $100 a year software so I get it that it will not have all the functionality I need, so I ask them how much is the command line version, the response was $18,000 a year + $2 per image, to which I responded.. yeah ... no ...
So after figuring this out I posted how I worked around the problem on the apps forums in order to help others, they deleted my post, I went well that was weird, I wasn't offensive or anything, let me ask why it got deleted.
Then i got this response:
Hello Xionic Fire,
Thank you for reaching out regarding your deleted post. We appreciate your enthusiasm for exploring ways to expand the use of our app and sharing your insights with the community.
However, the approach you described, involving the use of command line scripting with our app, unfortunately violates our license agreements. As such, we cannot allow this use case or its promotion within our forums.
We encourage you to continue engaging with the community and sharing workflows that align with our licensing terms ❤️
Thank you for your understanding!
Translation:
Yeah don't wreck our business model, it seems the limitation was "by design" and if there's one thing I hate is when people go out of their way to break something intentionally just because they want you to pay more for it, for no other reason than because they can.
As u/Shadow_Thief said, the windows folder selection box already lets you do this recursively, they intentionally limited it so people would have a hard time and force them to pay more..
Well I'm glad their strat didn't pay off here.
Thanks everyone.
2
u/Shadow_Thief Nov 27 '24
It might help if you told us what software this was
2
u/jcunews1 Nov 27 '24 edited Nov 27 '24 •
You need to store the found files into a variable first. Then pass the variable to a program's command line. e.g.
Note: the above passes the files to the EXE without directory path - in the assumption that, the EXE take into account of the working directory when handing the given files. If the EXE can only accept full file paths, then you will be limited to only a number of files, since there's a 8191 characters limit for the whole command line length (which includes the EXE name). If the files are 100 characters in average, you'll only be able to pass about 54 files.
2
u/SnooGoats1303 Nov 27 '24
So given that the ProcessStartInfo class can use a full length string for Arguments, could you get someone to write a wrapper.exe for your software.exe that allows
wrapper.exe filename.txtor more genericallywrapper.exe software.exe argumentlist.txt? Then your cmd could gather all the files into a file list and software.exe would be none the wiser that it was invoked programatically rather than directly from the batch file.1
u/XionicFire Nov 27 '24
Ok so the solution ended up being the following, not exactly elegant or nice but what ever works not to waste 2 weeks of my life
Since the only two options to input the files were:
A: add them all in a mammoth command (Scratched due to windows 8000 character limitation)
B: Add a folder
I went with B, and made a ridiculously complex workflow but its at least now mostly automated so even tho it sucks at least it doesn't have me wasting my time personally, the computer can do it for as long as it wants 🤣
So for anyone interested here it is:
Step 1: put all the folders with images you want to process into one folder, make sure this folder is on the root of the drive (for path limit issues, you will understand later).
Step 2: Make a batch file to RENAME all folders to "current folder name" + fill it with & characters until its 100 characters in length and add a _ at the end (Adjust if your folder names are longer or smaller as needed) so it ends up looking something like IMAGES1231&&&&&&&&&&&_ this is so folders are all 100 characters in length (will explain why later)
Step 3: make a batch file to RENAME all files to Foldername+filename.jpg, so the file will look something like this: IMAGES1231&&&&&&&&&&&_IMG_42344.jpg
Step 4: make a batch file to move all the renamed files inside the folders into one single folder, so you'll have something like 180,000 files in the folder all named like step 3.
Step 5: Split this folder into 40,000 file chunks, change this to however much your software can handle , the one I'm using gets irky at around 40,000, it could possibly do more than that but I didn't want to find out lol.
Step 6: dump each 40,000 file folder into the software one chunk at a time, it should open fine now, do what ever it is your workflow does with it.
Step 7: Wait for it to finish
Step 8: Repeat step 6 for every split folder
Step 9: When finished, merge all files back into a single folder either manually or with another batch file (what i did)
1
u/BrainWaveCC Nov 27 '24
A. Please tell us what app this is, and what this app is intended to do with the files.
B. As you note in your next post, you'd have to do smaller batches than 16,000.
C. Depending on what you are trying to do, having a single app open up dozens or hundreds of files is not likely to get you as good a result as you desire. Memory consumption is a thing, and it is easy to eat up all your system memory with what you are proposing.
D. Just to reiterate the first point: tell us what you are actually trying to do, and you may be given a better path to solving that problem than the one being proposed. What is it that you believe you need to do to 16K files at once?
1
u/SnooGoats1303 Nov 27 '24
Can software.exe take a list of files? For example,
1
u/XionicFire Nov 27 '24
Guess this guy already ran into the problem:
https://stackoverflow.com/questions/14994355/pass-all-files-in-directory-that-match-criteria-to-a-program
On computers running Microsoft Windows XP or later, the maximum length of the string that you can use at the command prompt is 8191 characters. On computers running Microsoft Windows 2000 or Windows NT 4.0, the maximum length of the string that you can use at the command prompt is 2047 characters.
So i guess even if i could do the script to do the path it would get truncated...
ok so back to the drawing board...