1

Following Check if a file exists in zip archive, I have a batch file that processes some files in a directory, and I use this line inside it:

call zipjs.bat list -source "C:\myZip.zip" -flat yes|find /i "filename" && (
  echo file exists
  color 
)|| (
  echo file does not exist
)

I have zipjs.bat in the same directory as my batch file:

Desktop->MyBatch.bat

Desktop->zipjs.bat

MyBatch.bat calls zipjs.bat and works fine as expected on my machine.

Problem:

I have got an access to a virtual machine to test my code, it is a windows desktop with some software running on it. Nothing fancy. There, I also put myBatch.bat and zipjs.bat under the same directory (Desktop). However, whenever I run myBatch, it prompts out that "zipjs.bat is not a recognized command". It is like myBatch can't locate zipjs.bat. What could be the issue?

Sandra K
  • 1,209
  • 1
  • 10
  • 21
  • can it be that is requires a path? – Lexib0y Dec 27 '17 at 15:14
  • @Lexib0y hmmm, why would it work without a path on my desktop? – Sandra K Dec 27 '17 at 15:15
  • I meant the path environment variable, is that the same on both desktop as VM? But you could also try to put it in the batch file itself. Unless you run it from the prompt in that directory or have the working directory set in the shortcut that runs the batch file, you need a path environment variable. (your desktop is not in the path by default, neither you can be sure it is the working directory) – Lexib0y Dec 27 '17 at 15:20
  • 1
    if the bat is started with admin privileges it will start in `system32` folder. Try to set `cd /d %~dp0` at the beggining of the file. – npocmaka Dec 27 '17 at 15:22
  • @npocmaka what does `cd /d %~dp0` exactly do? It worked! Maybe expand to an answer and thank you :) – Sandra K Dec 27 '17 at 15:28
  • 1
    cd /d %~dp0 or pushd %~dp0 is often used to change to the original directory from which the batch was started. This is very useful in newer OS's when the user may 'Run as administrator' which changes the current directory for you! Try it sometime. Just make a simple bat @echo off echo.CD=%CD% pushd %~dp0 echo.CD=%CD% pause – RGuggisberg Dec 27 '17 at 15:44
  • 1
    It might also work to use in the batch file `call "%~dp0zipjs.bat"` instead of just `call zipjs.bat` to call the batch file `zipjs.bat` always with path of your batch file independent on what is the current directory on running your batch file. And run in a command prompt window `call /?` explaining `%~dp0` (drive and path of argument 0 which is the batch file itself always ending with a backslash) and `cd /?` for help on commands __CALL__ and __CD__. – Mofi Dec 27 '17 at 16:24
  • Type `where zipjs.bat` will show where it is searching. – ACatInLove Dec 27 '17 at 18:21

1 Answers1

2

If the bat is started with admin privileges it will start in system32 folder. Try to set cd /d "%~dp0" at the beggining of the file (cd command changes the working directory and the /d switch is in case there's need of jumping between different drives).

The %0 argument is the the batch file itself (in case the shift command is not used) , so with %~dp0 you get the drive and path to the file (without the file name).

npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • 1
    Also worth mentioning that as @Mofi mentioned, using `"call %~dp0zipjs.bat"` instead of just `call zipjs.bat` works too – Sandra K Dec 28 '17 at 14:36