How to get file's size on UNC path without pushd? - batch-file

I'm helping a bud fix an application that has recently been changed over to using a UNC path. Before he could use a bat file to run
#echo off
echo %~z1
to get a file's size.
Now the bat file won't work because CMD does not support UNC paths as current directories. I thought about using pushd command to temporarily create a drive letter that points to the network resource but I'm thinking there has to be a more direct, cleaner way to do this I'm probably just not experience enough with CMD to know it yet.
Any suggestions or assistance would be greatly, appreciated!
Thanks.
Update
To clarify when the bat file is called (through a PHP file using exec() function) I get nothing in response. I tried a few ways of debugging (it's been a few days so I don't remember exactly what) but the most I could get was "Echo is off" or "The system cannot find the file specified." errors. I can copy/paste the file address into my Windows Explorer and I can find the file fine though.
Update II
It has been noted that the code shouldn't have a problem despite UNC not being supported. If this is true then what else could be the issue? Like I said before I can copy and paste the file paths that are given to the bat file and they open fine in windows explorer.
Update III
I tried timing how long the bat file took to execute and it seems to randomly either take almost no time or a little over a minute. So I'm guessing that might be my problem area. However when I run it via the ajax call its response time is about 550-650. I have no idea what would cause a bat file's execution time to vary by so much. Any ideas would be welcome!
Thanks in advance for any input!

cmd is not compatible with an UNC active directory, but the code in your file will not have any problem with it. You can invoke as
\\server\share\folder\file.bat \\server\share\folder\file.txt
d:\folder\file.bat "\\server\share\folder with spaces\file.txt"
"\\server\share\folder with spaces\file.bat" d:\file.txt
....
and in every case your posted code will work as long as both the batch file and the file to be processed exist

Related

How to install Remove Drive for use in batch file

Pay attention to the "2nd EDIT", that's where the real question is now.
I'm trying to add a bit to a batch file that will safely remove an external drive from my computer. After looking through questions on here that asked the same question, I concluded that Remove Drive was my best option. I downloaded the file and I used this tutorial to install the contents. However, this didn't install Remove Drive so that I can use it in a batch file. Can anyone explain how to do this or how I can find an appropriate tutorial?
EDIT:
I put the 64bit version of RemoveDrive.exe into my System32 folder. This made it so that RemoveDrive was now a recognized command in the cmd window. If I typed in RemoveDrive :E into the console, it would successfully remove the external E: drive. However, when I added the line RemoveDrive E: to my batch file, it acted as if I had only typed in RemoveDrive and displayed the help information at the end of my script. Any ideas why this happened? Is there another method to implement RemoveDrive in a batch file that I'm not seeing in the documentation?
2nd EDIT: I added the path to the function i.e. C:\Windows\System32\RemoveDrive E: -L. This changed the results from simply showing the /? information to making an actual attempt to run the function and remove the external drive. However, it failed. I used the -L to try to remove it safely again if it fails. It ends up "trying....." for long enough that its not going to work. I have the option to hit esc to cancel the attempt or E to "eject media" (I'm not sure what's different about this option and letting it "Try" again. When I hit E, it ultimately results in a "SUCCESS" However, it doesn't actually remove the drive, i.e. I can still access it and all the files.
I tried testing the function in a solo batch file which works fine.
#echo off
`C:\Windows\System32\RemoveDrive E: -L`
My guess, is that its because the rest of the batch file I'm trying to run it in, is copying files from the external drive and moving files to the external drive. I'm guessing that its reading the open console window as an "active" connection to the drive which means it can't safely disconnect. Is there anything I can do in a batch file that would let me disconnect any "connections" to the drive?

Batch Scripts Will not run on some Windows 7 systems

Any ideas why bat files don't run in some cases?
I have written an interactive batch script (also run as an exe) works on THOUSANDS of systems, HOWEVER, a small subset of systems will NOT launch the script. They open a black box window and then close the script without giving the user the bat menu. I tried the bat file association registry reset which ensures that .bat file association is correct but that didn't help.
What I have tried: bat registry fix (.reg file)
(which didn't seem to help, the script starts but doesn't run)
(I also tested a VERY basic script that printed Hello world which also didn't work)
Thanks everyone for your help. What I found out was:
(1) Reset the command prompt:
http://social.technet.microsoft.com/Forums/windows/en-US/0a74090e-53ce-4642-8dad-0523bfd6c8a2/cant-run-bat-batch-files?forum=w7itproui
(which did not work for me)
(2) In a command prompt I had the users type 'set path' which showed that they were missing /windows/system32/ for one user. So far I had one user put it in and it fixed it. One other user had %systemroot%/system32 which I will have them try adding the path the other way to see if it fixes it. Next I will see if I can just add that to the script.

Why doesn't my batch file execute automatically despite being placed in "Startup" folder?

This must sound too naive because I am not a programmer. I need help with a batch file execution.
There are two user accounts on my Windows XP enabled PC and one of these accounts is used by my brother. Sometimes I need to leave messages for him to read as a reminder of something, for example, to back up his personal folders when he logs in next time.
So, I created a simple batch file with the following code:
#echo off
msg * It's time to backup your folders!
When executed by double clicking, the file works fine and shows the expected message. I placed a copy of the batch file in "C:\Documents and Settings\Brother\Start Menu\Programs\Startup" but couldn't make it execute automatically when my brother logged in.
Could somebody please show me where I went wrong?
According to this answer on Superuser, the startup folders for XP are actually in
%AllUsersProfile%\Start Menu\Startup
%UserProfile%\Start Menu\Startup
So the Programs\Startup may be used for something else.
If you are using Windows 10, there are 2 things that may be doing this:
Your file needs Administrator privileges (When i copy any file to startup with Admin privileges it will not work). - My experience
When you boot your computer wait a bit (30 - 50 seconds) to load all files make sure to don't do anything.
This is my experience in Windows 10 please correct me if im wrong in anything

Batch file not working when running in the background - issue with substituted drive

I'll try to keep this short, without all the details of this batch file procedure.
OS: Windows XP
Action: Scheduled Task (background, I can't find a way to run it in the foreground)
Problem: files not created on mapped drive. When the batch file is ran directly, it works.
It does the following (or at least tries to):
clean a directory: works
create executables based on a substituted location
We use the following to map a local folder to the K: drive:
subst K: /D
subst K: D:\Development\SVN
The executables are built from source code that is located on (for example) K:\Sources to K:\Executables.
This fails, for a reason I did not yet discover. As mentioned above, if the batch file is ran directly it doesn't fail. If I substitute K:\ by C:\Development\SVN the issues seem to be resolved but still, this doesn't solve the fact that I can't run it when using K:.
I hope anyone here has an idea, I tried Googling for a long time + scanning SO but to no avail.
Thanks in advance!
substed drives are local to session, and your scheduled task is run in another session then your login session.
It seems to be possible to schedule task for user login session with schtasks.exe command line, but then the task runs only if the user is logged in. It might not be what you want, so using a full non-substed path might be the best solution after all.

Winzip hanging up in scheduled task batch file

I have a simple batch file as seen below that should extract a zip file to the root of E:. The zip file is valid and I can run the batch file from the command line just fine.
Instead of completing the task, it continues to inform me that the Status is "Running". The problem is, it is not running and the file never gets unzipped.
The task is running as a Domain Admin that has been specifically added as an Admin on the box.
Are there any known problems with using zip files in Scheduled Tasks. I actually have this same problem on 3 out of the 12 boxes this task runs on, but there is no rhyme nor reason as to why some servers work, and others don't.
Any ideas on how to debug what is going on, or a solution would be very helpful.
Here is the batch file I'm attempting to run.
SET RootPath=E:
SET WinzipLocation=E:\Program Files\WinZip
"%WinzipLocation%\winzip32" -e -o %CD%\TestZipFile.zip %RootPath%
Try to use the WinZip Command Line Support Add-on.
what if you use 7-zip in command line?
I realized after posting that the "bad" servers were all 64-bit. I was running the 32-bit version of winzip. Since the company I work for doesn't see the benefit in purchasing any software, I had no other option but to starting using 7-zip. I have not tested for any performance increases or hits, but I do not that it works, regardless of the environment.
Thanks for the answers, but it looks like without the 64-bit version of winzip....i have no other options.

Resources