0

I'm trying to open a batch file using Git Bash with a file path as parameter. I can grab the parameter using %1. However, when I give it the path, I get this error:

I/O Error: Couldn't open file 'C:UsersUsernameDownloadsFile.pdf'

I run my batch like this:

count.bat C:\Users\Username\Downloads\File.pdf

I've tried replacing \ with / in the file path, which works, but I'm going to drag'n'drop a file into command prompt, and I don't want to replace all the backslashes. Here's what I've done:

setlocal ENABLEDELAYEDEXPANSION
set variable="%1"
call set variable=%%variable:\=/%%

pdftotext %variable% -enc UTF-8 - | wc -m

That should replace \ with / but it doesn't. I get the same output doing this versus simply doing pdftotext %1 -enc.....

EDIT: Turns out Git Bash treats \ as an escape character. This means I can't simply type in the path without quotes around it, as it would try to escape it. I ended up using geisterfurz007's suggestion about opening cmd.exe, then do the file.bat, which would call sh.exe on my file.sh file, which would THEN do the whole operation. A workaround but at least now it works using cmd.

MortenMoulder
  • 6,138
  • 11
  • 60
  • 116
  • No need for delayed expansion, and no need for `call` and double-`%` here... Sometimes (depending on the program) replacing a single backslash by double-backslashes might also help... – aschipfl Nov 25 '16 at 12:10

1 Answers1

1

I am not sure what exactly the problem is for you but the following does the job for me:

@echo off
setlocal EnableDelayedExpansion
set variable="%~1"
set variable=%variable:\=/%
echo %variable%

Notice the ~ in %~1 This will automatically remove surrounding quotes if there are any. I am not sure if the call or the double % were causing the problem but they are not needed in this case if I am not mistaken.

Usage: foo.bat "C:\my Path\to\yeeeeey.file"

Output: "C:/my Path/to/yeeeeey.file"

Usage: foo.bat C:\Users\Username\Downloads\File.pdf

Output: "C:/Users/Username/Downloads/File.pdf"

EDIT: After a longer chat session with MortenMoulder we found a workaround for this: It seems that starting Git-Bash with a bash file including his one command pdftotext $1 -enc UTF-8 - | wc -m with the parameter from the batch file seemed to work:

batch-file.bat:

@echo off
setlocal EnableDelayedExpansion
set variable="%~1"
set variable=%variable:\=/%
C:\Program Files\Git\bin\sh.exe C:\Users\Username\count.sh %variable%

count.sh:

pdftotext $1 -enc UTF-8 - | wc -m
geisterfurz007
  • 5,292
  • 5
  • 33
  • 54
  • That's because you're surrounding your path with double quotes. That's not always the case. Look at my example. The path has no spaces, which means double quotes aren't necessary. – MortenMoulder Nov 25 '16 at 09:51
  • Nope! See my edit. It does not matter if you are surrounding it with quotes when starting the file. Have a look at the first line under my code please. If there are no quotes, none will removed (obviously) but if there are any they will be removed. – geisterfurz007 Nov 25 '16 at 09:55
  • I'm not getting the same results as you. Hmm. When I do `echo %variable%` it gives me `C:UsersUsernameDownloadsFile.pdf`. – MortenMoulder Nov 25 '16 at 09:57
  • Then it seems like you have `set variable=%variable:\=%` (missing `/` after the `=` )... Have you just copied the code I got? – geisterfurz007 Nov 25 '16 at 10:04
  • Yes I have copied exactly what you have. – MortenMoulder Nov 25 '16 at 10:15
  • Hmm I noticed this only appears to be a problem using Git Bash. Basically bash. It works fine using command prompt. I thought it would execute the same regardless? :O – MortenMoulder Nov 25 '16 at 10:17
  • Ayyy Bash aint Batch! Have a look [here](http://stackoverflow.com/questions/22397521/execute-commands-from-bat-file-on-git-bash-terminal) where someone seemed to have the same problem. – geisterfurz007 Nov 25 '16 at 10:22
  • I'm writing this this in batch though. Everything is batch except I need to run it in Git Bash (or any other bash terminal) because of `pdftotext` and `wc`. Also, I can run it fine. No problem with that. – MortenMoulder Nov 25 '16 at 10:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/129019/discussion-between-geisterfurz007-and-mortenmoulder). – geisterfurz007 Nov 25 '16 at 10:24