0

I create many one-action .bat files which accept drag-and-drop, and just pass an appropriate switch to main executable. The problem with .bat files is that I cannot reliably handle ANY name that was passed to me!

People complain that my programs not working because of funny names in paths or files. Most common examples contain characters like ()%!& and very long names, etc.

I searched this issue and the answer is always escaping; but how in a world I would escape a string that comes to me by arguments, like %1? So far I used short 8.3 names form, "%~s1", until it failed me too…

My question is, should I stop using Windows batch scripts? since I still have to program for XP, I don’t want to even look at PowerShell (or any dependences to Python or any other scripting language).

Compo
  • 36,585
  • 5
  • 27
  • 39
aleksusklim
  • 361
  • 3
  • 11
  • 2
    You can try with [this](https://stackoverflow.com/a/31358421/2861476) – MC ND May 29 '17 at 09:52
  • 2
    This is a lot of rambling, but no code. Generally you can handle arbitrary file names in batch files if you're very careful about variable expansion and quoting. Those two things done right solve most of the problems you can have. Some others, like Unicode file names, can sometimes never work correctly with out-of-the-box settings like Raster fonts, since a lot of operations will run through a codepage conversion which you'd rather avoid there. There are a bunch of Batch file gurus on SO, but I can guarantee you, they all want to rather see code than vague ideas. – Joey May 29 '17 at 09:52
  • @MCND, thanks, but it still gives me `"D:\test\C:\WINDOWS\Temp"` on a file `"D:\test\%temp%"`. Now I’m think that it is just impossible to handle with Batch, period. – aleksusklim May 29 '17 at 10:30
  • it is possible to make it work, you just have to build the variables yourself. I have done this before so can you. As far as your `&&` file is concerned as an example. you can escape most of these special characters easily. so `C:>&&` will give an error, but `C:>^&^&` will execute correctly, Spend some time and build your own conversions, post the code so we can see and can help where needed. – Gerhard May 29 '17 at 10:49
  • Same with `%temp%` when you do `D:\test\^%temp^%` the file is being recognised as a file and not a %temp% variable. – Gerhard May 29 '17 at 10:56
  • 2
    @aleksusklim, Tested with a file named `%temp%` and when dropped it will be correctly handled. But, if you use the command line, the variable reference is expanded by the parser *before* the batch file could handle it. – MC ND May 29 '17 at 11:14
  • @Compo, hey, I worked on my own edit during this time... Can I show it? – aleksusklim May 29 '17 at 11:18
  • @GerhardBarnard, problems are not in a string escaping! I talking about **user’s** files, not mine. User is also a one who invoked my program. If he wants to process "%temp%"-named file so bad – he will eventually got how to do this. But my idea was for user to work directly with .bat file, no interactive CMD at all! (Also when he used a command line, the user has more power in operating my program, because he can ask for help via switch, and then provide everything directly. But I targeting an average user, who can only read console screen, but not write to it) – aleksusklim May 29 '17 at 11:24
  • @Joey, I prepared an update to question, but here is a simple piece of code: `for %%i in ("%~1\*") do call :loop "%%i"` – I want to process all files in a dropped directory. In a loop I have f.e. `echo File: "%~s1", size is "%~z1"`. For me (Win32 XP), it will not work if there is for example "\100% quality\" folder in path to dropped one. The percent disappears. – aleksusklim May 29 '17 at 11:33
  • @aleksusklim, whilst it is encouraged to edit your questions for better understanding and readability, please ensure that your question isn't too far removed from the original such that a new question would have been a better decision. In this case as your initial question was answered perfectly well in the first comment, _should therefore be marked as a duplicate_, so it would be prudent to edit it youself, **being very careful to keep it succinct**. Also if you do, please fix the typo in the title, `parameters`. – Compo May 29 '17 at 11:55
  • @MCND, Hmm… If I change `if not errorlevel 1 goto :dropped` to just `goto :dropped` (in "Determine call origin" part) then it worked indeed! Well, I’ll try to get to the bottom of this and came up with my own solution based on yours. Which operating systems did you check? For me, initial script not working on XP nor Win 7. – aleksusklim May 29 '17 at 12:17
  • Tested (now, and working as is) in Win 7 32bit (`6.1.7601 spanish locale`) and Win 10 64bit (`10.0.15063 spanish locale`) but as the post was from 2015 probably I tested it in XP also, but I'm not sure. – MC ND May 29 '17 at 12:27
  • @Compo, when I made my answer an post it as a solution, using MCND’s code, can I put stuff that you deleted from my question – to my answer? After I explain the solution, as a bonus (I’ll show how to do things that I described – but with use of that code particularly). Or do you think it would be unnecessary waste of space? – aleksusklim May 29 '17 at 12:45
  • You can 'Post Your Answer' to your own question, _do not add it by editing your question_, make sure it is focused on the task. – Compo May 29 '17 at 13:16
  • @MCND: I didn’t abandon this, but now faced a similar issue – not with drag-and-drop, but a regular command prompt execution: https://stackoverflow.com/questions/44698833/ . More help is appreciated! – aleksusklim Jun 22 '17 at 12:02

0 Answers0